gpt4 book ai didi

python - 在字典中查找重复值并仅在具有相同键的值不同时才打印它们

转载 作者:行者123 更新时间:2023-12-01 09:06:43 26 4
gpt4 key购买 nike

我正在从 CSV 文件创建一个三元组字典,其中键 - 行号和值包含三个整数的列表。我还创建了另一个字典(名称),其中键为行号,值为两个字符串的列表。我想找到包含相同三元组的所有行,以防名称对不同。

到目前为止,我的代码正在查找所有重复项,以防两行上存在相同的三元组值,但如果在 3 行及更多行上存在重复项,则它将无法正常工作。我想更新或重写整个脚本,以便在出现 3 个或更多重复的情况下检查所有名称值是否不同并仅打印具有不同名称的行。例如,如果我们有以下三元组字典:三元组 = {1: [111, 222, 333], 2: [111, 222, 333], 3: [111, 222, 333], }names = {1: ['name1', 'name2'], 2: ['name1', 'name2'], 3: ['name1', 'name3']} 这将导致创建另一个字典: duplicd_value_keys = {(111, 222, 333): [1, 2, 3]} 并且我的脚本不会显示重复项,因为 names[1] == names[2]但原则上它应该打印出第 2 行和第 3 行的三元组值具有不同的名称。

for csv_infile in os.listdir(input_dir):
if csv_infile.lower().endswith('.csv'):
csv_in = os.path.join(input_dir, csv_infile)
with open(csv_in) as f_in:
# Creating dictionaries containing as a key the line number and as a value
triplet = {}
names = {}
l_num = 0
for line in f_in:
l_num += 1
triplet[l_num] = [(line.split('\t')[1]), (line.split('\t')[2]), (line.split('\t')[3])]
names[l_num] = [(line.split('\t')[4].lower().strip()), (line.split('\t')[5].lower().strip())]

# Finding the duplicated values and creating a new dictionary with values the line numbers.
duplicated_value_keys = collections.defaultdict(list)
for key, value in triplet.items():
duplicated_value_keys[tuple(value)].append(key)
for duplicated_keys in duplicated_value_keys.values():
if len(duplicated_keys) >1 and names[duplicated_keys[0]] != names[duplicated_keys[1]]:
print("There is a duplicated triplet on lines: {}.\n".format(', '.join(map(str, duplicated_keys))))

[编辑]:CSV 输入文件具有以下格式,并且以制表符分隔:

2       8004    3014    3       test name   1       14080   1       0       3478    1572    0       0
2 8004 3014 3 test name 1 8004 1 0 3478 1572 0 0
3 8004 3014 3 test name1 1 8004 1 0 3477 1571 0 0

最佳答案

可以使用defaultdict(list)来检测重复行。三元组将是字典的键,每个字典将包含找到该三元组的行号和名称的列表。读取所有条目后,迭代字典并仅显示包含不同名称的条目。例如:

import csv
from collections import defaultdict

triplets = defaultdict(list)

with open('test.csv', newline='') as f_input:
csv_input = csv.reader(f_input, delimiter='\t')

for line, row in enumerate(csv_input, start=1):
triplets[tuple(row[1:4])].append((line, list(map(str.lower, row[4:6]))))

for triplet, entries in sorted(triplets.items()):
if len(entries) > 1 and len({tuple(names) for line, names in entries}) > 1:
print("Duplicate triplet: {} on lines:".format(triplet))
for line, names in entries:
print(" {}, {}, {}".format(line, *names))
print()

对于给定的test.csv,这将产生:

Duplicate triplet: ('13115', '3209', '3') on lines:
44, skylink, horor film
69, skylink, private spice

Duplicate triplet: ('13139', '3219', '3') on lines:
8, skylink, nova cinema
13, skylink, prima zoom

Duplicate triplet: ('8004', '3014', '3') on lines:
2, skylink, ct 2
3, skylink, bar 2
4, skylink, tst 22
5, skylink, tst 22

关于python - 在字典中查找重复值并仅在具有相同键的值不同时才打印它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51986297/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com