我希望使用python中的列表理解列表中的打印项目。怎么办?
itemlist = [("Tatamotors",483.4568), ("M&M",953.8045),("TVSmotors",712),("AshokLeyland",142.2567)]
print([f"Item {stock} : Price = {price}" for stock,price in itemlist] )
for stock,price in itemlist:
print(f"Item {stock} : Price = {price}")
如何打印列表理解对象,就像循环一样?
我尝试解开列表中的每个项目,但无法做到。
如何解开每个项目并按行格式打印,而不是续订列表。
我删除了列表,Pyhton返回生成器对象如何打印?
print(f"Item {stock} : Price = {price}" for stock,price in itemlist )
输出:<发电机对象在0x7fd0af2bc5d0>
所需的输出:
项目tatamotors:价格= 483.4568
项目M& m:价格= 953.8045
项目TVSMOTORS:PRISE价格= 712
项目Ashokleyland:价格= 142.2567
使用列表理解
itemlist = [("Tatamotors",483.4568), ("M&M",953.8045),("TVSmotors",712),("AshokLeyland",142.2567)]
print([f"Item {stock} : Price = {price}" for stock,price in itemlist] )
for stock,price in itemlist:
print(f"Item {stock} : Price = {price}")
how can i print list comprehension object one by one like for loop does?
I tried unpacking each item in list but couldn't able to do it.
how can unpack each item and print in a line by line format instead of of returing list.
i removed list and pyhton returns generator object how can i print that?
print(f"Item {stock} : Price = {price}" for stock,price in itemlist )
Output:<generator object at 0x7fd0af2bc5d0>
Desired Output:
Item Tatamotors : Price = 483.4568
Item M&M : Price = 953.8045
Item TVSmotors : Price = 712
Item AshokLeyland : Price = 142.2567
Using LIST COMPREHENSION
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输出:
Output:
您可以使用列表理解打印,但我认为它将占据无用的内存,在大规模的环境中,这不是最佳实践。
但是,ya有以下方法,
输出:
You can print using List Comprehension, but I think it'll occupy useless memory, In big scale environment it's not the best practice.
But ya there is the way below,
Output: