简体   繁体   English

如何从元组打印项目?

[英]How can I print items from a tuple?

I got the tuple -我得到了元组 -

Result = [
    ('80407', 'about power supply of opertional amplifier', '11 hours ago'),
    ('80405', '5V Regulator Power Dissipation', '11 hours ago')]

I want to iterate over the tuples and separate the items in the tuples by ;我想遍历元组并将元组中的项目分开; . .

The output should be as follows -输出应如下 -

80407;about power supply of opertional amplifier;11 hours ago

I tried the following:我尝试了以下方法:

for item in zip(*Result):
    print(*item[0], end=';')

Which gave me the result -这给了我结果 -

8 0 4 0 7;a b o u t   p o w e r   s u p p l y   o f   o p e r t i o n a l   a m p l i f i e r;1 1   h o u r s   a g o;

and

for item in Result:
    print(*item[0], end=';')

Which gave me -这给了我 -

8 0 4 0 7;8 0 4 0 5;

How to properly iterate over a tuple?如何正确迭代元组?

Join the items in the tuple with a ; 加入元组中的项目; by iterating over each item in the tuple, and join the resultant string in the outer list with another separator, perhaps a newline \\n 迭代元组中的每个项目,并将外部列表中的结果字符串与另一个分隔符连接,也许是换行符\\n

s = '\n'.join(';'.join(item for item in lst) for lst in Result)
print(s)

The output will be 输出将是

80407;about power supply of opertional amplifier;11 hours ago
80405;5V Regulator Power Dissipation;11 hours ago

我用分号加入元组中的每个项目,然后使用换行符加入元组:

print('\n'.join(';'.join(i) for i in Result))

To accomplish this, no zip call is required. 要做到这一点,不需要zip调用。 Instead, your could can look like this: 相反,你可以看起来像这样:

for item in Result:
    print(*item, sep=';')

You were attempting to use end instead of sep . 你试图使用end而不是sep end just adds a semicolon to the end of the line, instead of a newline. end只是在行的末尾添加一个分号,而不是换行符。 sep specifies what seperates the values in a print call. sep指定在打印调用中分隔值的内容。 By default, this is a space ( sep=' ' ) and ( end='\\n' ) 默认情况下,这是一个空格( sep=' ' )和( end='\\n'

use can use %s and tuple position to achieve it. use可以使用%s和tuple位置来实现它。


for i in Result:
    print('%s;%s;%s'% (i[0],i[1],i[2]))

Using single join statement使用单个join语句

result = [
    ('80407', 'about power supply of operational amplifier', '11 hours ago'),
    ('80405', '5V Regulator Power Dissipation', '11 hours ago')]

for item in result:
  print(';'.join(item))

gives

80407;about power supply of operational amplifier;11 hours ago
80405;5V Regulator Power Dissipation;11 hours ago

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将列表中的项目转换为元组? - How can I convert the items in the list into a tuple? 如何使用元组中的项访问字典中的项? - How do I access items of a dictionary using items from a tuple? 为什么我不能从列表中打印单个元组? - why can't I print a single tuple from a list? 如何在列表中附加嵌套元组项? - How can I append nested tuple items in a list? 如果匹配一个元组的整个项目怎么办 - How can I make if in match whole items of a tuple 我如何访问实例变量项以从每个类中打印出唯一的项列表? - How can I access instance variable, items in order to print out a unique list of items from every class? 如何以表格式打印键为元组且值为元组的词典? - how can I print dictionary whose key is tuple and value is tuple in table format? 如何在带有两个元组元素的元组上循环并打印乘以元组元素的格式正确的字符串结果? - How can I loop over a tuple with two tuple elements and print a nice formatted string result of multiplying the tuple elements? 如何使代码从列表中选择一项,将其打印并一次又一次重复该过程? - How can I make the code choose one of the items from the list, print it and repeat the process again and again? 如何在 Python 中将多行中的项目列表打印到一行中? - How can I print list of items from multiple lines into a single line in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM