1

As a total beginner I'm quite proud of this function. Although I believe there's probably an easier, more pythonic way of doing the exact same thing:

Genes = ['Gen1', 'Gen2', 'Gen3']
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C']

def RawDict(keys, values):
    dictKeys = []
    dictValues = []
    for key in keys:
        keyVal = []
        for value in values:
            if value.find(key) == -1:
                pass
            else:
                keyVal.append(value)
        dictKeys.append(key)
        dictValues.append(keyVal)       
    return zip(dictKeys, dictValues)

GenDict = dict(RawDict(Genes, Mutations))

print(GenDict)

The function above is a rather overcomplicated (I think) way of putting several values (mutations) within keys (genes). However I was wondering if I could tweak this so I could get a dictionary by just doing this:

dict(GenDict, Genes, Mutations)

print(GenDict)

My struggle involves that when I use dict within the function, this won't work:

Genes = ['Gen1', 'Gen2', 'Gen3']
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C']

def fullDict(dictName, keys, values):
    dictKeys = []
    dictValues = []
    for key in keys:
        keyVal = []
        for value in values:
            if value.find(key) == -1:
                pass
            else:
                keyVal.append(value)
        dictKeys.append(key)
        dictValues.append(keyVal)       
    dictName = dict(RawDict(Genes, Mutations))

fullDict(GenDict, Genes, Mutations)

print(GenDict)

The above just won't work as GenDict is not defined.

3 Answers3

4

I am assuming that you want the "Gen"s to be stored by the numerical value that it contains.

Genes = ['Gen1', 'Gen2', 'Gen3']
Mutations = ['Gen1.A', 'Gen1.B', 'Gen2.A', 'Gen3.A', 'Gen3.B', 'Gen3.C']
the_dict = {i:[] for i in Genes}

for i in Mutations:
    new_val = i.split(".")

   the_dict[new_val[0]].append(i)

print(the_dict)

Output:

{'Gen2': ['Gen2.A'], 'Gen3': ['Gen3.A', 'Gen3.B', 'Gen3.C'], 'Gen1': ['Gen1.A', 'Gen1.B']}
Ajax1234
  • 69,937
  • 8
  • 61
  • 102
  • 1
    That. Was. Beautiful. – Miguel Alberola Cano Jun 08 '17 at 16:29
  • May I ask if you know of any source for things like these? Making up functions by looking up random functions in the python documentation doesn't seem efficient. And grabbing functions from repositories way above my skill level doesn't seem like a good idea... – Miguel Alberola Cano Jun 08 '17 at 16:30
  • 2
    You probably want to start by looking at other questions on Stackoverflow that are tagged with dict-comprehension and list comprehension and attempting to solve what other users post for questions yourself. To get a better feel for list and dict comprehension in general, you can take a look at this link: https://stackoverflow.com/questions/14507591/python-dictionary-comprehension – Ajax1234 Jun 08 '17 at 16:33
  • Thanks! I'll look it up! – Miguel Alberola Cano Jun 08 '17 at 16:36
  • 1
    Glad I could help! – Ajax1234 Jun 08 '17 at 16:36
4

From what I understand, you want to move from this:

gen_dict = make_dictionary(genes, mutations)

to this:

make_dictionary(gen_dict, genes, mutations)

where the make_dictionary function "creates" the variable gen_dict.

Unfortunately, this isn't really how variables work. If you want to define a variable called GenDict, the way to do this is to use GenDict = .... You could do something like this:

gen_dict = {}
fill_dictionary(gen_dict, genes, mutations)

This creates a variable called gen_dict and assigns it to a new, empty dictionary. Your function would then go through and add things to that dictionary:

def fill_dictionary(d, genes, mutations):
    for g in genes:
      d[g] = [m for m in mutations if m.startswith(g)]

But calling a function cannot cause a new variable to appear in the caller's scope. (This is not completely true, because of globals(), but for most intents and purposes, it is.)

(By the way, there is a one-liner that will create the dictionary: dictionary = { g : [m for m in mutations if m.startswith(g+".")] for g in genes }. Search for list comprehensions and dictionary comprehensions on Google or StackOverflow -- they are amazing!)

Alex Lew
  • 2,064
  • 14
  • 17
1

I assume you have a background in programming in some other language than Python; a language that lets you change function parameters. Well, Python does not. The problem is not with the use of dict, but rather with the fact that you're assigning to a function parameter. This will not have an effect outside the function. What you want to do is probably this:

def fullDict(keys, values):
    return { key: [ value for value in values if key in value] for key in keys }

print(fullDict(Genes, Mutations))
Błotosmętek
  • 12,717
  • 19
  • 29