Home > Software engineering >  compare one list elements with another list elements
compare one list elements with another list elements

Time:10-21

My use case is if list2 elements are in list1 then add them to a new list3 and do the set function on list3 and if length is ==2 then print it as partial if length greater than 2 then multiple . I want to add the option column in the excel below. Below excel file can print the 1st and second column but for the `option' i'm not getting the logic on how to do it. can someone help me with that? Thanks

CodePudding user response:

I'm not 100% sure I follow the second half of your question, but here's what you could do for the first part:

list3 = set([x for x in list2 if x in list1])

CodePudding user response:

Not very optimised way but this will work

res = set()
for i in list1:
    for j in list2:
        if j in i:
            res.add(i)
if len(res) == 2:
    print("partial")
if len(res) > 2:
    print("multiple")

optimised

Here I am assuming that your list1 will contain the searching character between two slashes or after :

d_list2 = dict(zip(list2,list2 ))

res = set(x.split("/")[1] if "/" in x else x.split(":")[1]  for x in list1 )
res = set(d_list2.get(x) for x in res)
if len(res) == 2:
    print("partial")
if len(res) > 2:
    print("multiple")
  • Related