简体   繁体   English

我想使用python中的列表理解一一打印列表中的项目。 怎么能这样做?

[英]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.

相关问题 如何确定一个列表是否包含另一个列表中的项目,然后在 Python 中打印它们的索引 - how do I determine if one list has items contained in another list, then print their index in Python 如何一一打印 Python 列表中的元素? - How do I print elements in a Python list one by one? 我该如何使用列表理解功能? - How can i do this using list comprehension? 在Python中使用Tkinter。 如何一次将一个列表变量调用到画布中 - With Tkinter in Python. How do I call a list variable into a canvas one at a time 在嵌套列表推导中仅使用一个列表中的项目 - Only using items from one list once in nested list comprehension 我想在 python 的同一行中打印和输入。 我该怎么做? - I want to print and input in the same line in python. How can I do it? Python:如何使用列表理解来表示这种特殊的for循环? - Python: How would one represent this particular for loop using list comprehension? 如何在Python中使用list comprehension从一个元素中获取多个元素? - How to get multiple element from one using list comprehension in Python? 如何在python中将一个列表中的项匹配到另一个列表? - How can I match items from one list to the other in python? 高级列表理解:如何在一个列表理解中适合多个 for 循环? - Advanced List Comprehension: how can I fit multiple for loops in one list comprehension?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM