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')]
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)
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']
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 list and a dictionary as below:
List1 = ['a', 'b', 'c', 'd']
d1 = OrderedDict([('c', '1'), ('b', '2')])
Suppose, List1 is a sorted list. How do I find a key in d1 which appears first in the List1? In example above its 'b'.
My code is below-
print d1.items()
d2={}
for key in d1:
d2 [key]=List1.index(key)
print "Output is", min(d2.items(), key=lambda x: x[1])[0]
The code is verbose. I would prefer more efficient code, may be one line code.
Edit: All keys of the dictionary appear on the list.
On average, x in set is O(1) while x in list is O(n).
In [17]: List1 = ['a', 'b', 'c', 'd']
In [18]: d1 = OrderedDict([('c', '1'), ('b', '2')])
In [19]: for key in List1:
....: if key in d1:
....: print key
....: break
....:
b
If you only want to have the top-first, then the code Sait posted does it. If you want to get all the keys that are found in the dict in the sequence they are found in the list the following helps. And in addition the first element of the returned list is of course the first found.
from collections import OrderedDict
List1 = ['a', 'b', 'c', 'd']
d1 = OrderedDict([('c', '1'), ('b', '2')])
found = [el for el in List1 if el in d1]