简体   繁体   中英

Python FOR Loop in a list in a print function

I have another question about somewhat the same problem as I had here -> Python double FOR loops without threading

Basically, I want the first for loop down here to enumerate over every school subject in a list, and then inside the print function, I want it to print every number that is in that list, so It comes out like 8 - 4 - 5 - 7 etc.

    for item in store:
        print(str(item + "|" + (str((cflist[i]) for i in range(3))) + "\n" + ("-------------" * 7)))
...
Running the complete code makes a big grid filled with these things, but different
subjects each line. (NTL here)
-------------------------------------------------------------------------------------------
NTL|<generator object <genexpr> at 0x0000000003404900>
-------------------------------------------------------------------------------------------

I have had the code working partially, so that each list number was in another subject line, so I would have NTL|8.2 and the next line ENT|5.7 But I want all those numbers to display after each other like NTL|8.2 - 5.7 - etc

EDIT: Done!

-------------------------------------------------------------------------------------------
NTL|7.2 8.4 6.7 5.3 4.8
-------------------------------------------------------------------------------------------

Using str on a generator expression will print str version of generator expression, not it's items as you expected:

>>> str((x for x in range(3)))
'<generator object <genexpr> at 0xb624b93c>'

Try something like this:

for item in store:
   strs = " ".join([cflist[i]) for i in range(3)])
   print(str(item + "|" + strs + "\n" + ("-------------" * 7)))

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