如何在python中拆分zip列表?

2024-04-29 15:16:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要把这个列表从sqlite3中分离出来

[(107, u'Ella', u'Fitzgerald'), (108, u'Louis', u'Armstrong'), (109, u'Miles', u'Davis'), (110, u'Benny', u'Goodman')]

进入

107, 'Ella', 'Fitzgerald'
108, 'Louis', 'Armstrong'
109, 'Miles', 'Davis'
110, 'Benny', 'Goodman'

怎么得到这个?在

谢谢。在


Tags: 列表sqlite3ellaarmstrongdavislouisgoodmanmiles
2条回答

这样可以将元组分成3个不同的变量:

for tuples in sqlite_list:
    id, name, last_name = tuples
.... #Do what you need

尝试以下操作:

data = [(107, u'Ella', u'Fitzgerald'), (108, u'Louis', u'Armstrong'), (109, u'Miles', u'Davis'), (110, u'Benny', u'Goodman')]

for record in data:
    print '{0:}, {1:}, {2:}'.format(*record)

# or in case you want to get a dictionary

d = dict([(x[0], ', '.join(x[1:])) for x in a])# sure that you can skip merging name and surname here(just replace join with x[1:])
for k, v in d.iteritems():
    print '%s, %s' % (k, v)

>>>107 Ella, Fitzgerald
>>>108 Louis, Armstrong
>>>109 Miles, Davis
>>>110 Benny, Goodman

相关问题 更多 >