Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Iterative dictionary matching

I have a 'dictionary' text file with the following format:

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

I have an input file that looks like this:

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

The output I'm trying to obtain is this:

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

I am attempting to split the dictionary file into columns assigning the first column as the key and the second and third columns as values. Then I read the input file by columns and match the number in columns[1:4] with the previously generated keys and print the corresponding values. For the last column in the input file I have created another key/value pair matching the identifier with the value, for example 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])

My script as is doesn't return anything and I'm not sure why.

like image 966
EA00 Avatar asked Sep 15 '26 22:09

EA00


1 Answers

You are over-engineering this. Just create a mapper, i.e. a dictionary, and then map onto the middle columns. So, using fake-input data:

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

So, create the mapper:

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'}

Then, something quick-and-dirty:

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
like image 66
juanpa.arrivillaga Avatar answered Sep 17 '26 12:09

juanpa.arrivillaga



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!