在Python中对列表中的重复项和其他列表的相同索引进行分组

qpgpyjmq  于 2022-12-28  发布在  Python
关注(0)|答案(1)|浏览(103)

我是Python的新手,我正在处理这个问题,我需要从一堆列表的同一索引中获取项目:

name_list = ["trung", "key", "trung","kayla","john","trung","key"]
id_list  = ["123", "456", "789", "852", "545", "915", "712"]
summary_list = ["aa12", "bb99", "cc66", "dd854", "ee842", "ff723", "gg413"]
version_list = ["a01", "a22", "a23", "a94", "a05", "a42", "a34"]

我想得到所有重复名字的列和它们各自的数据

Name: Trung
id_list = ["123", "789", "915"]
summary_list = ["aa12", "cc66", "ff723"]
version_list = ["a01", "a23", "a42"]

Name: key
id_list = ["456", "712"]
summary_list = ["bb99", "gg413"]
version_list = ["a22", "a34"]

Name: kayla
id_list = ["852"]
summary_list = ["dd854"]
version_list = ["a94"]

Name: john
id_list = ["545"]
summary_list = ["ee842"]
version_list = ["a05"]

我尝试的是:

for i in range(len(name_list)):
for name in name_list:
    if(name == name_list[i]):
        print("Hi ", name, "your id is: ", id_list[i], ". Part number: ", summary_list[i], ". And version: ", version_list[i])

但是打印出来的线太多了

Hi  trung your id is:  123 . Part number:  aa12 . And version:  a01 

Hi  trung your id is:  123 . Part number:  aa12 . And version:  a01 

Hi  trung your id is:  123 . Part number:  aa12 . And version:  a01 

Hi  key your id is:  456 . Part number:  bb99 . And version:  a22 

Hi  key your id is:  456 . Part number:  bb99 . And version:  a22 

Hi  trung your id is:  789 . Part number:  cc66 . And version:  a23 

Hi  trung your id is:  789 . Part number:  cc66 . And version:  a23 

Hi  trung your id is:  789 . Part number:  cc66 . And version:  a23 

Hi  kayla your id is:  852 . Part number:  dd854 . And version:  a94 

Hi  john your id is:  545 . Part number:  ee842 . And version:  a05 

Hi  trung your id is:  915 . Part number:  ff723 . And version:  a42 

Hi  trung your id is:  915 . Part number:  ff723 . And version:  a42 

Hi  trung your id is:  915 . Part number:  ff723 . And version:  a42 

Hi  key your id is:  712 . Part number:  gg413 . And version:  a34 

Hi  key your id is:  712 . Part number:  gg413 . And version:  a34

另一种方法,我尝试了,但它只返回与所有的一个是重复超过1次

id = []
for i in range(len(name_list)):
    if(name_list.count(name_list[i]) > 1):
        print(name_list[i], id_list[i], summary_list[i], version_list[i])
        id.append(id_list[i])
print(id)

结果

trung 123 aa12 a01
key 456 bb99 a22
trung 789 cc66 a23
trung 915 ff723 a42
key 712 gg413 a34
['123', '456', '789', '915', '712']

我如何让脚本理解和分离列表?谢谢你的阅读和帮助。

xpszyzbs

xpszyzbs1#

尝试使用collection.defaultdict并迭代name_list,为其他列表中的每个对应条目添加条目,如下所示:

import collections

name_list = ["trung", "key", "trung","kayla","john","trung","key"]
id_list  = ["123", "456", "789", "852", "545", "915", "712"]
summary_list = ["aa12", "bb99", "cc66", "dd854", "ee842", "ff723", "gg413"]
version_list = ["a01", "a22", "a23", "a94", "a05", "a42", "a34"]

# Group by name
dct = collections.defaultdict(lambda: {'id': [], 'summary': [], 'version': []})
for i, name in enumerate(name_list):
    dct[name]['id'].append(id_list[i])
    dct[name]['summary'].append(summary_list[i])
    dct[name]['version'].append(version_list[i])

# Print it out
for name in dct:
    print(f'Name: {name}')
    print(f'id_list = {dct[name]["id"]}')
    print(f'summary_list = {dct[name]["summary"]}')
    print(f'version_list = {dct[name]["version"]}')
    print()

相关问题