如何在 Python 中比较 2 个列表?

How to compare 2 lists with each other in Python?

我有以下 2 个列表。

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

现在对于 my_list 中的每个列表,我想检查 index[2] 是什么 index[3] 等于 my_values. 中的值 index[2] 是第三个my_listindex[4] 中的行是 my_list 中的第 4 行 简而言之:

  1. 对于摩洛哥,我想检查 index[2] 是什么 index[3] == 0,78
  2. 对于西班牙,我想检查 index[2] 是什么 index[3] == 0,40
  3. 对于意大利,我想检查 index[2] 是什么 index[3] == 0,67

这是我试过的代码:

my_answers = []
for key,sublists in itertools.groupby(my_list,lambda y:y[0]): 
    v = min(x for x in sublists if float(x[3].replace(',', '.')) == x for x in my_values); 
    my_answers.append(v[-2])
    
print(my_answers)

这是我收到的:

ValueError: min() arg is an empty sequence

这是我所期望的:

188,49
189,01
188,61

这是你想要的吗?

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,85'],
    ['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("\n".join(i[2] for i in my_list if i[3] in my_values))

输出:

188,49
189,01
188,61

单行线的作用如下:

  • 循环遍历 my_list 中的列表并检查每个子列表中索引为 [3] 的值与 my_value 列表
  • 如果条件是 True,它“保留”索引 [2]
  • 中的值
  • 最后它加入了所有符合上述条件的 values,例如189,01
  • 顺便说一下,i[2] for i in my_list if i[3] in my_values 是一个生成器
  • "\n"是换行符

编辑:

如果您希望输出在列表中,只需这样做:

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

这给你:

['188,49', '189,01', '188,61']
for i in my_list:
     if i[3] in my_values:
         print(i[2])

你可以这样操作-

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,85'],
    ['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']]
my_answers = []
for U_list in my_list:
    if U_list[0] == "Morocco" and U_list[3] == "0,78":
        my_answers.append(U_list[2])
    if U_list[0] == "Spain" and U_list[3] == "0,40":
        my_answers.append(U_list[2])
    if U_list[0] == "Italy" and U_list[3] == "0,67":
        my_answers.append(U_list[2])
print(my_answers)

我会建议将该列表迁移到字典列表,它应该使您更容易访问值:

将您的列表更改为:

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

然后您可以简单地:

for record in my_list:
    if record['num2'] in my_values:
        print(f"{record['num2']} {record['num1']}")

或单线:

print("\n".join(f"{record['num2']} {record['num1']}" for record in my_values if record['num2'] in my_values))

将其转换为数据帧并使用np.where

import pandas as pd
import numpy as np

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

输出:

my_out_list
Out[18]: ['188,49', '189,01', '188,61']