简体   繁体   中英

Trying to print the first element of a nested list using list comprehension

I have done my research on the web, even looked at solution for this particular problem from HackerRank but since it is supposed to be an exercise with list comprehension, I keep to it.

I keep getting <generator object <genexpr> at 0x04246760> for the following code:

mainList = []

for _ in range(int(input())):
    name = input()
    score = float(input())

mainList.append([name,score])

mainList.sort(key = lambda x: [x[1],x[0]])
mainList = list(filter(lambda x: x[1] != mainList[0][1], mainList))

print([y for y,z in x if z == mainList[0][1]] for x in mainList)
print(y for x in mainList for y,z in x if z == mainList[0][1])        

Using debug in VSCode, the content of mainList is exactly what I am expecting but the print part errors out. Both print(...) are my attempt. I just need to know why they don't work.

Here is the input data:

5
Harry
37.21
Berry
37.21
Tina
37.2
Akriti
41
Harsh
39
mainList = []

for _ in range(int(input())):
    name = input()
    score = float(input())

    mainList.append([name,score])

mainList.sort(key = lambda x: [x[1],x[0]])
mainList = list(filter(lambda x: x[1] != mainList[0][1], mainList))

print(*[x[0] for x in mainList if x[1] == mainList[0][1]], sep = "\n")

Many thanks to Barmar

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