简体   繁体   中英

python basic for loop/list comprehension

Say I have:

list=[('mike', 22.0), ('john', 33.0)]

I want to print a list of just the numbers, smallest to largest. I tried doing this:

for s in list:
    print(sorted(s[1]))

But it said 'int object is not iterable'?

If i do this, it works ( output is: [22,33])

print (sorted(s[1] for s in list))

How does the 2nd one work and what's the difference between the 1st and 2nd attempt? Hard time understanding the list comprehension technique.

BTW, the final goal is to print the NAME of the person will the lowest mark. But I'm trying to understand this part first before I associate the name with the mark. Dict would make it so much easier to assign key-values though

When you do:

for s in list:
    print(sorted(s[1]))

You're trying to sort an int . The sorted function requires an iterable (for example, a list). Hence your error. This is noticeable since:

for s in list:
    print(s[1])

prints:

22.0
33.0

On the other hand, when you do: Sorted(s[1] for s in myList) , you're sorting a list (actually you're sorting a generator, but there's a somewhat fine line). Try:

newList = [s[1] for s in myList]
newList

and you'll see that you're essentially sorting a list. Sorting a list makes sense, but sorting a single number doesn't. Sorted is a function that returns a sorted list. To illustrate the difference between generators and iterables (like lists, tuples and dicts), compare the last snippet to the below:

newGenerator = (s[1] for s in myList)
print(newGenerator) #probably won't give what you ever need
for item in newGenerator:
    print item  #this will iterate through the items in the generator and print them

I had assumed you wanted the group sorted, but someone else answered that already and got downvoted so I didn't add it. Here is how you would do that:

myList.sort(key = lambda x: x[1])

Then:

myList[0][0]

Will be the name of the lowest person.

In this case, sort is a method, which operates on myList and changes it. If you pass it the key = lambda x: x[1] argument, it will sort the list based on the position 1 . So, if your number was in a different position in your tuples you would use a different number there. lambda is just a way to make a function on the fly, in this case the function just grabs the 1 element (second element).

Because a list comprehension will give a generator here, see:

>>> Mylist=[('john', 33.0),('mike', 22.0)]
>>> print(s[1] for s in Mylist)
<generator object <genexpr> at 0x7fd4a005e0f8>

And however, sorted() can be use on an iterable like list or generator , and then convert it to a list.

For example, range() will give a generator on Python 3(if you're using Python 2, use xrange() instead):

>>> a = range(20)
>>> sorted(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

And about why does this list comprehension give a generator , I think you forgot [] :

>>> Mylist=[('john', 33.0),('mike', 22.0)]
>>> print(sorted([s[1] for s in Mylist]))
[22.0, 33.0]
>>> print(sorted(s[1] for s in Mylist))
[22.0, 33.0]
>>> print([s[1] for s in Mylist])
[33.0, 22.0]
>>> print(s[1] for s in Mylist)
<generator object <genexpr> at 0x7f8ab881c6d0>
>>> 

By the way, about your code without list comprehension:

>>> Mylist=[('john', 33.0),('mike', 22.0)]
>>> l = []

>>> for s in sorted(Mylist, key=lambda x:x[1]):
...     l.append(s[1])

>>> print(l)
[22.0, 33.0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM