Python - 迭代字典匹配

时间:2017-10-25 18:27:42

标签: python dictionary

我有一个'字典'文本文件,格式如下:

1 CH a
2 H1 a
3 H2 a
4 H3 b
5 CO b
6 HA b
...

我有一个如下所示的输入文件:

D1 2 1 3 4  78.5
D2 2 3 4 5 100.2
D3 2 1 5 6  35.2
...

我想要获得的输出是:

a H1 a CH a H2 b H3  78.5
a H1 a H2 b H3 b CO 100.2
a H1 a CH b CO b HA  35.2

我正在尝试将字典文件拆分为列,将第一列指定为键,将第二列和第三列指定为值。然后我按列读取输入文件,并将列[1:4]中的数字与先前生成的键匹配,并打印相应的值。对于输入文件中的最后一列,我创建了另一个与标识符匹配的键/值对,例如key=4, value=78.5

file = open("dictionary.txt", "r")
one_dict = dict()
two_dict = dict()
for line in file:
  fields = line.split(" ")
number = fields[0]
name = fields[1]
letter = fields[2]
one_dict[int(number)] = [letter, name]

with open('input.txt') as infile:
  for line in infile:
  fields = line.split("\t")
value = fields[6]
col_4 = fields[4]
two_dict[int(col_4)] = [value]
for x in fields[1: 3]:
    if x in one_dict:
        output[x] = one_dict[x]
    for col_4 in fields:
        if x in one_dict:
           col_4 = one_dict[x[value]]
           print(output[x], output4[x], two_dict[col_4])

我的脚本不会返回任何内容,我不知道为什么。

1 个答案:

答案 0 :(得分:1)

你过度设计了这个。只需创建一个映射器,即字典,然后映射到中间列。所以,使用假输入数据:

In [34]: s1 = """1 CH a
    ...: 2 H1 a
    ...: 3 H2 a
    ...: 4 H3 b
    ...: 5 CO b
    ...: 6 HA b"""

In [35]: s2 = """D1 2 1 3 4  78.5
    ...: D2 2 3 4 5 100.2
    ...: D3 2 1 5 6  35.2"""

In [36]: import io

所以,创建映射器:

In [37]: with io.StringIO(s1) as f: # pretend string is a file
    ...:     mapper = {}
    ...:     for line in f:
    ...:         key, _ , value = line.partition(' ')
    ...:         mapper[key] = value.strip()
    ...:

In [38]: mapper
Out[38]: {'1': 'CH a', '2': 'H1 a', '3': 'H2 a', '4': 'H3 b', '5': 'CO b', '6': 'HA b'}

然后,快点肮脏:

In [40]: buffer = io.StringIO() # fake output file
    ...: with io.StringIO(s2) as infile, buffer as outfile:
    ...:     for line in infile:
    ...:         _, *data, last = line.split()
    ...:         outfile.write(' '.join(map(mapper.get, data)))
    ...:         outfile.write(" " + last + "\n")
    ...:     result = buffer.getvalue() # get the value before we "close" the fake file
    ...:
    ...:

In [41]: print(result)
H1 a CH a H2 a H3 b 78.5
H1 a H2 a H3 b CO b 100.2
H1 a CH a CO b HA b 35.2