How can I find the elements from a list in the keys from a dictionary?
2
0
Entering edit mode
9.7 years ago

I have created a dictionary and I am trying to find if the elements in a list called "seqrecord" are the keys of the dictionary. The script I have done is below:

def read_rebase(Renzymes):
    renzymedict = {}
    
    filenz = open(Renzymes)
    for line in filenz.xreadlines():
        fields = line.split()
        name = fields[1]
        pattern = fields[2]
        renzymedict[(pattern)] = name
    filenz.close()

    return renzymedict
    ren = {}
    ren = read_rebase(Renzymes)

for k in seqrecord:
    if k in ren:

        print k, ren[k]

It doesn't work...
NameError: name 'ren' is not defined

What I am doing wrong? 'ren' should be the dictionary...

Thank you

python list dictionary biopython • 2.8k views
ADD COMMENT
0
Entering edit mode

after a return statement nothing will be executed! the function returns to its call by returning renzymedict

return renzymedict

ren = {} #not reached!
ren = read_rebase(Renzymes)
ADD REPLY
0
Entering edit mode

I have deleted return statement, but the result is the same ('ren' is not defined)

ADD REPLY
0
Entering edit mode

Maybe your indentation is wrong in your posted code?

ADD REPLY
1
Entering edit mode

try this

def read_rebase(Renzymes):
    renzymedict = {}

    filenz = open(Renzymes)
    for line in filenz.xreadlines():
        fields = line.split()
        name = fields[1]
        pattern = fields[2]
        renzymedict[(pattern)] = name
    filenz.close()

    return renzymedict

ren = {}

ren = read_rebase(Renzymes)

for k in seqrecord:
    if k in ren:
        print k, ren[k]
ADD REPLY
0
Entering edit mode

I don't think so,

ren = {}
ren = read_rebase(Renzymes)

are "inside" the def read_base function...

ADD REPLY
0
Entering edit mode

but this will not work. if you put it outside of the function it will work. it works like this:

ren = {} #initialize a empty dict

ren = read_rebase(Renzymes) # call the function read_rebase(Renzymes) which returns renzymdict, which is a dict

now ren is declared and a known variable which can be used in

if k in ren:
ADD REPLY
0
Entering edit mode

Ok, modified this error, now there is a new one...

NameError: name 'Renzymes' is not defined

(Thank you for your answers)

ADD REPLY
2
Entering edit mode

Renzymes is variable which should contain your filename.

def read_rebase(Renzymes):
    renzymedict = {}

    filenz = open(Renzymes)
    for line in filenz.xreadlines():
        fields = line.split()
        name = fields[1]
        pattern = fields[2]
        renzymedict[(pattern)] = name
    filenz.close()
    return renzymedict

ren = {}

ren = read_rebase("/your/path/and/file.txt")

for k in seqrecord:
    if k in ren:
        print k, ren[k]
ADD REPLY
0
Entering edit mode

Sorry, I don't understand what I should do

ADD REPLY
3
Entering edit mode

Here is how you defined your function read_rebase:

def read_rebase(Renzymes):
    ...

When you call this function, you need to provide some value for the local variable Renzymes that is a path to a file somewhere. For example:

ren = read_rebase("/your/path/and/file.txt")

The file /your/path/and/file.txt will get opened in the function read_rebase.

If you instead call this function like so:

ren = read_rebase(Renzymes)

Then you need to have defined a global variable (also poorly and ambiguously named as Renzymes) somewhere further up in the code. If this variable is not defined, then the read_rebase function cannot work.

So either set the path to the file explicitly, as shown above, or set a sensibly-named variable and pass that to your function, e.g.:

path_to_Renzymes_file = "/foo/bar/baz.txt"
ren = read_rebase(path_to_Renzymes_file)
ADD REPLY
0
Entering edit mode

Thank you so much! It works! :)

ADD REPLY
1
Entering edit mode
9.7 years ago

Use the built-in set functions of Python, eg:

s = set(seqrecord= ['a', 'b', 'd', 'e'])
m = set(myDict= {'a': 0, 'd': 0, 'f': 0})

s.intersection(m)

s.difference(m)

m.difference(s)

You do not have to transform them into sets before hand, of course. It can be done on the fly, just substitute s or m by set(...)

ADD COMMENT
0
Entering edit mode
9.7 years ago

To intersect lists, i.e. to find what elements in a list are or are not present in another list, it is good to use list comprehensions, they are fast and readable:

seqrecord= ['a', 'b', 'd', 'e']
myDict= {'a': 0, 'd': 0, 'f': 0}

## Elements in common between seqrecord and keys
[x for x in seqrecord if x in myDict.keys()]
['a', 'd']

## Elements in seqrecord NOT in keys
[x for x in seqrecord if x not in myDict.keys()]
['b', 'e']

## Keys NOT in seqrecord
[x for x in myDict.keys() if x not in seqrecord]
['f']
ADD COMMENT

Login before adding your answer.

Traffic: 2691 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6