If I had a dict containing {'a':'b', 'b':'c', 'c':'d'} and I want to use these keys to replace the contents of list l = ['z', 'q', 'f'] with their corresponding value, how would I do it?
When I first tried to solve this problem, I figured I could enter something like list[i] = get.(i) for i in dict. That doesn't seem to work, though.
my_dict = {'a':'b', 'b':'c', 'c':'d'}
l = ['b', 'c', 'a']
new_list = [my_dict[x] for x in l]
Of course, that's assuming you have a key for every element in the l list. Afterwards you can then do l = list(new_list). If you want to still use the l variable.
Below should take care of corner case scenarios ...
cyclic keys occurring in dictionary (e.g. {'a':'b', 'b':'c', 'c':'d'})
key is repeating multiple times in list (e.g. ['b', 'c', 'a', 'z', 'b', 'c'])
key in list doesn't exists in dictionary's keys (e.g. 'z')
Here are 2 solutions, one by updating same list and second by creating new list.
Updating same list
dictionary = {'a':'b', 'b':'c', 'c':'d'}
l = ['b', 'c', 'a', 'z', 'b', 'c']
print(l)
position = 0
for item in l:
if item in dictionary.keys():
l[position] = dictionary[item]
position = position + 1
print(l)
Creating new list
dictionary = {'a':'b', 'b':'c', 'c':'d'}
l = ['b', 'c', 'a', 'z', 'b', 'c']
nl = []
for item in l:
if item in dictionary.keys():
nl.append(dictionary[item])
else:
nl.append(item)
print(l)
print(nl)
Sample Run
======= RESTART: C:/listByMap.py =======
['b', 'c', 'a', 'z', 'b', 'c']
['c', 'd', 'b', 'z', 'c', 'd']
Related
When I practising Python, I have two lists:
list_a = [1, 'a', 'c', 'e', 'f']
list_b = [2, 'b', 'c', 'd', 'e']
and I want the output is:
list_c = [3, 'a','b','c','d','e','f']
I tried:
list_c = [x + y for (x, y) in zip(list_a, list_b)]
the output is:
[3, 'ab', 'cc', 'ed', 'fe']
I also tried:
list_c = set(list_a + list_b)
the output is:
{1, 2, 'a', 'b', 'c', 'd', 'e', 'f'}
Can someone know how to do it? And the real output is like this:
list_c = [3, 'a','b','c','d','e','f']
Thanks.
This is an option for your example but I'm not really sure what you want.
list_a = [1, 'a', 'c', 'e', 'f']
list_b = [2, 'b', 'c', 'd', 'e']
def merge(a,b):
result=[]
for (r,p) in zip(a,b):
if(type(r) == type(p)):
if type(r)==int:
result.append(str(r+p))
else:
result.append(r)
result.append(p)
else:
result.append(r)
result.append(p)
result = list(set(result))
result.sort()
for n,k in enumerate(result):
try:
result[n] = int(k)
except:
pass
return(result)
print(merge(list_a,list_b))
Prints:[3, 'a', 'b', 'c', 'd', 'e', 'f']
I have a list and a dictionary:
list = ['a', 'b', 'c'] .
dict = {'1': ['a', 'd', 'e'], '2': ['b', 'c', 'f'], '3': ['b', 'a', 'e']} .
I want to get the key of the one that matches the lists items the most. If there are two with the same amount i want both.
Assuming your writing in python, this is a simplistic approach which either appends or adds new keys whose values' similarity to the list are at a maximum.
l= ['a', 'b', 'c']
d = {'1': ['a', 'd', 'e'], '2': ['b', 'c', 'f'], '3': ['b', 'a', 'e']}
high = -1
key = []
for k,v in d.items():
occ = (len(l) + len(v)) - len(set(l + v))
print((set(l+v)))
if(occ >= high):
if(occ == high):
key.append(k)
else:
key = [k]
high = occ
print(key)
I am new to python coding, I would like some help in creating nested list with specific place values. I want to list values which are in 0,3,6,9... into one nested list, 1,4,7,10 in one nested list and so on.
I have an original list as:
List1 = [A,B,C,D,E,F,G,H,I,J,K,L]
I need output nested list like this:
List2 = [[A,D,G,J],[B,E,H,K],[C,F,I,L]]
Currently I am using python 2.7 version. I am not bothered about sorting the list before or after.Can anyone please help me with python code for this?
You could use a list comprehension with slicing:
>>> List1 = list('ABCDEFGHIJKL')
>>> List1
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L']
>>> [List1[i::3] for i in range(3)]
[['A', 'D', 'G', 'J'], ['B', 'E', 'H', 'K'], ['C', 'F', 'I', 'L']]
I have a huge list and want to convert it into a dictionary like this.
Sample list: ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
Output dictionary: {'a':'b', 'c':'d', 'e':'f', 'g':'h'}
I want the sequence to be intact. I read another post similar to it which uses izip from itertools. I tried using it as:
from itertools import izip
i = iter(list_name)
dic = dict(izip(i, i))
But it gives me a dictionary with all sequence jumbled.
Also, the list has even number of elements.
dicts are unordered you can use an OrderedDict to maintain insertion order:
from collections import OrderedDict
from itertools import izip
i = iter(list_name)
dic = OrderedDict(izip(i, i))
Output:
In [3]: list_name = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']
In [4]: i = iter(list_name)
In [5]: dic = OrderedDict(izip(i, i))
In [6]: dic
Out[6]: OrderedDict([('a', 'b'), ('c', 'd'), ('e', 'f'), ('g', 'h')]
I am doing a minor structure manipulation using python, and have a few issues.
Currently my output is the data below.
[['a', ['b', 'c'], ['d', 'e']], ['h', ['i'], ['j']]]
I want to get into this structure below, but my data structure comes out a bit wrong. There could be multiple lists with different entry per list.
(a, b, a, d), (a, c, a, e), (h, i, h, j)
What would be the best approach?
Here's a quick one:
from itertools import product, izip
data = [['a', ['b', 'c'], ['d', 'e']], ['h', ['i'], ['j']]]
result = []
for d in data:
first = d[0]
for v in izip(*d[1:]):
tmp = []
for p in product(*[first, v]):
tmp.extend(p)
result.append(tuple(tmp))
print result
Output:
[('a', 'b', 'a', 'd'), ('a', 'c', 'a', 'e'), ('h', 'i', 'h', 'j')]