Replace dict key by value of other dict - python

Today I need to replace the key of dict one by value of dict two. Dict one has multiple keys and I only want to replace the keys which match dict 2.
In the end I want to get the dict one back with the old keys (the ones which did not match) and the new keys (which have been changed when they matched)
I wrote the following script but I get no output so I am not sure if I am doing it right, can someone explain to me?
Thanks a lot
ERCC = {}
my_file = open('a.txt')
for line in my_file:
config,name = line.strip().split()
ERCC[contig] = name
RSEM = {}
names_file = open('b.txt')
for line in names_file:
genes, count = line.strip().split()
RSEM[gene] = count
def convert(RSEM,ERCC):
for key, value in RSEM.items():
for keys, values in ERCC.items():
if keys == key:
RSEM[key] = values
return RSEM
print RSEM
convert(RSEM, ERCC)

>>> dic={}
>>> for k,v in myboi.items():
r=input("Enter item to Update write in ""=")
if r:
dic[r]=v
else:
dic[k]=v
Enter item to Update write in ="Mahesh"
Enter item to Update write in ="Saka"
>>>
>>> dic
{'Mahesh': 'Mahesh', 'Saka': 'Mahesh'}
You want compulsary input key in this program you want update one or more time you empty dic={}
result={'Mahesh': 'Mahesh', 'Saka': 'Mahesh'}
>>> fi.close()
>>> fi=open("m.txt","r")
>>> fi.readlines()
['Maheshname']
>>> fi=open("m.txt","w+")
>>> for k,v in myboi.items():
fi.write("'"+k+"'"+":"+"'"+v+"'")
>>> fi.close()
>>> fi=open("m.txt","r")
>>> fi.readlines()
["'Mahesh':'Mahesh''name':'Mahesh'"]

Here's a two-liner for the convert function:
RSEM = {key: ERCC.get(key, RSEM[key]) for key in RSEM}
print RSEM
To dump a dict to a file just do:
with open("your_file_name", "w") as dumpfile:
dumpfile.write(str(RSEM))

Your code seems to be fine. But you have used return statement before print statement. The execution of convert function stops at return *** and the print statement is not executed at all. That is the reason why you are not getting any output.

Related

iterate over a subset of dictionary keys

I am looking to learn how to pass certain keys/values in a dictionary to another function within a for loop. The "certain" keys all share the same initial string and are incremented by a trailing integer like this:
data = {}
data["HMD1"] = [a,b,c]
data["HMD2"] = [d,f,g] #and so on...
There are other keys with dissimilar names witin the same dictionary. Now within a for loop I would like to pass the values for each key that starts with "HMD" to another function. Here is a minimal working example of a failed attempt:
data = {}
data["HMD1"] = [0,2,3]
data["HMD2"] = [5,6,4]
data["not"] = 1237659398
data["HMD3"] = [1,1,1]
def dummyfun(vargin):
print(vargin)
return vargin
for f in range(1,2,1):
out = dummyfun(data[eval(''.join(("HMD",str(f))))])
This was a poor guess, of course it returns an error because eval() tries to evaluate "HMD1" which is not a variable but a key in data. Does anyone know how to do this properly?
You don't need eval at all for this. You only need to build the string with .format for example
for f in range(1,4): #range don't include the end point
out = dummyfun(data["HMD{}".format(f)])
with this you get the desire result. But that will fail if the key is not in the dict, you can check it first, catch the exception or provide a default value in case the desire key is not there
#check first
for f in range(1,4):
key = "HMD{}".format(f)
if key in data:
out = dummyfun(data[key])
#catch the exception
for f in range(1,4):
try:
out = dummyfun(data["HMD{}".format(f)])
except KeyError:
print("key",f,"is not in the data")
#provide a default value
for f in range(1,4):
out = dummyfun(data.get("HMD{}".format(f),None))
Just iterate through the dictionary using a for loop and use an if statement to check for validity of the keys:
for key in yourDict: #a for loop for dict iterates through its keys
if 'HMD' in key: #or you can replace with any other conditional
#DO WHAT YOU WANT TO DO HERE
And here's a quick working example:
>>> data = {'HMD1': [1,2,3], 'HMD23':'heyo mayo', 'HMNOT2':'if this prints, I did something wrong'}
>>> for key in data:
... if 'HMD' in key:
... print data[key]
...
[1, 2, 3]
heyo mayo
With further understand of what you want, you can also look at this backwards and create key strings and print the values that those key's point to:
#let's say you want to print HMD1, HMD2, HMD4, but not anything else
keylist = [#list of keys that you want]
for key in keylist:
if key in data:
print data[key]
and, again, a working example.
>>> data = {'HMD1': [1,2,3], 'HMD3':'heyo mayo, this shouldnt print', 'HMD4':123, 'HMD2':['g', 'h', 'i'], 'HMNOT2':'if this prints, I did something wrong'}
>>> keylist = ['HMD1', 'HMD2', 'HMD4']
>>> for key in keylist:
... if key in data:
... print data[key]
...
[1, 2, 3]
['g', 'h', 'i']
123

Python Dictionary Not Returning All It's Items

I'm having an issue with my code were I read a text file, put the information in the file inside a list, and then convert the list into a dictionary.
The problem is that when I try to print the items contained in it (be it the entire dictionary or the keys or values only), it only prints one key/value pair.
In fact, the dictionary only contains that particular key/value pair and nothing more.
Here is my code:
class Babies:
b_list = []
b_dict = {}
def __init__(self, scotbabies=None): # placeholder
self.scotbabies = scotbabies
def read_names_from_file(self, file):
global b_list
global b_dict
for line in open(file):
b_list = line.split()
b_dict = {k: v for k, v in (b_list for word in b_list)}
print b_dict # if I print the dictionary at this point it will
# print the entire dictionary. If I try to print it inside another
# function or elsewhere, it will only print the first key/value pair.
babies = Babies()
babies.read_names_from_file('scotbabies2014.txt')
The text file contains a list of all the names of the babies born in Scotland along with their gender.
You are redefining the entire dictionary every iteration of the loop.
b_dict = {k: v for k, v in (b_list for word in b_list)} reassigns b_dict to a new dictionary.
You should look into the dictionary type's update function, which kind of merges two dictionaries.

How to add list object to Dictionary in Python

I am trying to add list object to Dictionary.
Code:
for line in file:
splitObj=line.split("=") #split a line xxx= yyy using = delimiter
listObj=list(splitObj)
chip_mem.update(listObj[0], listObj[1])
This didn't help. Can anyone please let me know how to add the contents of the list to the dict as a key, value pair?
for line in file:
pieces = line.split("=")
chip_mem[pieces[0]] = pieces[1]
Note that there's no reason to call list() on the result of .split, since it's already a list.
Note that if not all of your lines have = signs in them, you might need to check for this:
for line in file:
pieces = line.split("=")
if len(pieces) > 1:
chip_mem[pieces[0]] = pieces[1]
Assuming you get a key and a value made for each thing in your list by splitting them on the equal sign, you can always do:
for k,v in list.split("="):
mydict[k] = v
dict update method requires other key-value pairs, see:
>>> mydict = {}
>>> mydict.update({'param': 'value'})
>>> print mydict # prints{'param': 'value'}
So in your case, for instance:
myfile = open("config.txt")
lines = myfile.readlines()
chip_mem = {}
for line in lines:
parts = line.strip().split("=")
chip_mem.update({parts[0]: parts[1]})
print chip_mem

How to turn a dictionary "inside-out"

Disclaimer: I am just getting started learning Python
I have a function that counts the number of times a word appears in a text file and sets the word as the key and the count as the value, and stores it in a dictionary "book_index". Here is my code:
alice = open('location of the file', 'r', encoding = "cp1252")
def book_index(alice):
"""Alice is a file reference"""
"""Alice is opened, nothing else is done"""
worddict = {}
line = 0
for ln in alice:
words = ln.split()
for wd in words:
if wd not in worddict:
worddict[wd] = 1 #if wd is not in worddict, increase the count for that word to 1
else:
worddict[wd] = worddict[wd] + 1 #if wd IS in worddict, increase the count for that word BY 1
line = line + 1
return(worddict)
I need to turn that dictionary "inside out" and use the count as the key, and any word that appears x amount of times as the value. For instance: [2, 'hello', 'hi'] where 'hello' and 'hi' appear twice in the text file.
Do I need to loop through my existing dictionary or loop through the text file again?
As a dictionary is a key to value mapping, you cannot efficiently filter by the values. So you will have to loop through all elements in the dictionary to get the keys which values have some specific value.
This will print out all keys in the dictionary d where the value is equal to searchValue:
for k, v in d.items():
if v == searchValue:
print(k)
Regarding your book_index function, note that you can use the built-in Counter for counting things. Counter is essentially a dictionary that works with counts as its values and automatically takes care of nonexistant keys. Using a counter, your code would look like this:
from collections import Counter
def book_index(alice):
worddict = Counter()
for ln in alice:
worddict.update(ln.split())
return worddict
Or, as roippi suggested as a comment to another answer, just worddict = Counter(word for line in alice for word in line.split()).
Personally I would suggest the use of a Counter object here, which is specifically made for this kind of application. For instance:
from collections import Counter
counter = Counter()
for ln in alice:
counter.update(ln.split())
This will give you the relevant dictionary, and if you then read the Counter docs
You can just retrieve the most common results.
This might not work in every case in your proposed problem, but it's slightly nicer than manually iterating through even the first time around.
If you really want to "flip" this dictionary you could do something along these lines:
matching_values = lambda value: (word for word, freq in wordict.items() if freq==value)
{value: matching_values for value in set(worddict.values())}
The above solution has some advantages over other solutions in that the lazy execution means that for very sparse cases where you're not looking to make a lot of calls to this function, or just discover which value actually have corresponding entries, this will be faster as it won't actually iterate through the dictionary.
That said, this solution will usually be worse than the vanilla iteration solution since it actively iterates through the dictionary every time you need a new number.
Not radically different, but I didn't want to just copy the other answers here.
Loop through your existing dictionary, here is an example using dict.setdefault():
countdict = {}
for k, v in worddict.items():
countdict.setdefault(v, []).append(k)
Or with collections.defaultdict:
import collections
countdict = collections.defaultdict(list)
for k, v in worddict.items():
countdict[v].append(k)
Personally I prefer the setdefault() method because the result is a regular dictionary.
Example:
>>> worddict = {"hello": 2, "hi": 2, "world": 4}
>>> countdict = {}
>>> for k, v in worddict.items():
... countdict.setdefault(v, []).append(k)
...
>>> countdict
{2: ['hi', 'hello'], 4: ['world']}
As noted in some of the other answers, you can significantly shorten your book_index function by using collections.Counter.
Without duplicates:
word_by_count_dict = {value: key for key, value in worddict.iteritems()}
See PEP 274 to understand dictionary comprehension with Python: http://www.python.org/dev/peps/pep-0274/
With duplicates:
import collections
words_by_count_dict = collections.defaultdict(list)
for key, value in worddict.iteritems():
words_by_count_dict[value].append(key)
This way:
words_by_count_dict[2] = ["hello", "hi"]

Defining a list of values for a dictionary key using an external file

I have a file with a list of paired entries (keys) that goes like this:
6416 2318
84665 88
90 2339
2624 5371
6118 6774
And I've got another file with the values to those keys:
266743 Q8IUM7
64343 H7BXU6
64343 Q9H6S1
64343 C9JB40
23301 Q8NDI1
23301 A8K930
As you can see the same key can have more than one value. What I'm trying to do is creating a dictionary by automatically creating the initial k, v pair, and then append more values for each entry that is already in the dictionary, like this:
Program finds "266743: 'Q8IUM7'", then "64343: 'H7BXU6'". And when it finds "64343: 'Q9H6S1'" it does this: "64343: ['H7BXU6', 'Q9H6S1']".
This is what I have so far:
# Create dictionary
data = {}
for line in inmap:
value = []
k, v = [x.strip() for x in line.split('\t')]
data[k] = value.append(v)
if k in data.viewkeys() == True and v in data.viewvalues() == False:
data[k] = value.append(v)
But the if statement seems to not be working. That or having the value = [] inside the for loop. Any thoughts?
This is not a good idea. You should be using a list from the start and expand that list as you go along, not change from "string" to "list of strings" when more than one value is found for the key.
For this, you can simply use
from collections import defaultdict
data = defaultdict(list)
for line in inmap:
k, v = (x.strip() for x in line.split('\t'))
data[k].append(v)
This works because a defaultdict of type list will automatically create a key together with an empty list as its value when you try to reference a key that doesn't yet exist. Otherwise, it behaves just like a normal dictionary.
Result:
>>> data
defaultdict(<type 'list'>, {'23301': ['Q8NDI1', 'A8K930'],
'64343': ['H7BXU6', 'Q9H6S1', 'C9JB40'], '266743': ['Q8IUM7']})

Categories