簡體   English   中英

如何在 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_list中的第 3 行, index[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))

Output:

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"是換行符

編輯:

如果您希望 output 在列表中,只需執行以下操作:

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

將其轉換為 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'])

output:

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

我建議將該列表遷移到字典列表,它應該使您更容易訪問值:

將您的列表更改為:

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 = [
    ['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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM