如何在Python中比较两个列表中的值?

2024-04-27 11:45:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两份清单:

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'],
]

我有一个检查以下内容的代码:

  1. 对于摩洛哥,它检查{}中的{}是{}中的{}
  2. 对于西班牙,它检查{}中的{}是{}中的{}
  3. 对于意大利,它检查index[2] in my_list 是什么index[3] my_values == 0,67

现在我有一个问题,正如你所看到的,{}在{}中存在于摩洛哥和西班牙,我只想让它检查一下摩洛哥

这是我的代码:

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

这是我的输出:

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

这是我喜欢的输出:

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

正如您所看到的,我希望{}中的{}只用于{},而{}用于{}等。。。请注意,在我的Office数据集中my_lists包含了更多的国家

#添加。我甚至尝试了熊猫,但仍然收到了相同的输出。

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']

Tags: 代码indfforindexmyanotherlist
3条回答

使用带有迭代器的原始数据结构

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
    

用法

# 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']

如果我正确理解您的要求,您希望循环列表中的国家,同时循环其他列表中的索引

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)

如果您的国家/地区多于my_values中的值,这显然会抛出一个IndexError

也许更好的方法是将my_list转换为dict,其中键是国家名称,值是该国家的值

我建议使用字典,然后过滤数据集

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']

作为补充说明,如果您使用的是更大的数据集,那么查看pandas包可能会有所帮助,这是一个用于数据分析的流行Python库

相关问题 更多 >