将对象列表转换为整数列表和查找选项卡

2024-05-19 16:26:47 发布

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

为了说明我的意思,这里有一个例子

messages = [
  ('Ricky',  'Steve',  'SMS'),
  ('Steve',  'Karl',   'SMS'),
  ('Karl',   'Nora',   'Email')
]

我想把这个列表和组的定义转换成一个整数列表和一个查找字典,这样组中的每个元素都有一个唯一的id

^{pr2}$

我想知道是否有一个优雅和Python式的解决办法来解决这个问题。在

我也乐于接受比create_lookup_list等更好的术语


Tags: id元素列表字典定义email整数karl
3条回答

在Otto的答案(或其他任何使用string->;id dicts的人)中,我将替换(如果您喜欢超速):

# create the lookup table
lookup_dict = {}
for group in indices:
    lookup_dict[group] = sorted(indices[group].keys(),
            lambda e1, e2: indices[group][e1]-indices[group][e2])

通过

^{pr2}$

这是更好的,因为直接分配给逆数组中的每个项比排序快。在

我的长度和复杂性差不多:

import collections

def create_lookup_list(messages, labels):

    # Collect all the values
    lookup = collections.defaultdict(set)
    for msg in messages:
        for l, v in zip(labels, msg):
            lookup[l].add(v)

    # Make the value sets lists
    for k, v in lookup.items():
        lookup[k] = list(v)

    # Make the lookup_list
    lookup_list = []
    for msg in messages:
        lookup_list.append([lookup[l].index(v) for l, v in zip(labels, msg)])

    return lookup_list, lookup

defaultdictitertools.count().next方法相结合是将标识符分配给唯一项的好方法。下面是一个如何在您的案例中应用的示例:

from itertools import count
from collections import defaultdict

def create_lookup_list(data, domains):
    domain_keys = defaultdict(lambda:defaultdict(count().next))
    out = []
    for row in data:
        out.append(tuple(domain_keys[dom][val] for val, dom in zip(row, domains)))
    lookup_table = dict((k, sorted(d, key=d.get)) for k, d in domain_keys.items())
    return out, lookup_table

编辑:注意在python3中,count().next变成了count().__next__或{}。在

相关问题 更多 >