Dictionary打印相同的值名称

  • 本文关键字:打印 Dictionary python
  • 更新时间 :
  • 英文 :


我有一个字典,里面有3个键,每个键有多个值。

import os
import glob
import re 
import itertools 
import pandas as pd
from collections import defaultdict 
path = 'C://Users/123/Desktop/files'
list_inst = []
main_inst = [' INST1', ' INST2', ' INST3']
common_inst = []
dictionary = defaultdict(list)
regex = re.compile('(?i)inst(s+w+)')
inst_list = []
files_name = []
for filename in os.listdir(path):
if filename.endswith('.txt'):
with open(os.path.join(path,filename),"r") as f:
for line in f:
match=(re.findall(regex,line))
if match: 
inst_list=inst_list + match 
#print(inst_list)
file_list = []
file_ex = os.path.splitext(filename)[0]
file_list.append(file_ex)
for m in main_inst:
if m in inst_list:
dictionary[m]=file_list
inst_list=[]
print(dictionary)

输出为:

defaultdict(<class 'list'>, {' INST1': ['XYZ', 'XYZ'], ' INST2': ['ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC', 'ABC']})

我希望输出为:

defaultdict(<class 'list'>, {' INST1': ['XYZ', 'MNO'], ' INST2': ['ABC', 'DJS', 'WEF', 'VSD', 'GDS',....]})

更改打印位置没有帮助。我认为这里的词典创建方式有问题。我对所有这些概念都很陌生,任何帮助都将不胜感激。

减少后

import os
import re 
from collections import defaultdict 
path = 'C://Users/123/Desktop/files'
#path = '.'
main_inst = [' INST1', ' INST2', ' INST3']
main_inst_set = set(main_inst)
dictionary = defaultdict(list)
regex = re.compile('(?i)inst(s+w+)')
#regex = re.compile(r'(?i)insts*( INST[123])b')
for filename in sorted(os.listdir(path)):  # filenames in alphabetical order
if filename.endswith('.txt'):
with open(os.path.join(path, filename)) as f:
for line in f:
match = re.findall(regex, line)
match = set(match) & main_inst_set
#print(match, line.strip())
if match: 
file_ex = filename[:-4] # because `.txt` has 4 chars
for m in match:
dictionary[m].append(file_ex)
#file_ex = os.path.splitext(filename)[0]
#for m in match:
#    if m in main_inst:
#        dictionary[m].append(file_ex)
print(dictionary)
print('---')
for key, value in dictionary.items():
print(key, value)

ABC.txt

inst INST1
inst INST2
inst INST3
inst INST15

MNO.txt

inst INST1
inst INST3

XYZ.txt

inst INST2
inst INST3
inst INST4

结果

defaultdict(<class 'list'>, {' INST1': ['ABC', 'MNO'], ' INST2': ['ABC', 'XYZ'], ' INST3': ['ABC', 'MNO', 'XYZ']})
---
INST1 ['ABC', 'MNO']
INST2 ['ABC', 'XYZ']
INST3 ['ABC', 'MNO', 'XYZ']

最新更新