Python FOR循环打印函数中的列表 [英] Python FOR Loop in a list in a print function

查看:222
本文介绍了Python FOR循环打印函数中的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我还有一个问题,与我在这里有一样的问题 - >
Python double FOR循环无线程



基本上,我希望第一个循环在这里列举列表中的每个学科,然后在里面打印功能,我希望打印每一个在该列表中的数字,所以它出现像8 - 4 - 5 - 7等。

 < (3)))+\ n+(str((cflist [i]))($ {code> -------------* 7)))
...
运行完整的代码使得大网格充满了这些东西,但不同的
科目每一行。 (NTL here)
------------------------------------------ -------------------------------------------------
NTL |< generator object< genexpr>在0x0000000003404900>
---------------------------------------------- ---------------------------------------------

我有部分代码工作,所以每个列表编号都在另一个主题行中,所以我会有 NTL | 8.2 和下一行 ENT | 5.7 但是我希望所有这些数字都像 NTL | 8.2 - 5.7 - 等等



完成!

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


解决方案

生成器表达式中的code> str 将会打印 str 版本的生成器表达式,而不是像您期望的那样:

 >>> str((x对于范围(3)中的x))
'<发生器对象< genexpr>在0xb624b93c>'

尝试如下所示:

($ {pre $
print(str) (item +|+ strs +\\\
+(-------------* 7)))


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)))

这篇关于Python FOR循环打印函数中的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆