如何在Python中比较两个列表?
我有以下两个清单如何在Python中比较两个列表?,python,list,lambda,itertools,Python,List,Lambda,Itertools,我有以下两个清单 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', '
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_value中的一个值。
index[2]
是my_list
中的第三行,index[4]
是my_list中的第四行
对于摩洛哥,我想检查一下索引[2]
是什么索引[3]
=0,78
对于西班牙,我想检查一下索引[2]
是什么索引[3]
=0,40
对于意大利,我想检查一下索引[2]
是什么索引[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\u列表
中的列表上循环,并使用索引[3]
检查每个子列表中的值,然后重新输入my\u值
列表
- 如果条件为
True
,则它“保留”索引[2]
- 最后,它连接所有与上述匹配的
值,例如189,01
- 顺便说一下,如果我的_值中的i[3],则我的_列表中的i的
i[2]是一个生成器
“\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']
这是你想要的吗
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\u列表
中的列表上循环,并使用索引[3]
检查每个子列表中的值,然后重新输入my\u值
列表
- 如果条件为
True
,则它“保留”索引[2]
- 最后,它连接所有与上述匹配的
值,例如189,01
- 顺便说一下,如果我的_值中的i[3],则我的_列表中的i的
i[2]是一个生成器
“\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']
你可以这样做-
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 = [
['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))
我建议将该列表迁移到字典列表中,这样会使您更容易访问值:
将列表更改为以下内容:
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))
将其转换为dataframe并使用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']
将其转换为dataframe并使用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']
什么是索引[2]
和索引[3]
?哦,你是说每个元素的第三和第四个元素sublist@Capie索引[2]
是我的列表中的第3行,而索引[4]是我的列表中的第4行
什么是索引[2]
和索引[3]
?哦,你是说每个元素的第三和第四个元素sublist@Capie索引[2]
是我的列表
中的第3行,而索引[4]是我的列表
中的第4行,这是一个非常强的一行。我认为它能起作用。你能详细地告诉我你到底做了什么,这样我就知道如果我的数据发生了变化,该怎么想了?我已经更新了我的答案。'\n'
在你的代码中做了什么?顺便说一句我希望输出是在一个列表,这是一个非常强大的一行。我认为它能起作用。你能详细地告诉我你到底做了什么,这样我就知道如果我的数据发生了变化,该怎么想了?我已经更新了我的答案。'\n'
在你的代码中做了什么?顺便说一句我想将输出放在列表中如果我想将其放在列表中怎么办?@TangerCity将打印替换为列表即可。我已经编辑了你喜欢的答案。如果我想把它放在一个列表中怎么办?@TangerCity用列表代替打印。我已经根据你的喜好编辑了答案。