将元组的格式和打印列表作为一行

时间:2022-12-24 21:46:01

I have a list containing tuples that is generated from a database query and it looks something like this.

我有一个包含从数据库查询生成的元组的列表,它看起来像这样。

[(item1, value1), (item2, value2), (item3, value3),...]

The tuple will be mixed length and when I print the output it will look like this.

元组将是混合长度,当我打印输出时,它将看起来像这样。

item1=value1, item2=value2, item3=value3,...

I have looked for a while to try to find a solution and none of the .join() solutions I have found work for this type of situation.

我已经找了一段时间试图找到一个解决方案,但我找不到适用于这种情况的.join()解决方案。

6 个解决方案

#1


9  

You're after something like:

你是这样的:

>>> a = [('val', 1), ('val2', 2), ('val3', 3)]
>>> ', '.join('{}={}'.format(*el) for el in a)
'val=1, val2=2, val3=3'

This also doesn't care what type the tuple elements are... you'll get the str representation of them automatically.

这也不关心元组元素的类型......你将自动获得它们的str表示。

#2


2  

You can use itertools as well

你也可以使用itertools

from itertools import starmap
', '.join(starmap('{}={}'.format, a))

#3


2  

If each tuple is only an (item, value) pair then this should work:

如果每个元组只是一个(item,value)对,那么这应该有效:

l = [(item1, value1), (item2, value2), (item3, value3), ...]
', '.join('='.join(t) for t in l)
'item1=value1, item2=value2, item3=value3, ...'

#4


1  

Try this:

尝试这个:

lst = [('item1', 'value1'), ('item2', 'value2'), ('item3', 'value3')]
print ', '.join(str(x) + '=' + str(y) for x, y in lst)

I'm explicitly converting to string the items and values, if one (or both) are already strings you can remove the corresponding str() conversion:

我明确地转换为字符串项和值,如果一个(或两个)已经是字符串,你可以删除相应的str()转换:

print ', '.join(x + '=' + y for x, y in lst)

#5


0  

If you want something like that, I would use a dictionary.

如果你想要这样的东西,我会用一本字典。

dict = {1:2,3:4}
print dict

Then, you can loop through it like this:

然后,您可以像这样循环:

dict = {1:2,3:3}
print dict

for i in dict:
    print i, "=", dict[i]

Hope it helps!

希望能帮助到你!

#6


0  

One possible solution is this, definitely the shortest code

一种可能的解决方案是,这绝对是最短的代码

>>> a = [('val', 1), ('val2', 2), ('val3', 3)]
>>>', '.join('%s=%s' % v for v in a)
'val=1, val2=2, val3=3'

works with python 2.7 as well

也适用于python 2.7

#1


9  

You're after something like:

你是这样的:

>>> a = [('val', 1), ('val2', 2), ('val3', 3)]
>>> ', '.join('{}={}'.format(*el) for el in a)
'val=1, val2=2, val3=3'

This also doesn't care what type the tuple elements are... you'll get the str representation of them automatically.

这也不关心元组元素的类型......你将自动获得它们的str表示。

#2


2  

You can use itertools as well

你也可以使用itertools

from itertools import starmap
', '.join(starmap('{}={}'.format, a))

#3


2  

If each tuple is only an (item, value) pair then this should work:

如果每个元组只是一个(item,value)对,那么这应该有效:

l = [(item1, value1), (item2, value2), (item3, value3), ...]
', '.join('='.join(t) for t in l)
'item1=value1, item2=value2, item3=value3, ...'

#4


1  

Try this:

尝试这个:

lst = [('item1', 'value1'), ('item2', 'value2'), ('item3', 'value3')]
print ', '.join(str(x) + '=' + str(y) for x, y in lst)

I'm explicitly converting to string the items and values, if one (or both) are already strings you can remove the corresponding str() conversion:

我明确地转换为字符串项和值,如果一个(或两个)已经是字符串,你可以删除相应的str()转换:

print ', '.join(x + '=' + y for x, y in lst)

#5


0  

If you want something like that, I would use a dictionary.

如果你想要这样的东西,我会用一本字典。

dict = {1:2,3:4}
print dict

Then, you can loop through it like this:

然后,您可以像这样循环:

dict = {1:2,3:3}
print dict

for i in dict:
    print i, "=", dict[i]

Hope it helps!

希望能帮助到你!

#6


0  

One possible solution is this, definitely the shortest code

一种可能的解决方案是,这绝对是最短的代码

>>> a = [('val', 1), ('val2', 2), ('val3', 3)]
>>>', '.join('%s=%s' % v for v in a)
'val=1, val2=2, val3=3'

works with python 2.7 as well

也适用于python 2.7