How to match elements from arrays and only print matches? - python-2.7

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?

Related

finding index of list based on another list in Julia

I have two lists, A and B, B the sublist of A. Both the elements in the A and B are randomly ordered. I am looking for a way to find indexes of elements in A for elements in B, probably ordered. In Python, the following seems to work
B_list = sorted([A.index(i) for i in B])
I am not sure how this can be achieved in Julia. I am pretty new to the Language. I tried something like
B_list = sort(filter(in(B), A))
It did not work at all, please help!
There's a function (inspired by MATLAB) that can directly do it for you: indexin(B, A)
julia> a = ['a', 'b', 'c', 'b', 'd', 'a'];
julia> b = ['a', 'b', 'c'];
julia> indexin(b, a)
3-element Vector{Union{Nothing, Int64}}:
1
2
3
If you want the indices ordered, you can do a sort on that:
julia> b = ['b', 'a', 'c'];
julia> indexin(b, a)
3-element Vector{Union{Nothing, Int64}}:
2
1
3
julia> indexin(b, a) |> sort
3-element Vector{Union{Nothing, Int64}}:
1
2
3
You can also try this comprehension-style way
# data
julia> show(A)
[9, 7, 4, 7, 8, 3, 1, 10, 4, 10]
julia> show(B)
[10, 8, 3, 7]
julia> sort([A[findall(i.==A)] for i in B])
4-element Vector{Vector{Int64}}:
[3]
[7, 7]
[8]
[10, 10]

Inserting values into a Python list

I am attempting to insert items into a list based on the index found in list a.
a = [2,1,0]
b = ['c','b','a']
c =[]
for (number,letter) in zip(a,b):
c.insert(number,[number,letter])
print(c)
This outputs: [[0, 'a'], [2, 'c'], [1, 'b']]
But I expected: [[0, 'a'], [1, 'b'], [2, 'c']]
Why does this happen?
Here's what actually happens inside your loop.
>>> c = []
>>> c.insert(2,[2,'c'])
>>> c
[[2, 'c']]
>>> c.insert(1,[1,'b'])
>>> c
[[2, 'c'], [1, 'b']]
>>> c.insert(0,[0,'a'])
>>> c
[[0, 'a'], [2, 'c'], [1, 'b']]
As you can see, the position your values are inserted at is relative to the contents of the list at the time of insertion. I'd recommend just sorting the zipped list.

function that takes variable length as argument and returns tuple

I have written code as below:
def convTup(*args):
t = set([])
for i in args:
t.add(i)
return tuple(t)
print convTup('a','b','c','d')
print convTup(1,2,3,4,5,6)
print convTup('a','b')
Expected output :
('a', 'b', 'c', 'd')
(1, 2, 3, 4, 5, 6)
('a', 'b')
But I got output as below:
('a', 'c', 'b', 'd')
(1, 2, 3, 4, 5, 6)
('a', 'b')
Why has the order of the elements changed only for ('a','b','c','d')? How can I print the tuple in the same order as the given input?
You can use this and you'll have a tuple sequence as your input
>>> def f(*args):
p = []
[p.append(x) for x in args if x not in p]
return tuple(p)
>>> f(1, 1, 2, 3)
(1, 2, 3)
>>> f('a', 'b', 'c', 'd')
('a', 'b', 'c', 'd')
This function will create a list of unique elements and track their order then return it as a tuple.
You can see the same functionality using a set instead of list. A set doesn't keep track of the order the elements were entered.
>>> def tup1(*args):
l = {x for x in args}
return tuple(l)
>>>
>>> tup1('a', 'b', 'c', 'd')
('a', 'c', 'b', 'd')
You can implement your own SortedSet collection if you use it in multiple places.
This should do what you need:
def convTup(*args):
return sorted(tuple(set(args)), key=lambda x: args.index(x))
Sou you convert args into set that is ordered by default, then turn it into tuple and finally sort that tuple by the original order.
So, to be precise, this function is ordering by order of appearance taking into account only the first appearance of element in args.

Nested list remove duplicate list

In a nested list (like the one below) that is continuous,
I want to remove duplicate entry where first and third are equal values. What is the most efficient way of doing this?
[[a, 1, a], [b, 1, b], [c, 2, d],[e, 4,g]
Return
[[c, 2, d],[e, 4,g]]
>>> seq = [['a', 1, 'a'], ['b', 1, 'b'], ['c', 2, 'd'],['e', 4, 'g']]
>>> seq = [item for item in seq if item[0] != item[2]]
>>> print seq
[['c', 2, 'd'], ['e', 4, 'g']]
What you want to do is go through each sublist, and go through each item in that sublist. I there is a duplicate item in that sublist set the flag to True and ignore it, if not then append that list to a new list.
lists = [['a', 1, 'a'], ['b', 1, 'b'], ['c', 2, 'd'],['e', 4,'g']]
newLists = []
for l in lists:
if l[0] != l[len(l) - 1]:
newLists.append(l)
print newLists

Python - Compare two lists in a comprehension

I'm trying to understand how comprehensions work.
I would like to loop through two lists, and compare each to find differences.
If one/or-more word(s) is different, I would like to print this word(s).
I'd like this all in one nice line of code, which is why I'm interested in comprehensions.
Doing it in "one nice line of code" is code golf, and misguided. Make it readable instead.
for a, b in zip(list1, list2):
if a != b:
print(a, "is different from", b)
This is not different in any significant way from this:
[print(a, "is different from", b) for a, b in zip(list1, list2) if a!=b]
Except that the expanded version easier to read and understand than the comprehension.
Like kriegar suggested using sets is probably the easiest solution. If you absolutely need to use list comprehension, I'd use something like this:
list_1 = [1, 2, 3, 4, 5, 6]
list_2 = [1, 2, 3, 0, 5, 6]
# Print all items from list_1 that are not in list_2 ()
print(*[item for item in list_1 if item not in list_2], sep='\n')
# Print all items from list_1 that differ from the item at the same index in list_2
print(*[x for x, y in zip(list_1, list_2) if x != y], sep='\n')
# Print all items from list_2 that differ from the item at the same index in list_1
print(*[y for x, y in zip(list_1, list_2) if x != y], sep='\n')
If you want to compare two lists for differences, I think you want to use a set.
s.symmetric_difference(t) s ^ t new set with elements in either s or t but not both
example:
>>> L1 = ['a', 'b', 'c', 'd']
>>> L2 = ['b', 'c', 'd', 'e']
>>> S1 = set(L1)
>>> S2 = set(L2)
>>> difference = list(S1.symmetric_difference(S2))
>>> print difference
['a', 'e']
>>>
one-line form?
>>> print list(set(L1).symmetric_difference(set(L2)))
['a', 'e']
>>>
if you really want to use a list comprehension:
>>> [word for word in L1 if word not in L2] + [word for word in L2 if word not in L1]
['a', 'e']
much less efficient as the size of the lists grow.