If you only want to know if any item of d is contained in paid[j], as you literally say:
if any(x in paid[j] for x in d): ...
If you also want to know which items of d are contained in paid[j]:
contained = [x for x in d if x in paid[j]]
contained will be an empty list if no items of d are contained in paid[j].
There are other solutions yet if what you want is yet another alternative, e.g., get the first item of d contained in paid[j] (and None if no item is so contained):
firstone = next((x for x in d if x in paid[j]), None)
BTW, since in a comment you mention sentences and words, maybe you don't necessarily want a string check (which is what all of my examples are doing), because they can't consider word boundaries -- e.g., each example will say that 'cat' is in 'obfuscate' (because, 'obfuscate' contains 'cat' as a substring). To allow checks on word boundaries, rather than simple substring checks, you might productively use regular expressions... but I suggest you open a separate question on that, if that's what you require -- all of the code snippets in this answer, depending on your exact requirements, will work equally well if you change the predicate x in paid[j] into some more sophisticated predicate such as somere.search(paid[j]) for an appropriate RE object somere.
(Python 2.6 or better -- slight differences in 2.5 and earlier).
If your intention is something else again, such as getting one or all of the indices in d of the items satisfying your constrain, there are easy solutions for those different problems, too... but, if what you actually require is so far away from what you said, I'd better stop guessing and hope you clarify;-).
If you only want to know if any item of d is contained in paid[j], as you literally say:
if any(x in paid[j] for x in d): ...
If you also want to know which items of d are contained in paid[j]:
contained = [x for x in d if x in paid[j]]
contained will be an empty list if no items of d are contained in paid[j].
There are other solutions yet if what you want is yet another alternative, e.g., get the first item of d contained in paid[j] (and None if no item is so contained):
firstone = next((x for x in d if x in paid[j]), None)
BTW, since in a comment you mention sentences and words, maybe you don't necessarily want a string check (which is what all of my examples are doing), because they can't consider word boundaries -- e.g., each example will say that 'cat' is in 'obfuscate' (because, 'obfuscate' contains 'cat' as a substring). To allow checks on word boundaries, rather than simple substring checks, you might productively use regular expressions... but I suggest you open a separate question on that, if that's what you require -- all of the code snippets in this answer, depending on your exact requirements, will work equally well if you change the predicate x in paid[j] into some more sophisticated predicate such as somere.search(paid[j]) for an appropriate RE object somere.
(Python 2.6 or better -- slight differences in 2.5 and earlier).
If your intention is something else again, such as getting one or all of the indices in d of the items satisfying your constrain, there are easy solutions for those different problems, too... but, if what you actually require is so far away from what you said, I'd better stop guessing and hope you clarify;-).
I assume you mean list and not array? There is such a thing as an array in Python, but more often than not you want a list instead of an array.
The way to check if a list contains a value is to use in:
if paid[j] in d:
# ...
I have a case where two lists might looks like this
A = [ABC, DEF, GHI] B = [ABC, LMN]
And I want to return TRUE if any of list A is in list B. So the above case should be TRUE but the below case should be false.
A = [ABC] B = [BCD]
All methods I find online would result in the second case being TRUE because the letters 'B' and 'C' are shared in the two lists. However, I want to treat the string as a whole and not compare individual letters.
Is there a way to do this? Appreciate any help!
Python comparing a string or list of strings to a string or list - Code Review Stack Exchange
Python compare part of string to list - Stack Overflow
Faster Way of Comparing String to Large List of Strings
How do I compare string in an list?
Videos
Like this, you can use a list comprehension:
mylist = ["770", "790", "1470", "1490"]
sq = "BPBA-SG790-NGTP-W-AU-BUN-3Y"
matching = [m for m in mylist if m in sq]
print(matching)
Output:
['790']
You can use the in keyword from python:
mylist = ["770", "790", "1470", "1490"]
sq = "BPBA-SG790-NGTP-W-AU-BUN-3Y"
for i in mylist:
if i in sq:
print(i)
The code iterates through the list and prints the list element if it is in the string
List = ['bro', 'huh', 'man', 'huh', 'bro']
I am new to python. Basically, I want to write if the first string like 'bro' == to the last string "bro" then print (True) if it its equal. Vice versa for the "huh".
This is a job for sets
Convert your lists of lists to sets of tuples (you can't have sets of lists, as sets can only contain hashable objects, which lists, as all built-in mutable objects, are not)
a = set(map(tuple, [['abc','Hello World'],['bcd','Hello Python']]))
b = set(map(tuple, [['abc','Hello World'],['bcd','Hello World'],['abc','Python World']]))
or create them directly as sets of tuples:
a = {('abc','Hello World'),('bcd','Hello Python')}
b = {('abc','Hello World'),('bcd','Hello World'),('abc','Python World')}
You can then easily and efficiently get your differences:
print(b - a)
# {('abc', 'Python World'), ('bcd', 'Hello World')}
print(a - b)
# {('bcd', 'Hello Python')}
or even the intersection
print(a & b)
# {('abc', 'Hello World')}
or the union:
print(a | b)
# {('abc', 'Python World'), ('bcd', 'Hello World'), ('abc', 'Hello World'), ('bcd', 'Hello Python')}
If you want to have a quick solution without much concerns about performance you can use
b_not_in_a = [i for i in b if i not in a]
a_not_in_b = [i for i in a if i not in b]
s = ["this", "this", "and", "that"]
for i in xrange(1,len(s)):
if s[i] == s[i-1]:
print s[i]
EDIT:
Just as a side note, if you are using python 3.X use range instead of xrange
strings = ['this', 'this', 'and', 'that']
for a, b in zip(strings, strings[1:]):
if a == b:
print a
Use set intersection for this:
list(set(listA) & set(listB))
gives:
['a', 'c']
Note that since we are dealing with sets this may not preserve order:
' '.join(list(set(john.split()) & set(mary.split())))
'I and love yellow'
using join() to convert the resulting list into a string.
--
For your example/comment below, this will preserve order (inspired by comment from @DSM)
' '.join([j for j, m in zip(john.split(), mary.split()) if j==m])
'I love yellow and'
For a case where the list aren't the same length, with the result as specified in the comment below:
aa = ['a', 'b', 'c']
bb = ['c', 'b', 'd', 'a']
[a for a, b in zip(aa, bb) if a==b]
['b']
If order doesn't matter, but you want to print out a nice diff:
def diff(a, b):
if len(set(a) - set(b)) > 0:
print(f"Items removed: {set(a) - set(b)}")
if len(set(b) - set(a)) > 0:
print(f"Items added: {set(b) - set(a)}")
if set(a) == set(b):
print(f"They are the same")
diff([1,2,3,4], [1,2,3])
# Items removed: {4}
diff([3,4], [1,2,3])
# Items removed: {4}
# Items added: {1, 2}
diff([], [1,2,3])
# Items added: {1, 2, 3}
diff([1,2,3], [1])
# Items removed: {2, 3}
listA = ['a', 'b', 'c']
listB = ['a', 'h', 'c']
diff(listA, listB)
# Items removed: {'b'}
# Items added: {'h'}
john = 'I love yellow and green'
mary = 'I love yellow and red'
diff(john, mary)
# Items removed: {'g'}