我希望使用python中的列表理解列表中的打印项目。怎么办?

发布于 2025-02-02 03:52:50 字数 727 浏览 3 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

ㄖ落Θ余辉 2025-02-09 03:52:50
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]))  

输出:

Item Tatamotors : Price = 483.4568
Item M&M : Price = 953.8045
Item TVSmotors : Price = 712
Item AshokLeyland : Price = 142.2567
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
野稚 2025-02-09 03:52:50

您可以使用列表理解打印,但我认为它将占据无用的内存,在大规模的环境中,这不是最佳实践。
但是,ya有以下方法,

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] 

输出:

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.
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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文