0

How would I make a dictionary that has keys that represent not just a single string, but multiple strings (array)? So each key is tied to an array that I can keep appending new strings to if the key is present in another file?

I am a relatively new Python programmer so I can visualize how my script would work... but alas I can't think of the proper syntax.

I have the logic down (I think), I just need help with which modules or functions to use in Python. I am about 3/4 the way through Learn Python the Hard Way by Zed Shaw, if you need a reference to my current knowledge in Python.

Lets get to it. Example:

File 1:

A
B
C
D

File 2:

Ted A
Mike A
Wilma B
Frank C
Dog D
Fred D

File 3:

Jon Ted
Sid Ted
Mic Mike
Will Dog
Tod Dog

Hopeful Result (write to File 4):

>A Ted Mike Jon Sid Mic
>B Wilma
>C Frank
>D Dog Fred Will Tod

So using file 1 for the keys we can fill the elements with file 2 and 3 if the key is present. Notice in file 2 A and D have multiple answers. Next look into file 3 and we expand even further; we also see that Jon and Sid(file3) = Ted(file2) = A. A also = Mike (file2) = Mic (file 3)

This is the simplest example of my data (100,000's of sequences), and the real kicker is I have five hierarchically expanding files, not just 2 (example above).

~~~ Answered ~~~

Thank you F.J and others

4
  • Try looking at collections.defaultdict and maybe read about autovivification.
    – JBernardo
    Nov 30, 2012 at 21:58
  • you just set a dictionary's element to an array e.g. a['foo']=[1,2,3]
    – Dmitry B.
    Nov 30, 2012 at 21:59
  • I'm not 100% sure, but also consider looking at a digraph en.wikipedia.org/wiki/Directed_graph - as it sounds like you're heading in that direction... Nov 30, 2012 at 22:05
  • @Jon Yes sir, that will be where the data goes. FYI, the data is ~100,000 sequences from 30 different "MIDs" (samples), which represent 4 different lakes. All of which can be boiled down to 15 OTUs (operational taxonomic unit). The OTUs are represented by the A,B,C above while the sequences(ID's) are represented by the names. The lakes names can be back converted from the sequence ID's. I just need to make a nice script to keep track of all of them :)
    – jon_shep
    Nov 30, 2012 at 23:25

1 Answer 1

1

The following should work:

keys = []
data = {}
names = {}

for line in open('file1', 'r'):
    keys.append(line.strip())
    data[line.strip()] = []

for line in open('file2', 'r'):
    name, key = line.strip().split()
    if key in data:
        data[key].append(name)
        names[name] = key

for line in open('file3', 'r'):
    nickname, name = line.strip().split()
    if name in names:
        data[names[name]].append(nickname)

with open('file4', 'w') as file4:
    for key in keys:
        file4.write('>{0} {1}\n'.format(key, ' '.join(data[key])))
4
  • Could you help me with the final print code in my newest edit? I just wanna make sure all the data is correctly in the hash. If true, I'll thank you for your answer here, could you also explain the line data = {}?? is that the same as saying data = dict()
    – jon_shep
    Nov 30, 2012 at 23:16
  • @jon_shep Yes, data = {} is the same thing as data = dict(). For more info, check out this question.
    – Nathan
    Dec 1, 2012 at 0:03
  • So how can I learn more of this "short hand" syntax? What does the key = [] mean then?
    – jon_shep
    Dec 1, 2012 at 0:10
  • [] creates an empty list, you find this in the Python data model and expressions documentation. Dec 1, 2012 at 2:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.