I am new to Python and I have been having some issues with dictionaries.

My aim is to get a dictionary as below:

done_dict = {orthogroup1: 1, orthogroup2: 1, orthogroup3: 1}

So far I have the following:

for orthogroup_name in done_match:
    split_name =  orthogroup_name.split('.')
    split_name = split_name[0]
    done_dict = dict.fromkeys(split_name)
    done_dict = dict.fromkeys
    print(done_dict)

With this I get something like this:

{'g': None, 'h': None, 'o': None, '1': None, 'p': None, 'r': None, 'u': None, 't': None, '6': None, '8': None}

Can somebody help explain how do I turn None into 1 and how I get python to not split every character into a new key?

Thank you so very much!

1 Answers

3
Patrick Haugh On

fromkeys expects an iterable of keys that is uses to build the dictionary. You're providing it a single key at a time, which it is interpreting as an iterable of characters.

You can set up a generator expression to process the file names into the keys, and then give that generator to fromkeys:

processed_names = (name.split('.')[0] for name in done_match)
d = dict.from_keys(processed_names, 1)