创建没有重复的列表列表

时间:2014-01-27 19:42:58

标签: python list

我的数据结构有很多重复。如果我要创建一个筛选列表以列出所有唯一类型,我会

type_ids = []
for record in file:
    type_id = record['type_id']
    if type_id not in type_ids:
        type_ids.append(type_ids)

我将获得类似type_ids=['A','B','G']的内容。现在我想要类似于描述性名称的类型以及id,在types = [ ['A','Alpha'], ['B','Beta'], ['G','Gamma'] ]我尝试过的结构中

types = []
for record in file:
    type_id = record['type_id']
    type_name = record['type_name']
    if [type_id,type_name] not in types:
        types.append([type_id,type_name])

我得到一个列表,但有很多重复,而不是所有类型的代表。这段代码有什么问题?

2 个答案:

答案 0 :(得分:1)

types = set((r['type_id'], r['type_name']) for r in file)

Python有一个set类型内置,它是一个无序的元素集合。您可以使用这一行创建一组唯一的(type_id, type_name)元组。

答案 1 :(得分:1)

在原始代码中,将始终执行if语句,这可能会导致大量重复。 type_ids是一个字符串列表;你的if语句正在检查列表的成员资格。 [type_id, type_name]中没有type_ids形式的列表。我不确定您是否正在寻找已经存在的type_ids成员资格或您正在构建的types成员资格。

相反,你想要这样的东西:

types = []
for record in file:
    type_id = record['type_id'] # Assuming these two lines get the data correctly
    type_name = record['type_name']
    if type_id not in type_ids: # e.g. if 'A' in ['A', 'B', 'C']
    # OR, if [type_id, type_name] not in types:
        types.append([type_id], [type_name])

但是,我建议您以dictionary格式存储您的信息,该格式是专门为相关键值对设计的:

types = {}
for record in file:
    type_id = record['type_id']
    type_name = record['type_name']
    if type_id not in type_ids:
    # OR, if type_id not in types:
        types[type_id] = type_name
相关问题