I have two lists, i am trying to match one item from the first list to another from the second list under a certain condition (for example if they share the same number in the same location). i wrote my code to match the first set ['A','B','C',4,'D'] and only print the set from list2 that has 4 in the same location. so basically my output would be:
['A','B','C',4,'D']
[1, 2, 3, 4, 5]
well i can't figure out how to print only the match
here is my code:
list1 = [['A','B','C',4,'D'],['A','B','C',9,'D'],['A','B','C',5,'D'],['A','B','C',6,'D'],['A','B','C',7,'D']]
list2 = [[1,2,3,2,5],[1,2,3,5,5],[1,2,3,3,5],[1,2,3,4,5],[1,2,3,1,5],[1,2,3,2,5]]
for var in list1:
print var
for i in range(0,len(list2)):
for var1 in list2:
if list1[0][3] == list2[i][3]:
print var1
Your program would become easier, if you used izip of itertools. Assuming you just need to print the elements
from itertools import izip
list1 = [['A','B','C',4,'D'],['A','B','C',9,'D'],['A','B','C',5,'D'],['A','B','C',6,'D'],['A','B','C',7,'D']]
list2 = [[1,2,3,2,5],[1,2,3,5,5],[1,2,3,3,5],[1,2,3,4,5],[1,2,3,1,5],[1,2,3,2,5]]
for item1 in list1:
for item2 in list2:
for i,j in izip(item1, item2):
if i==j:
print i
By using izip two times, it would be much easier
from itertools import izip
list1 = [['A','B','C',4,'D'],['A','B','C',9,'D'],['A','B','C',5,'D'],['A','B','C',6,'D'],['A','B','C',7,'D']]
list2 = [[1,2,3,2,5],[1,2,3,5,5],[1,2,3,3,5],[1,2,3,4,5],[1,2,3,1,5],[1,2,3,2,5]]
for i in izip(list1,list2):
for item1, item2 in izip(i[0],i[1]):
if item1 == item2:
print item1
Almost. I am not sure if that is what you wanted but the following code prints all pairs which have the same number in the 4th location of the array:
list1 = [['A','B','C',4,'D'],['A','B','C',9,'D'],['A','B','C',5,'D'],
['A','B','C',6,'D'],['A','B','C',7,'D']]
list2 = [[1,2,3,2,5],[1,2,3,5,5],[1,2,3,3,5],[1,2,3,4,5],[1,2,3,1,5],
[1,2,3,2,5]]
for t in list1:
print t
for b in list2:
if t[3] == b[3]:
print b
Output is:
['A', 'B', 'C', 4, 'D']
[1, 2, 3, 4, 5]
['A', 'B', 'C', 9, 'D']
['A', 'B', 'C', 5, 'D']
[1, 2, 3, 5, 5]
['A', 'B', 'C', 6, 'D']
['A', 'B', 'C', 7, 'D']
Is that what you were looking for?
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']
Example:
a = ['a', 'c'] # first test
b = ['a', 'b', 'c']
a = ['a','e'] # second test
b = ['a','b','c']
list_final = compare(a,b) # we tried this not working
We have to create the following logic: if both elements of list a are in list b, it will proceed further, whereas if one element of list a is not available in list b it has to terminate. Please suggest me a solution.
Is this what you are looking for?
def compare(x, y):
"Return True if all the elements of x are in y, False otherwise"
return all([item in y for item in x])
You can use the function above in conditional branching:
def wrapper(x, y):
if compare(x, y):
print 'Proceed further'
else:
print 'Break'
Give this code a try to figure out how it works:
>>> a1 = ['a','c']
>>> a2 = ['a','e']
>>> b = ['a','b','c']
>>> wrapper(a1, b)
Proceed further
>>> wrapper(a2, b)
Break
Make this as your compare method using all keyword of python :-
def compare(x,y):
if all(i in y for i in x):
print "Proceed"
# Call the next code piece
else:
print "Terminate"
break
Now pass a as x and b as y in the call :-
>>> # Test 1
>>> a = ['a', 'c'] # first test
>>> b = ['a', 'b', 'c']
>>> compare(a, b)
Proceed
>>> # Test 2
>>> a = ['a', 'e'] # first test
>>> b = ['a', 'b', 'c']
>>> compare(a, b)
Terminate
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')]