简体   繁体   English

python元组列表到嵌套列表字典的列表

[英]python list of tuples to list of dictionary of nested lists

I am trying to convert list of tuples (example z below) to z1. 我正在尝试将元组列表(下面的示例z)转换为z1。 First 2 items in z can be same and so becomes common field in result's dictionary. z中的前2个项可以相同,因此成为结果字典中的公共字段。 Please below. 请在下面。 My attempt is shown as well but it fails to group common element? 也显示了我的尝试,但是未能对通用元素进行分组? Any help? 有什么帮助吗?

FROM:

z= [(53, 'example 2', 2, 'instagram', 'nyc'),
    (53, 'example 2', 5, 'instagram', 'detroit'),
    (53, 'example 2', 7, 'twitter', 'harlem'),
    (50, 'example 5', 8, 'twitter', 'harlem'),
    (27, 'example 6', None, None, None), 
   ]

TO:

z1=[
 {'id':        53,
  'name':      'example 2',
  'hashtags':  [ { 'tag_id': 2, 'platform': 'instagram', 'tagname': 'nyc' },
                 { 'tag_id': 5, 'platform': 'instagram', 'tagname': 'detroit' },
                 { 'tag_id': 7, 'platform': 'twitter',   'tagname': 'harlem' },
               ]
 },
 {'id':        50,
  'name':      'example 2',
  'hashtags':  [ { 'tag_id': 8, 'platform': 'twitter', 'tagname': 'harlem' },
               ]
 },
 {'id':        27,
  'name':      'example 6',
  'hashtags':  [ { 'tag_id': None, 'platform': None, 'tagname': None },
               ]
 },
]

My Attempt: 我的尝试:

ld = []
for a, b, c, d, e in z:
    ld.append({ 'id':       a,
                'name':     b,
                'tag_id':   c,
                'hashtags': [{'platform': d, 'hashtag':  e}, ]
             })

print ld

Output: 输出:

[
 {'id':        53,
  'name':      'example 2',
  'hashtags':  [ { 'tag_id': 2, 'platform': 'instagram', 'tagname': 'nyc' }]
 },
 {'id':        53,
  'name':      'example 2',
  'hashtags':  [ { 'tag_id': 5, 'platform': 'instagram', 'tagname': 'detroit' }]
 },
 {'id':        53,
  'name':      'example 2',
  'hashtags':  [ { 'tag_id': 7, 'platform': 'twitter',   'tagname': 'harlem' },]
 },
 {'id':        50,
  'name':      'example 2',
  'hashtags':  [ { 'tag_id': 8, 'platform': 'twitter', 'tagname': 'harlem' },
               ]
 },
 {'id':        27,
  'name':      'example 6',
  'hashtags':  [ { 'tag_id': None, 'platform': None, 'tagname': None },
               ]
 },
]

The problem is that you aren't looking to see if you've already added a dict with the given id to ld ("Have I already added the element with id 53 to the list?"). 问题是您不希望查看是否已经向ld添加了具有给定id的字典(“我是否已将ID为53的元素添加到列表中?”)。 You need to check to see if you've already added it. 您需要检查是否已添加它。

The first thing that comes to mind is storing previous ids in a dict mapping the to the index. 首先想到的是将以前的ID存储在将映射到索引的字典中。 This doesn't increase the runtime complexity. 这不会增加运行时的复杂性。

ld = []
encountered_id_index = {}
for a, b, c, d, e in z:
    if a in encountered_id_index:
        index = encountered_id_index[a]
        ld_dict = ld[index]
        ld_dict['hashtags'].append({'platform': d, 'hashtag': e, 'tag_id': c})
    else:
        ld.append({ 'id': a,
                    'name': b,
                    'hashtags': [{'platform': d, 'hashtag': e, 'tag_id': c}]
        })
        index = len(ld) - 1
        encountered_id_index[a] = index

This is untested, but I think that should get the job done. 这未经测试,但我认为应该可以完成工作。

Unrelated, but I'd recommend changing the variable names in the for loop to something more meaningful. 无关,但我建议将for循环中的变量名称更改为更有意义的名称。 "id" instead of "a", "name" instead of "b", etc. I promise you that if you learn to properly name your variables now, you'll have fewer headaches in the future. “ id”代替“ a”,“ name”代替“ b”,等等。我向您保证,如果您现在学会正确地命名变量,那么将来的麻烦就更少了。 It dramatically increases readability of your code. 它大大提高了代码的可读性。

from collections import defaultdict, namedtuple

HashTag = namedtuple('HashTag', ['tag_id', 'platform', 'tag_name'])

class Entries:
    def __init__(self):
        self.entries = defaultdict(list)

    def add_entry(self, id, name, tag_id, platform, tag_name):
        key = (id, name)
        value = HashTag(tag_id, platform, tag_name)
        self.entries[key].append(value)

z1 = Entries()
for entry in z:
    z1.add_entry(*entry)

... the only thing I don't like about this is that you need to know both the id and name to look up an entry. ...我对此唯一不满意的是,您需要同时知道ID 名称才能查找条目。 If I were using this seriously, I would modify it to index entries only on id, then have a second dict linking name to id, then implement __ getitem __ such that it will do a lookup on either id or name. 如果我认真地使用了它,我将其修改为仅对id索引条目,然后再将名称链接到id的第二个字典,然后实现__ getitem __,以便对id或name进行查找。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM