every key compares with value(you may say there is spelling check betwen key and value).If there are only 2 words mismatch then print the key
input={"their":"thuor","diksha","dijmno"}
output=["their"]
def find_correct(words_dict):
count=0
final_list=[]
for key,value in words_dict.items():
for i in range(len(value)): # this may need adjusting for different length words
if(value[i]!=key[i]):
count+=1
if(count<=2):
final_list.append(key)
return final_list
print(find_correct({"their":"thuor","diksha":"dijmno"}))
This can be done with list comprehension and sets
print([i for i in d if len(set(i) - set(d[i])) == 2])
I have two dictionaries:
dict_1 = {'H1':[0,1,3,2],
'H2':[5,4,2,1,5],
'H3':[1,2,5]}
dict_2 = {'H1':[[1,4,3,2],
[6,5,3,2],
[1,4,3,2]],
'H2':[[0,0,1,6,3],
[2,1,4,2,1],
[5,1,2,5,3]],
'H3':[[2,3,4],
[2,1,4],
[0,1,2]]}
I'm trying to iterate through the items in the values of the keys in dict_1, and check if any of those items are in any of the items in the values of the corresponding key in dict_2. Then print the value, and the index where it was found in the key of dict_2.
So for example, because 0 in the list of the key 'H1' of dict_1 isn't found in any of the lists of the key 'H1' in dict 2, nothing would be printed. Now if we check for 1, then
1, 'H1', 0, # because there is a one in the first list of 'H1' of dict_2
1, 'H1', 2, # because there is a one in the 3rd list of 'H1' of dict_2
here 1 corresponds to the item that was checked for, 'H1' the key that was checked, and 0, and 2 represent which list the 1 was found in in dict_2
this is what I have:
for i in range(3):
for k,v in dict_1.items():
for x,y in dict_2.items():
if k == x:
for j in v:
if j in y[i]:
print j,x,i
But I'm wondering if this will take care of everything, or if it will print repeated items?
This should work:
for key, d1_list in dict_1.iteritems():
for element in d1_list:
for ind, d2_list in enumerate(dict_2[key]):
if element in d2_list:
print element, key, ind
Iterate over all the key, and lists and d1. Then over element in d1_list, and finally check if it's any any of the lists in dict_2 corresponding to the same key.
Some answers first: yes, your code will "take care of everything", and will not "print repeated items" meaning that if a value is appearing several time in the same list in dict_2, than it will be spotted only once by your code.
Executing your code on those dict demonstrates this:
dict_1 = {'H1':[0,1,3,2]}
dict_2 = {'H1':[[0,0],
[1,2]]}
for i in range(2):
for k,v in dict_1.items():
for x,y in dict_2.items():
if k == x:
for j in v:
if j in y[i]:
print j,x,i
Output is:
0 H1 0
1 H1 1
2 H1 1
Hence spotting only once the 0 value while appearing twice in first sublist in dict_2.
Now, to complete the same objective, we can simplify a little your code, given that:
you want to compare only the same keys in the two dicts
you may want to have different size of list in dict_2
I would propose the following:
# Loop on all key values in dict_1
for k,v in dict_1.items():
# Loop only on values in dict_2 for same key
for i,y in enumerate(dict_2[k]):
# Loop over values in first list
for j in v:
# Assert existence in second list
if j in y:
# Display result if found (note that index of subblist in dict_2 is retrieved through enumerate function
print j,x,i
This output the exact same result with additional lisibility and flexibility for dict_2 format.
Hope this helps.
I have a python dictionary containing n key-value pairs, out of which n-1 values are identical and 1 is not. I need to find the key of the distinct element.
For example: consider a python list [{a:1},{b:1},{c:2},{d:1}]. I need the to get 'c' as the output.
I can use a for loop to compare consecutive elements and then use two more for loops to compare those elements with the other elements. But is there a more efficient way to go about it or perhaps a built-in function which I am unaware of?
If you have a dictionary you can quickly check and find the first value which is different from the next two values cycling around the keys of your dictionary.
Here's an example:
def find_different(d):
k = d.keys()
for i in xrange(0, len(k)):
if d[k[i]] != d[k[(i+1)%len(k)]] and d[k[i]] != d[k[(i+2)%len(k)]]:
return k[i]
>>> mydict = {'a':1, 'b':1, 'c':2, 'd':1}
>>> find_different(mydict)
'c'
Otherwise, if what you have is a list of single-key dictionaries, then you can do it quite nicely mapping your list with a function which "extracts" the values from your elements, then check each one using the same logic.
Here's another working example:
def find_different(l):
mask = map(lambda x: x[x.keys()[0]], l)
for i in xrange(0, len(l)):
if mask[i] != mask[(i+1)%len(l)] and mask[i] != mask[(i+2)%len(l)]:
return l[i].keys()[0]
>>> mylist = [{'a':1},{'b':1},{'c':2},{'d':1}]
>>> find_different(mylist)
'c'
NOTE: these solutions do not work in Python 3 as the map function doesn't return a list and neither does the .keys() method of dictionaries.
Assuming that your "list of pairs" (actually list of dictionaries, sigh) cannot be changed:
from collections import defaultdict
def get_pair(d):
return (d.keys()[0], d.values()[0])
def extract_unique(l):
d = defaultdict(list)
for key, value in map(get_pair, l):
d[value].append(key)
return filter(lambda (v,l): len(l) == 1, d.items())[0][1]
If you already have your dictionary, then you make a list of all of the keys: key_list = yourDic.keys(). Using that list, you can then loop through your dictionary. This is easier if you know one of the values, but below I assume that you do not.
yourDic = {'a':1, 'b':4, 'c':1, 'd':1, }
key_list = yourDic.keys()
previous_value = yourDic[key_list[0]] # Making it so loop gets past first test
count = 0
for key in key_list:
test_value = yourDic[key]
if (test_value != previous_value) and count == 1: # Checks first key
print key_list[count - 1]
break
elif (test_value != previous_value):
print key
break
else:
previous_value = test_value
count += 1
So, once you find the value that is different, it will print the key. If you want it to print the value, too, you just need a print test_value statement
I have files with a lines as such, where every row has an index (a,b) and then a list of number associated to them
a\t12|123|08340|4985
b\t3856|12|276
What i want is to get to this output
12 a
123 a
8340 a
4985 a
3856 b
276 b
Note that I am only wanting to output a unique set of the genes, with the value of first occurrence in case there are more than one of the same numbers in the rows.
I went about it in this way: by trying to add the numbers to a dictionary with the letter as keys, and the numbers as values. Finally, only outputting the set() of the numbers together with the corresponding letter.
uniqueval = set()
d = defaultdict(list)
for line in file:
fields = line.strip().split(\t)
Idx = fields[0]
Values = fields[1].split("|")
for Val in Values:
uniqueval.add(Val)
d[Idx] += Val
for u in uniqueval:
print u,"\t", [key for key in d.keys() if u in d.values()]
The script runs, but when I look into the dictionary, the Val's are all split by character, as such:
{'a': ['1','2','1'....], 'b': ['3', '8',....]}
I don't understand why the Values get split since it's in a for loop, I thought it was going to take each Val as a new value to add to the dict. Could you help me understand this issue?
Thank you.
You are extending your lists with Val:
d[Idx] += Val
This adds each character in Val as a separate element.
Use append() instead:
d[Idx].append(Val)
I have a dictionary of lists with info such as var1=vara, var1=varb, var2=vara etc. This can have lots of entries, and I print it out ok like this
for y in myDict:
print(y+"\t"+myDict[y])
I have another list which has exclusions in like this var2, var3 etc. This may have < 10 entries and I can print that ok like this
for x in myList:
print(x)
Now I want to remove occurrences of key val pairs in the dictionary where the keys are the list values. I tried this
for x in myList:
for y in myDict:
if x != y: print(y+"\t"+myDict[y])
but on each pass through the list it lets all the others apart from the current `x to the screen
Is there a nice python way to remove the key val pairs from the dictionary if the key exists in the list?
Do you mean
for key in myDict:
if key not in myList:
print(key+"\t"+myDict[key])
Or one of many alternatives:
for key in (set(myDict)-set(myList)):
print(key+"\t"+myDict[key])
mySet = set(myList)
myNewDict = dict(((k, v) for k, v in myDict if k not in mySet))
Note that using mySet instead of myList isn't a concern unless myList has a large number of entries.