[英]I want print items in list one by one using list comprehension in python. How can do that?
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?我怎样才能像 for 循环一样一一打印列表理解对象?
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?我删除了列表,pyhton 返回生成器对象我该如何打印?
print(f"Item {stock} : Price = {price}" for stock,price in itemlist )
Output : <generator object at 0x7fd0af2bc5d0>输出: <0x7fd0af2bc5d0 处的生成器对象>
Desired Output :期望的输出:
Item Tatamotors : Price = 483.4568项目 Tatamotors:价格 = 483.4568
Item M&M : Price = 953.8045 M&M 项目:价格 = 953.8045
Item TVSmotors : Price = 712项目 TVSmotors : 价格 = 712
Item AshokLeyland : Price = 142.2567物品 AshokLeyland : 价格 = 142.2567
Using LIST COMPREHENSION使用列表理解
itemlist = [("Tatamotors",483.4568), ("M&M",953.8045),("TVSmotors",712),("AshokLeyland",142.2567)]
print('\n'.join([f"Item {stock} : Price = {price}" for stock,price in itemlist]))
Output:输出:
Item Tatamotors : Price = 483.4568
Item M&M : Price = 953.8045
Item TVSmotors : Price = 712
Item AshokLeyland : Price = 142.2567
You can print using List Comprehension, but I think it'll occupy useless memory, In big scale environment it's not the best practice.您可以使用 List Comprehension 进行打印,但我认为它会占用无用的内存,在大规模环境中这不是最佳做法。 But ya there is the way below,
但是你有下面的方法,
itemlist = [("Tatamotors",483.4568), ("M&M",953.8045),("TVSmotors",712),("AshokLeyland",142.2567)]
# Dummy list, which is no use other that printing
[print(f"Item {stock} : Price = {price}") for stock,price in itemlist]
Output:输出:
Item Tatamotors : Price = 483.4568
Item M&M : Price = 953.8045
Item TVSmotors : Price = 712
Item AshokLeyland : Price = 142.2567
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.