TangerCity
TangerCity

Reputation: 855

How to compare values in two lists in Python?

I have 2 lists:

my_values = ['0,78', '0,40', '0,67']

my_list = [
    ['Morocco', 'Meat', '190,00', '0,15'], 
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Morocco', 'Meat', '187,99', '0,101'],
    ['Spain', 'Meat', '190,76', '0,10'], 
    ['Spain', 'Meat', '190,16', '0,20'], 
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,78'],
    ['Italy', 'Meat', '190,20', '0,11'],
    ['Italy', 'Meat', '190,10', '0,31'], 
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72'],
    ['Italy', 'Meat', '187,36', '0,80'],
]

I have a code which is checking the following:

  1. For Morocco it checks at what index[2] inmy_listisindex[3]in my_values == 0,78
  2. For Spain it checks at what index[2] inmy_list isindex[3]in my_values == 0,40
  3. For Italy it checks at what index[2] in my_list isindex[3]in my_values == 0,67

Now I have a problem, as you can see 0,78 in my_values is present in Morocco AND Spain, I only want it to check it for Morocco.

This is my code:

yet_another_list = [i[2] for i in my_list if i[3] in my_values]
print(yet_another_list)

This is my output:

['188,49', '189,01', '187,95', '188,61']

This is my prefered output:

['188,49', '189,01', '188,61']

As you can see I want index[1] of my_values to be only used for Morocco, and index[2] for Spain etc... Please note that in my officla dataset my_lists contains a lot more countries...

#ADDED. I even tried Pandas but still received the same output.

df=pd.DataFrame(my_list)
df['Filter']=np.where([i in my_values for i in df[3]],"Yes","")
my_out_list=list(df[2][df['Filter']=='Yes'])

print(my_out_list)

>> 
['188,49', '189,01', '187,95', '188,61']

Upvotes: 0

Views: 187

Answers (3)

DarrylG
DarrylG

Reputation: 17156

Using original data structures with iterators.

def get_values(my_list_, *my_values_):
    ''' Finds the desired result using my_list_ and my_values
        my_valuesis one or more list
    '''
    output = []
    # Find values for each list in my_values_
    for my_values__ in my_values_:
         # Create iterators 
        result = []
        my_values_iter = iter(my_values__) # iterator for current list of values
        my_list_iter = iter(my_list_)  # from beginning of my_list_

        v = next(my_values_iter, None)
        i = next(my_list_iter, None)
        while v and i:
            if v == i[3]:
                # found match
                result.append(i[2])
                v = next(my_values_iter, None) # Next value to find in my_values
                i = next(my_list_iter, None)   # Next value to check in my_list
            else:
                # try next value from my_list
                i = next(my_list_iter, None)   # Next value to check in my_list
        output.append(result)
        
     if len(output) == 1:
        return output[0]  # Only single list
    else:
        return tuple(x for x in output) # Output tuple of lists
    

Usage

# Single list of values
a = get_values(my_list, ['0,78', '0,40', '0,67'])
print(f'a = {a}') # Output: a = ['188,49', '189,01', '188,61']

# Two list of values (can handle an arbitrary number)
a, b = get_values(my_list, ['0,78', '0,40', '0,67'], ['0,78', '0,10', '0,78'])
print(f'a = {a}, b = {b}') # Output: a = ['188,49', '189,01', '188,61'], b = ['188,49', '190,76', '187,95']

Upvotes: 1

tripleee
tripleee

Reputation: 189397

If I understand your requirement correctly, you want to loop over countries in the list, and simultaneously loop over the indices in the other list?

previous = my_list[0][0]
ind = 0
result = []
for item in my_list:
    if item[0] != previous:
        ind += 1
        previous = item[0]
    if item[3] == my_values[ind]:
        result.append(item[2])
print(result)

This will obviously throw an IndexError if you have more countries than values in my_values.

Perhaps a better approach would be to turn my_list into a dict where the keys are country names and the values are the values for that country, though.

Upvotes: 0

ScootCork
ScootCork

Reputation: 3676

I'd recommend using a dictionary and then filtering your dataset

my_values = {'Morocco': '0,78', 'Spain': '0,40', 'Italy': '0,67'}

my_list = [
    ['Morocco', 'Meat', '190,00', '0,15'], 
    ['Morocco', 'Meat', '189,90', '0,32'], 
    ['Morocco', 'Meat', '189,38', '0,44'],
    ['Morocco', 'Meat', '188,94', '0,60'],
    ['Morocco', 'Meat', '188,49', '0,78'],
    ['Morocco', 'Meat', '187,99', '0,101'],
    ['Spain', 'Meat', '190,76', '0,10'], 
    ['Spain', 'Meat', '190,16', '0,20'], 
    ['Spain', 'Meat', '189,56', '0,35'],
    ['Spain', 'Meat', '189,01', '0,40'],
    ['Spain', 'Meat', '188,13', '0,75'],
    ['Spain', 'Meat', '187,95', '0,78'],
    ['Italy', 'Meat', '190,20', '0,11'],
    ['Italy', 'Meat', '190,10', '0,31'], 
    ['Italy', 'Meat', '189,32', '0,45'],
    ['Italy', 'Meat', '188,61', '0,67'],
    ['Italy', 'Meat', '188,01', '0,72'],
    ['Italy', 'Meat', '187,36', '0,80'],
]


print([e[2] for e in filter(lambda x: x[3] == my_values[x[0]], my_list)])

>>> ['188,49', '189,01', '188,61']

As a side note, if you're working with much larger data sets it might be beneficial to look at the pandas package, a popular Python library for data analysis

Upvotes: 1

Related Questions