Ale*_*x_P 4 python iteration tuples python-3.x
I got the tuple -
Result = [('80407', 'about power supply of opertional amplifier', '11 hours ago'), ('80405', '5V Regulator Power Dissipation', '11 hours ago')]`
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)
I tried the following:
for item in zip(*Result):
print(*item[0], end=';')
Run Code Online (Sandbox Code Playgroud)
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;`
Run Code Online (Sandbox Code Playgroud)
and
for item in Result:
print(*item[0], end=';')
Run Code Online (Sandbox Code Playgroud)
Which gave me -
8 0 4 0 7;8 0 4 0 5;
Run Code Online (Sandbox Code Playgroud)
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
s = '\n'.join(';'.join(item for item in lst) for lst in Result)
print(s)
Run Code Online (Sandbox Code Playgroud)
The output will be
80407;about power supply of opertional amplifier;11 hours ago
80405;5V Regulator Power Dissipation;11 hours ago
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
80 次 |
| 最近记录: |