I am out of trial and error again Lists

MRAB python at mrabarnett.plus.com
Thu Oct 23 01:09:54 CEST 2014


On 2014-10-22 23:30, Seymore4Head wrote:
> On Wed, 22 Oct 2014 16:30:37 -0400, Seymore4Head
> <Seymore4Head at Hotmail.invalid> wrote:
>
> One more question.
> if y in str(range(10)
> Why doesn't that work.

In what way doesn't it work?

If you want to know what it returns, print it out.

> I commented it out and just did it "long hand"
>
> def nametonumber(name):
>      lst=[]
>      nx=[]
>      for x in (name):

You don't need to put the parens around name...

>          lst.append(x)
>      for y in (lst):

...or around lst.

You're iterating over a string, putting its characters into a list, and
then iterating over that list. You're doing more work than you need to!

>          #if y in str(range(10)):
>          if y in "1234567890":
>              nx.append(y)
>          if y in " -()":
>              nx.append(y)
>          if y in "abc":
>              nx.append("2")
>          if y in "def":
>              nx.append("3")
>          if y in "ghi":
>              nx.append("4")
>          if y in "jkl":
>              nx.append("5")
>          if y in "mno":
>              nx.append("6")
>          if y in "pqrs":
>              nx.append("7")
>          if y in "tuv":
>              nx.append("8")
>          if y in "wxyz":
>              nx.append("9")
>      number="".join(str(e) for e in nx)

The list nx already contains strings, so you don't need str here.

>      return (number)

You don't need the parens here either.

> a="1-800-getcharter"
> print (nametonumber(a))#1800 438 2427 837
> a="1-800-leo laporte"
> print (nametonumber(a))
> a="1 800 callaprogrammer"
> print (nametonumber(a))
>




More information about the Python-list mailing list