简体   繁体   中英

Can I write this as a two-level list comprehension?

I've got this bit:

for line in entireResult.split('\n'):
    print line.split(',')[0]

... and I feel like it can be list-comprehended, but I'm a bit too Friday-nighted. I tried something like:

[for l[0] in line.split(',') for line in entireResult('\n')]

but that didn't fly (line not defined). Is there a way to do this? Super-extra karma for answering before I finish coding my script with the boring loop codez.

You only have one loop.

[line.split(',')[0] for line in entireResult.split('\n')]

Of course, you should do it as a generator if possible.

print '\n'.join(line.split(',')[0] for line in entireResult.split('\n'))

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