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