具有dict键值的元组的Python列表

时间:2015-08-12 04:45:31

标签: python list dictionary

我很难尝试将带有键值的元组列表转换为字典。

我有一个元组列表:

[('season', '1', 'episode', '1', 'showkey', '1736'),
('season', '1', 'episode', '2', 'showkey', '1737'),
('season', '1', 'episode', '3', 'showkey', '1738'),
('season', '1', 'episode', '4', 'showkey', '1739'),
('season', '1', 'episode', '5', 'showkey', '1740'),
('season', '1', 'episode', '6', 'showkey', '1741'),
('season', '1', 'episode', '7', 'showkey', '1742'),
('season', '1', 'episode', '8', 'showkey', '1743'),
('season', '1', 'episode', '9', 'showkey', '1744'),
('season', '1', 'episode', '10', 'showkey', '1745'),
('season', '1', 'episode', '11', 'showkey', '1746'),
('season', '1', 'episode', '12', 'showkey', '1747'),
('season', '1', 'episode', '13', 'showkey', '1748')]

如何使用键值对创建字典,如:

{'season':1, 'episode':2, 'showkey':1736}

感谢您的帮助!

5 个答案:

答案 0 :(得分:7)

如果你想为每个元组使用一个字典:

>>> i = ('season', '1', 'episode', '2', 'showkey', '1737')
>>> dict(zip(i[::2], i[1::2]))
{'season': '1', 'episode': '2', 'showkey': '1737'}

要为您的列表展开:

new_list = [dict(zip(i[::2], i[1::2])) for i in list_of_data]

这结合了以下事实:

  1. dict()构造函数采用可迭代的对,它可以转换为键/值。
  2. zip()将从传递给它的两个迭代中返回元组对。
  3. 切片允许您添加起始偏移量和步骤参数。
  4. 结合以上内容:

    1. 首先,我们通过跳过第一个元素i[::2],通过奇数位置i[1::2]然后通过偶数位置对元组进行切片,这给出了两个表示键的列表,第二个表示值:

      >>> i[::2]
      ('season', 'episode', 'showkey')
      >>> i[1::2]
      ('1', '2', '1737')
      
    2. 接下来,我们将这两个列表提供给zip以给我们(键,值)对:

      >>> zip(i[::2], i[1::2])
      [('season', '1'), ('episode', '2'), ('showkey', '1737')]
      
    3. 最后,我们将结果列表传递给dict()构造函数:

      >>> dict([('season', '1'), ('episode', '2'), ('showkey', '1737')])
      {'season': '1', 'episode': '2', 'showkey': '1737'}
      

答案 1 :(得分:1)

我会使用列表推导,并通过在其中创建每个连续2个元素来创建元组。

代码 -

d = [dict((i[j],i[j+1]) for j in range(0,len(i),2)) for i in lt]

此处lt是元组列表。

示例 -

>>> lt = [('season', '1', 'episode', '1', 'showkey', '1736'),
... ('season', '1', 'episode', '2', 'showkey', '1737'),
... ('season', '1', 'episode', '3', 'showkey', '1738'),
... ('season', '1', 'episode', '4', 'showkey', '1739'),
... ('season', '1', 'episode', '5', 'showkey', '1740'),
... ('season', '1', 'episode', '6', 'showkey', '1741'),
... ('season', '1', 'episode', '7', 'showkey', '1742'),
... ('season', '1', 'episode', '8', 'showkey', '1743'),
... ('season', '1', 'episode', '9', 'showkey', '1744'),
... ('season', '1', 'episode', '10', 'showkey', '1745'),
... ('season', '1', 'episode', '11', 'showkey', '1746'),
... ('season', '1', 'episode', '12', 'showkey', '1747'),
... ('season', '1', 'episode', '13', 'showkey', '1748')]
>>>
>>> d= [dict((i[j],i[j+1]) for j in range(0,len(i),2)) for i in lt]
>>> d
[{'showkey': '1736', 'episode': '1', 'season': '1'}, {'showkey': '1737', 'episode': '2', 'season': '1'}, {'showkey': '1738', 'episode': '3', 'season': '1'}, {'showkey': '1739', 'episode': '4', 'season': '1'}, {'showkey': '1740', 'episode':
 'season': '1'}, {'showkey': '1744', 'episode': '9', 'season': '1'}, {'showkey': '1745', 'episode': '10', 'season': '1'}, {'showkey': '1746', 'episode': '11', 'season': '1'}, {'showkey': '1747', 'episode': '12', 'season': '1'}, {'showkey':

答案 2 :(得分:0)

results = []
for datum in data:
    # pick off every alternate item from the tuple, starting with the zeroth.
    keys = datum[::2]
    # pick off every alternate item from the tuple, starting with the oneth.
    values = datum[1::2]
    kv = zip(keys, values)
    new_dict = dict(kv)
    results.append(new_dict)

在慢动作中:

    yum install epel-release
    yum install nodejs

请原谅错别字,请在我的手机上输入。

答案 3 :(得分:0)

x=[('season', '1', 'episode', '1', 'showkey', '1736'),
('season', '1', 'episode', '2', 'showkey', '1737'),
('season', '1', 'episode', '3', 'showkey', '1738'),
('season', '1', 'episode', '4', 'showkey', '1739'),
 ('season', '1', 'episode', '5', 'showkey', '1740'),
  ('season', '1', 'episode','6', 'showkey', '1741'),
 ('season', '1', 'episode', '7', 'showkey', '1742'),
('season', '1', 'episode', '8', 'showkey', '1743'),
('season', '1', 'episode', '9', 'showkey', '1744'),
('season', '1', 'episode', '10', 'showkey', '1745'),
('season', '1', 'episode', '11', 'showkey', '1746'),
('season', '1', 'episode', '12', 'showkey', '1747'),
('season', '1', 'episode', '13', 'showkey', '1748')]
d={}
m=0
for i in x:
    d1={}
    for j in i[::2]:

        d1[j]=i[i.index(j)+1]

    d[m]=d1
    m=m+1

print d

您可以创建字典字典以保存所有数据。

答案 4 :(得分:0)

inputd = [('season', '1', 'episode', '1', 'showkey', '1736'),
('season', '1', 'episode', '2', 'showkey', '1737'),
('season', '1', 'episode', '3', 'showkey', '1738'),
('season', '1', 'episode', '4', 'showkey', '1739'),
('season', '1', 'episode', '5', 'showkey', '1740'),
('season', '1', 'episode', '6', 'showkey', '1741'),
('season', '1', 'episode', '7', 'showkey', '1742'),
('season', '1', 'episode', '8', 'showkey', '1743'),
('season', '1', 'episode', '9', 'showkey', '1744'),
('season', '1', 'episode', '10', 'showkey', '1745'),
('season', '1', 'episode', '11', 'showkey', '1746'),
('season', '1', 'episode', '12', 'showkey', '1747'),
('season', '1', 'episode', '13', 'showkey', '1748')]


>>> map(dict, map(lambda x: zip(x[::2],x[1::2]), inputd))
[{'season': '1', 'episode': '1', 'showkey': '1736'}, {'season': '1', 'episode': '2', 'showkey': '1737'}, {'season': '1', 'episode': '3', 'showkey': '1738'}, {'season': '1', 'episode': '4', 'showkey': '1739'}, {'season': '1', 'episode': '5', 'showkey': '1740'}, {'season': '1', 'episode': '6', 'showkey': '1741'}, {'season': '1', 'episode': '7', 'showkey': '1742'}, {'season': '1', 'episode': '8', 'showkey': '1743'}, {'season': '1', 'episode': '9', 'showkey': '1744'}, {'season': '1', 'episode': '10', 'showkey': '1745'}, {'season': '1', 'episode': '11', 'showkey': '1746'}, {'season': '1', 'episode': '12', 'showkey': '1747'}, {'season': '1', 'episode': '13', 'showkey': '1748'}]

关于python slice

  1. x[::2] - 表示从头到尾,每个跳过2个位置 时间
  2. x[1::2] - 表示从索引1到结尾开始,向前跳过2 每次放置