min2bro
min2bro

Reputation: 4638

Match tuple values within the list

Compare tuples within a list, I have list which contains details of the address, and i need to compare if two address's are same or not,

address1= [('1546-1711', 'AddressNumber'),
  ('South', 'StreetNamePreDirectional'),
  ('4650', 'AddressNumber'),
  ('West,', 'StreetNamePostDirectional'),
  ('Green', 'PlaceName'),
  ('Lake', 'PlaceName'),
  ('town,', 'PlaceName'),
  ('MI', 'StateName'),
  ('84107', 'ZipCode')]

 address2 = [('800', 'AddressNumber'),
  ('Green', 'StreetName'),
  ('Lake', 'PlaceName'),
  ('town,', 'PlaceName'),
  ('MI', 'StateName'),
  ('84110', 'ZipCode')]

There are some attibutes which are missing in second address like 'StreetNamePreDirectional' and 'StreetNamePostDirectional', so i have to compare on the remaining attributes whichever is present, so first i need to check what attributes are common between both and then compare if they have matching values, what's the pythonic way to do it

My code so far:

for item1 in add2:
    for item2 in add1:
        if(item2[1]=='AddressNumber' and item1[1]=='AddressNumber'):
            addressnumbermatch = True
        if(item2[1]=='StreetNamePreDirectional' == item1[1]=='StreetNamePreDirectional'):
            StreeNameDirectionMatch = True
        if(item2[1]=='StreetNamePreDirectional' == item1[1]=='StreetNamePreDirectional'):
            StreetNamePreDirectionalMatch = True

if(addressnumbermatch and StreeNameDirectionMatch and StreetNamePreDirectionalMatch ):
    # Do somthing

Note: There are multiple attibutes like AddressNumber and Place Name so it should match any one value out of multiple values. so first address contains two Address number so need to check if any of these two values matches with the Address number of Address2

Upvotes: 0

Views: 1379

Answers (2)

Uvar
Uvar

Reputation: 3462

(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

Upvotes: 1

Derte Trdelnik
Derte Trdelnik

Reputation: 2706

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...

Upvotes: 1

Related Questions