Cheng
Cheng

Reputation: 11

Find the same elements from two lists and print the elements from both lists

There are two lists:

k = ['a', 'a', 'b', 'b', 'c', 'c', 'd', 'e']
l = ['a', 'c', 'e']

I want to find the same elements from these two lists, that is: ['a', 'c', 'e']

then I want to print out the element we found, for example, 'a' from both lists, that is: ['a', 'a', 'a'].

The result I want is as follows:

['a', 'a', 'a', 'c', 'c', 'c', 'e', 'e']

I try to doing in this way:

c = []
for item_k in k:
   for item_j in j:
      if item_k== item_j:
          c.append(item_k)
      c.append(item_j)

However, the result is ['a', 'a', 'c', 'c', 'e', 'e']

Also in this way:

c=[]
for item_k in k:
   if item_k in l:
      c.append(item_k)
      d=l.count(item_k)
      c.append(item_k*d)
      print c

But it do not works, can anybody tell me how to do it? really appreciate your help in advance

Upvotes: 1

Views: 225

Answers (6)

rlbond
rlbond

Reputation: 67749

result = [x for x in sorted(k + l) if x in k and x in l]
print(result)

results:

['a', 'a', 'a', 'c', 'c', 'c', 'e', 'e']

Upvotes: 4

Gnarlywhale
Gnarlywhale

Reputation: 4220

Here is an implementation that matches your initial algorithm:

k = ['a', 'a', 'b', 'b', 'c', 'c', 'd', 'e']
l=['a', 'c', 'e']
c=[]
for x in l:
    count = 0
    for y in k:
        if x == y:
            count += 1
    while count>=0:
        c.append(x)
        count = count -1

print c

Upvotes: 1

Rob Foley
Rob Foley

Reputation: 581

Here's a compact way. Readability might suffer a little, but what fun are comprehensions without a little deciphering?

import itertools
k = ['a', 'a', 'b', 'b', 'c', 'c', 'd', 'e']
l = ['a', 'c', 'e']

combined = [letter for letter in itertools.chain(k,l) if letter in l and letter in k]

Upvotes: 1

Michael Neylon
Michael Neylon

Reputation: 624

You can use set operations to intersect and then loop through, appending to a new list any that match the intersected list

k = ['a', 'a', 'b', 'b', 'c', 'c', 'd', 'e']
l = ['a', 'c', 'e']

common_list = list(set(k).intersection(set(l)))

all_results = []

for item in k:
    if item in common_list:
        all_results.append(item)

for item in l:
    if item in common_list:
        all_results.append(item)

print sorted(all_results)

output:

['a', 'a', 'a', 'c', 'c', 'c', 'e', 'e']

Upvotes: 1

John Coleman
John Coleman

Reputation: 51998

If the lists are sorted and you want the result to be sorted:

sorted([x for x in list1 if x in set(list2)] + [x for x in list2 if x in set(list1)] )

Upvotes: 1

MatsLindh
MatsLindh

Reputation: 52792

Since you want to pick up elements from both lists, the most straight forward way is probably to iterate over both while checking the other one (this is highly optimizatiable if you depend on speed for doing this):

merged = []

for el in list1:
    if el in list2:
        merged.append(el)

for el in list2:
    if el in list1:
        merged.append(el)

.. if the order of the elements is important, you'll have to define an iteration order (in what order do you look at what element from what array?).

Upvotes: 1

Related Questions