This is documented under Structural Pattern Matching
Like unpacking assignments, tuple and list patterns have exactly the same meaning and actually match arbitrary sequences. Technically, the subject must be a sequence. Therefore, an important exception is that patterns don’t match iterators. Also, to prevent a common mistake, sequence patterns don’t match strings.
and in PEP 635 -- Structural Pattern Matching: Motivation and Rationale
Answer from user459872 on Stack OverflowAs in iterable unpacking, we do not distinguish between 'tuple' and 'list' notation.
[a, b, c],(a, b, c)anda, b, care all equivalent. While this means we have a redundant notation and checking specifically for lists or tuples requires more effort (e.g. caselist([a, b, c])), we mimic iterable unpacking as much as possible.
Videos
It's a pretty nifty feature and it's a much easier to extend or extend, like selectively flattening some values in a dictionary based on the key, for instance. I've written about it extensively on Mastering Structural Pattern Matching
a good way to do this is to have a function that will extract the information you need from the tuple list i.e.
def get_address_info(address_data):
address_number = None
street_name_pre_directional = None
street_name_post_directional = None
for value, property_name in address_data:
if property_name == "AddressNumber":
address_number = value
elif property_name == "StreetNamePreDirectional":
street_name_pre_directional = value
elif property_name == "StreetNamePostDirectional":
street_name_post_directional = value
return address_number, street_name_pre_directional, street_name_post_directional
that way you dont have to compare the items with each another, and the maintenance is easier
now you can get the necessary data and compare them directly and determine whether some information is missing by checking whether None is present in the info
add1_info = get_address_info(add1)
add2_info = get_address_info(add2)
if add1_info == add2_info and None not in add1_info and None not in add2_info :
#do something...
(Apart from the issue @glibdud pointed out) The way to go, I think, is to use set.
s1 = set(address1)
s2 = set(address2)
common = s1.intersection(s2)
Of course you can use more set operations to determine whether the overlap is enough evidence of being similar, but we do not have that information on your data.
In this case, it will output:
s1.intersection(s2)
Out[31]: {('Lake', 'PlaceName'), ('MI', 'StateName'), ('town,', 'PlaceName')}
In the end, I get the feeling from the definition of the question where one of the two can contain multiple addresses that you want to know whether an address is listed in your address register. In that case instead of intersection you'll want to use s1.issuperset(s2) or s2.issubset(s1), which returns you a nice bool to work with.
s1.issuperset(s2)
>> False
You didn't make the RegEx ungreedy. The solution is re.findall(r'\((.*?,.*?)\)',s).
Alternatives. First one uses a complement match often used as an alternative to non-greedy search where it is not available.
>>> re.findall(r'\(([^)]*)\)',s)
['aleakedteaserand, NN', 'abehind, IN', 'the, DT']
>>> re.split('\), \(', s.strip('[()]'))
['aleakedteaserand, NN', 'abehind, IN', 'the, DT']
No regex
>>> s.strip('[()]').split('), (')
['aleakedteaserand, NN', 'abehind, IN', 'the, DT']
You were close, after iterating the inner loop, you should check whether the item from the outer loop is actually equal to the tup[1] (each tup represent (0, 'a') or (1, 'b') for example).
if they equal, just append the first element in tup (tup[0]) to the result list.
lst = ['a', 'a', 'b']
catlist = [(0, 'a'), (1, 'b')]
catindexes = []
for item in lst:
for tup in catlist:
if item == tup[1]:
catindexes.append(tup[0])
print (catindexes)
You also can use list comprehension:
catindexes = [tup[0] for item in lst for tup in catlist if tup[1] == item]
>>> lst = ['a', 'a', 'b']
>>> catlist = [(0, 'a'), (1, 'b')]
>>> catindexes = []
>>> for item in lst:
... for i in catlist:
... if i[1] == item:
... catindexes.append(i[0])
...
>>> catindexes
[0, 0, 1]
During the iteration, i is a direct reference to an element of catlist, not its index. I'm not using i to extract an element from lst, the for ... in ... already takes care of that. As i is a direct reference to a tuple, I can simply extract the relevant fields for matching and appending without the need to mess with the indexing of lst.