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]
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.
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?
here is my current list: [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
want to extract from the list and nested items using indexing and slicing, this is what I want to extract: [0, 2, 3, [5 ,6], 8, 10]
code so far:
list = [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
new_list = list[0], list[2], list[3], list[4]
print("new list is", new_list)
outputs this: new list is (0, [1, 2, 3, 4], [[5], [6, 7]], [8, 9, 10]), need to extrac tthe nexted items and format the list like this: [0, 2, 3, [5 ,6], 8, 10]
L = [0, [], [1,2,3,4], [[5],[6,7]], [8,9,10]]
new_list = [L[0], L[2][1], L[2][2], [L[3][0][0], L[3][1][0]], L[-1][-3], L[-1][-1]]
print("new list is", new_list)
print()
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.