0

the below code attempts to scan all elements of mag for all elements of note. If all UNIQUE elements of note can be found in mag, then it should print YES .. else it should print NO. The check for elements is supposed to be case sensitive.

mag =['two', 'times', 'three', 'is', 'not', 'four', 'two']
note =['two', 'times', 'two', 'is', 'four']
r = ""
x=[]
for i in mag:
    for j in note:
        if j in i and j not in x:
            x.append(j)
    if len(x)==len(note):
        r = "YES, all note elements can be found in mag"
    else:
        r = "NO, all note elements cannot be found in mag"
print(r)
print (" ".join(map(str, x)))

When i run the code, i get r = "NO, all n elements cannot be found in mag" .. but that should not be the case because the frequency of element "two" is 2 in both list(mag) and list(note)

What I want to achieve: I want to compare whether each unique element in note can be found in mag. e.g. if mag=["a", "b", "c", "d"] and note = ["b", "a", "c"] .. it will return TRUE. but if mag=["a", "b", "c", "d"] and note = ["b", "a", "a" "c"] then it return false because "a" occurs twice in note but once in mag

3 Answers3

0

An easy ad-hoc way to check whether all elements of list1 are in list2 is to use

def check_subset(list1, list2):
    for item in list1:
        if item not in list2:
            return False
    return True

Or as a oneliner using list comprehension:

all([item in list2 for item in list1])

A better way would be to use sets, e.g. see here: Python - verifying if one list is a subset of the other

Fabian Ying
  • 1,216
  • 1
  • 10
  • 15
  • You didn't read the question carefully -- if a word occurs twice in one list then he wants to check that it also occurs at least twice in the other, and so on. – RemcoGerlich Nov 29 '17 at 10:59
  • thanks !! But it failed one test case. code returns "YES" .. but the frequency of 'two' in note is not equal to the frequency of 'two' in mag. mag =['two', 'times', 'three', 'is', 'not', 'four'] new =['two', 'times', 'two', 'is', 'four'] r = "" for item in new: if item not in mag: r = "NO, all n elements cannot be found in m" else: r = "YES, all n elements can be found in m" print(r) –  Nov 29 '17 at 11:02
  • I have checked the sets, unfortunately it compares the length of one set to another. But I want to compare whether each unique element in note can be found in mag. e.g. if mag=["a", "b", "c", "d"] and note = ["b", "a", "c"] .. it will return TRUE. but if mag=["a", "b", "c", "d"] and note = ["b", "a", "a" "c"] then it return false because "a" occurs twice in note but once in mag –  Nov 29 '17 at 11:16
0

Use a Counter to count how many times each word occurs in each, and then I believe your check comes down to the question of whether the counter of note is smaller than the counter of mag, but unfortunately Counters don't implement <= as I thought. You have to compare them by hand:

from collections import Counter

counter_note = Counter(note)
counter_mag = Counter(mag)

if all(counter_mag.get(word, 0) >= count for word, count in counter_note.items()):
    r = "YES, all note elements can be found in mag"
else:
    r = "NO, all note elements cannot be found in mag"
RemcoGerlich
  • 30,470
  • 6
  • 61
  • 79
  • Thanks! but I get a type error "TypeError: '<=' not supported between instances of 'Counter' and 'Counter'" –  Nov 29 '17 at 11:04
  • this is working. But there is a problem each time i change an element and run the program, it returns the right answer only after running a second time. Im not sure what is causing this. e.g. the current list for note and mag returns True, but if i delete one occurrence of element 'two' from mag, and i run program, for the first run time it will return true, then i run same program a second time before it retuns false(which will be the right answer) –  Nov 29 '17 at 12:15
  • That's something odd with how you're running the program. Normally "running the program" should mean that there is no memory of any previous runs. – RemcoGerlich Nov 29 '17 at 12:18
0

Not a one-liner, because you have to identify the shorter list first. If you always know, that note has less elements than mag, then you can skip this test.

a = [note, mag][len(mag) > len(note)]         #more list elements
b = [note, mag][mag != a]                     #less list elements

print(all([a.count(item) >= b.count(item) for item in set(b)]))

Output

mag =['two', 'times', 'three', 'is', 'not', 'four', 'two']
note =['two', 'times', 'two', 'is', 'four']
>>>True

mag =['two', 'times', 'three', 'is', 'not', 'five']
note =['two', 'times', 'two', 'is', 'four']
>>>False
Mr. T
  • 11,960
  • 10
  • 32
  • 54
  • 1
    This is perfect with less complexity!!. but it has failed some test cases –  Nov 29 '17 at 12:27
  • Can you give an example that failed? Just so I see, what I missed. – Mr. T Nov 29 '17 at 12:35
  • mag = ['apgo', 'clm', 'w', 'lxkvg', 'mwz', 'elo', 'bg', 'elo', 'lxkvg', 'elo', 'apgo', 'apgo', 'w', 'elo', 'bg'] note = ['elo', 'lxkvg', 'bg', 'mwz', 'clm', 'w'] it returns FALSE .. but all elements of note are inside mag –  Nov 29 '17 at 12:45
  • How stupid of me. Of course. Changed now `==` to `>=`. Sorry for the confusion. – Mr. T Nov 29 '17 at 12:51