Tan*_*ity 1 python lambda list python-itertools
我有以下 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']]
Run Code Online (Sandbox Code Playgroud)
现在对每一个列表中my_list我要检查什么index[2]是index[3]相等的值my_values. index[2]是这是第3排my_list,并index[4]在第4行my_list这么短说:
index[2]是index[3]==0,78index[2]是index[3]==0,40index[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)
Run Code Online (Sandbox Code Playgroud)
这是我收到的:
ValueError: min() arg is an empty sequence
Run Code Online (Sandbox Code Playgroud)
这是我的预期:
188,49
189,01
188,61
Run Code Online (Sandbox Code Playgroud)
这是你想要的吗?
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))
Run Code Online (Sandbox Code Playgroud)
输出:
188,49
189,01
188,61
Run Code Online (Sandbox Code Playgroud)
这是单线的作用:
my_list和检查从各子表与索引值[3]反对票的my_value列表True,它“保留”索引中的值[2]values与上述匹配的,例如189,01i[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)
Run Code Online (Sandbox Code Playgroud)
这给你:
['188,49', '189,01', '188,61']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
101 次 |
| 最近记录: |