【发布时间】:2018-08-23 12:52:26
【问题描述】:
我正在从 CSV 文件创建一个三元组字典,其中键 - 行号和值是一个包含三个整数的列表。我还在创建另一个字典(名称),其中键为行号,值为两个字符串的列表。我想找到包含相同三元组的所有行,以防名称对不同。
到目前为止,我的代码是查找所有重复项,以防两行存在相同的三元组值,但如果存在 3 行或更多行的重复,它将无法正常工作。我想更新或重新编写整个脚本,以便在 3 个或更多重复的情况下检查所有名称值是否不同并仅打印具有不同名称的行。例如,如果我们有以下三元组字典:
triplet = {1: [111, 222, 333], 2: [111, 222, 333], 3: [111, 222, 333], } 和 names = {1: ['name1', 'name2'], 2: ['name1', 'name2'], 3: ['name1', 'name3']} 这将导致创建另一个字典:duplicated_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
【问题讨论】:
-
目前还不清楚不同名称的含义。与第一个条目不同,每个条目都不同?如果与第一个条目不同,则只显示最后一个条目。
-
@MartinEvans,首先感谢您的帮助。使用不同的名称,我的意思是只有当第 4 列和第 5 列中的条目不同时,脚本才应显示重复的第 1、2、3 列的行。例如,在我的 CSV 示例中,应该只报告第 2 行和第 3 行,因为第 1 行和第 2 行的第 4 列和第 5 列是相同的。
标签: python python-3.x csv dictionary