Question: How can I find the elements from a list in the keys from a dictionary?
0
gravatar for albamaria.muniesa
5.3 years ago by
Spain
albamaria.muniesa0 wrote:

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

dictionary biopython list python • 1.4k views
ADD COMMENTlink modified 5.3 years ago by Eric Normandeau10k • written 5.3 years ago by albamaria.muniesa0

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 REPLYlink modified 5.3 years ago • written 5.3 years ago by linus330

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

ADD REPLYlink written 5.3 years ago by albamaria.muniesa0

maybe your indentation is wrong in your posted code?

ADD REPLYlink written 5.3 years ago by linus330
1

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 REPLYlink modified 5.3 years ago by Istvan Albert ♦♦ 81k • written 5.3 years ago by linus330

I don't think so,

ren = {}
ren = read_rebase(Renzymes)

are "inside" the def read_base function...

ADD REPLYlink written 5.3 years ago by albamaria.muniesa0

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 REPLYlink modified 5.3 years ago • written 5.3 years ago by linus330

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

NameError: name 'Renzymes' is not defined


(Thank you for your answers)

ADD REPLYlink written 5.3 years ago by albamaria.muniesa0
2

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 REPLYlink modified 5.3 years ago • written 5.3 years ago by linus330

Sorry, I dont understand what I should do
 

ADD REPLYlink written 5.3 years ago by albamaria.muniesa0
3

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 REPLYlink modified 5.3 years ago • written 5.3 years ago by Alex Reynolds29k

Thank you so much! It works! :)

ADD REPLYlink written 5.3 years ago by albamaria.muniesa0
1
gravatar for Eric Normandeau
5.3 years ago by
Quebec, Canada
Eric Normandeau10k wrote:

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 COMMENTlink modified 5.3 years ago • written 5.3 years ago by Eric Normandeau10k
0
gravatar for dariober
5.3 years ago by
dariober10k
WCIP | Glasgow | UK
dariober10k wrote:

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 COMMENTlink written 5.3 years ago by dariober10k
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1846 users visited in the last hour