'Python Elements of list without substrings of another list
I'm not a programmer, but I have a great desire to learn python. So, I hope you indulge me. My Problem: I need two new lists derived from lists A and B:
list elements with substrings in B (found in stackoverflow) and,
list elements without substrings in B. By the way, each time a new A list is produced in my app, elements without "B list substrings" can change in A list. New list can't have elements changed upper or lower case because I'll use their names the way they are.
A = ['EEG FP1-LE', 'EEG FP2-LE', 'EEG F3-LE', 'EEG F4-LE', 'EEG PG1-LE','EEG PG2-LE', 'EEG EKG-LE', 'PHOTIC PH']
B = ['Fp1', 'F3', 'Fp2','F4']
from How to compare two lists to keep matching substrings?
with_substrings =[[a for a in A if b in a][0] if any(b in a for a in A) else b for b in B]
print(with_substrings)
returns:
['Fp1', 'EEG F3-LE', 'Fp2', 'EEG F4-LE'] # and thats great!
However, I dont understand why 'EEG FP1-LE' and 'EEG FP2-LE' become 'Fp1' and 'Fp2', while 'EEG F3-LE' and 'EEG F4-LE' are the same.
Secondly, I need another list with elements without substrings in "B".
without_substrings = code??
without_substrings = ['EEG PG1-LE','EEG PG2-LE', 'EEG EKG-LE', 'PHOTIC PH']
I really appreciate any help and suggestions of a good list comprehension tutorial.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
