I am out of trial and error again Lists

Seymore4Head Seymore4Head at Hotmail.invalid
Thu Oct 23 22:44:01 CEST 2014


On Thu, 23 Oct 2014 13:39:06 -0600, Ian Kelly <ian.g.kelly at gmail.com>
wrote:

>On Thu, Oct 23, 2014 at 11:07 AM, Seymore4Head
><Seymore4Head at hotmail.invalid> wrote:
>> BTW I forgot to add that example 2 and 3 don't seem to be too useful
>> in Python 3, but they are in Python 2.  I don't understand how the
>> Python 3 is an improved version.
>
>In Python 2, range returns a list containing all the requested
>elements. This is simple to work with but often not desirable, because
>it forces the entire sequence to be loaded into memory at once. Large
>ranges may cause excessive memory use and related slowdowns, while
>very large ranges may not even be usable.
>
>In Python 3, range returns a compact object that knows what elements
>it contains and can iterate over them (which is the most common use of
>ranges by far), but without needing to load them all into memory at
>once. While iterating, only the current element needs to exist in
>memory. You can still get the range as a list if you want it, by
>calling list(range(10)), but it doesn't force you to create a list,
>which makes it more versatile than the Python 2 construct.
>
>The more specialized data structure used in Python 3 also allows for
>certain optimizations, for example membership testing. In Python 2, if
>you do the test "97 in range(100)", it has to construct a 100-element
>list and then iterate over 97 of the elements before discovering that
>the list does in fact contain the number 97. The Python 3 range object
>knows what the bounds of the range are, so all it has to do is check
>that 0 <= 97 < 100 to know that the number 97 is included in the
>range.

I tried to make range(10) work in Python 3 by:
        if int(y) in range(10):
            name.append(str(y))

It doesn't.

def nametonumber(name):
    lst=[]
    for y in (name):
        y.lower()
        if int(y) in range(10):
            name.append(str(y))
        if y in " -()":
            name.append(y)
        if y in "abc":
            name.append("2")
        if y in "def":
            name.append("3")
        if y in "ghi":
            name.append("4")
        if y in "jkl":
            name.append("5")           
        if y in "mno":
            name.append("6")
        if y in "pqrs":
            name.append("7")
        if y in "tuv":
            name.append("8")
        if y in "wxyz":
            name.append("9")
    number="".join(str(e) for e in name)
    return (number)
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