如何在Python中使用itertools函数的索引

如何在Python中使用itertools函数的索引,python,list,if-statement,lambda,itertools,Python,List,If Statement,Lambda,Itertools,我有一个列表,其中包含多个示例列表,如下所示: import itertools stand_city = 11 stand_liverpool = 6.5 premier_league = [ ['A1','Manchester City', '10,1', 'Aguero'], ['A2','Manchester City', '11,2', 'Mahrez'], ['A3','Manchester City'

我有一个列表,其中包含多个示例列表,如下所示:

import itertools

stand_city = 11
stand_liverpool = 6.5

premier_league = [
             ['A1','Manchester City', '10,1', 'Aguero'],
             ['A2','Manchester City', '11,2', 'Mahrez'],
             ['A3','Manchester City', '13,5', 'Sterling'],
             ['B1','Liverpool', '4,5', 'Mane'],
             ['B2','Liverpool', '5,6', 'Salah'],
             ['B3','Liverpool', '7,2', 'Jota']]
现在,对于每个列表,我希望在其超过
stand\u city
stand\u liverpool
之前获得最后一个值。取决于列表中的索引[1]。如果它是曼城,我需要它使用曼城,如果它是利物浦,我需要它使用利物浦。我希望这些值存储在新列表中

这是我的代码:

new_list  = []
for key,sublists in itertools.groupby(premier_league,lambda y:y[0]):
    club = (list(sublists)[0][1])
    if club == 'Manchester City':
        v=[] 
        for v in itertools.takewhile(lambda x:float(x[-2].replace(",","."))<stand_city ,sublists):
            pass
        if v: 
            x = v[-1]
            new_list.append(x)
    elif club == 'Liverpool':
        v=[] 
        for v in itertools.takewhile(lambda x:float(x[-2].replace(",","."))<stand_liverpool ,sublists):
            pass
        if v: 
            x = v[-2]
            new_list.append(x)
            
print(new_list) 
[]
10,1
5,6
这是我想要的输出:

new_list  = []
for key,sublists in itertools.groupby(premier_league,lambda y:y[0]):
    club = (list(sublists)[0][1])
    if club == 'Manchester City':
        v=[] 
        for v in itertools.takewhile(lambda x:float(x[-2].replace(",","."))<stand_city ,sublists):
            pass
        if v: 
            x = v[-1]
            new_list.append(x)
    elif club == 'Liverpool':
        v=[] 
        for v in itertools.takewhile(lambda x:float(x[-2].replace(",","."))<stand_liverpool ,sublists):
            pass
        if v: 
            x = v[-2]
            new_list.append(x)
            
print(new_list) 
[]
10,1
5,6

我对你的代码做了一些小的修改,以得到你想要的结果

stand_city = 11
stand_liverpool = 6.5

premier_league = [
             ['A1','Manchester City', '10,1', 'Aguero'],
             ['A2','Manchester City', '11,2', 'Mahrez'],
             ['A3','Manchester City', '13,5', 'Sterling'],
             ['B1','Liverpool', '4,5', 'Mane'],
             ['B2','Liverpool', '5,6', 'Salah'],
             ['B3','Liverpool', '7,2', 'Jota']]

res = []
for g, value in groupby(premier_league, key = lambda x:x[1]): # group by according to index 1
    less_than = [] # temporary list to hold all values less than your threshold for a particular group
    for i in value: # iterate thorugh the values in each group
        float_i = float(i[2].replace(',', '.')) # convert index 2 to float
        to_compare = stand_city if g == 'Manchester City' else stand_liverpool
        if float_i < to_compare: # compare `float_i` with either `stand_city` or `stand_liverpool` based on group
            less_than.append(i[2]) # add all values that meet the condition to a temp list
    res.extend(less_than[-1:]) # take only the last element from the list for each group, `[-1:]` is done to prevent cases where `less_than` is an empty list
print(res) # ['10,1', '5,6']
stand_city=11
站姿=6.5
英超联赛=[
['A1'、'Manchester City'、'10,1'、'Aguero'],
['A2'、'Manchester City'、'11,2'、'Mahrez'],
['A3'、'Manchester City'、'13,5'、'Sterling'],
['B1'、'Liverpool'、'4,5'、'Mane'],
['B2'、'Liverpool'、'5,6'、'Salah'],
['B3'、'Liverpool'、'7,2'、'Jota']
res=[]
对于g,groupby中的值(英超联赛,key=lambda x:x[1]):#根据索引1分组
less_than=[]#临时列表,用于保存特定组中小于阈值的所有值
对于i in value:#迭代每个组中的值
float_i=float(i[2]。替换(“,”,“)#将索引2转换为float
如果g==‘曼城’站在利物浦,那么就站在曼城
如果float_i
或者以更短的方式

from itertools import groupby
res = []
for g, value in groupby(premier_league, key = lambda x:x[1]):
    last = [x[2] for x in value if float(x[2].replace(',', '.')) <= (stand_city if g == 'Manchester City' else stand_liverpool)][-1:]
    res.extend(last)

print(res) # ['10,1', '5,6']
从itertools导入groupby
res=[]
对于g,groupby中的值(英超联赛,key=lambda x:x[1]):

last=[x[2]表示浮动时的x值(x[2]。替换(',','))为什么在
itertools.groupby(英超联赛,lambda y:y[0])
中使用
y[0]
(俱乐部名称)?为什么期望输出中包含
4,5
?这不是利物浦的值超过6.5mkrieger1之前的最后一个值。这是一个错误。我编辑了我的期望输出。你能解释一下你做了什么吗?它看起来不错,而且短得多。让我打断它out@TangerCity行吗?好的,谢谢,我会查出来的。好的,下一个问题我要问这就是为什么我的代码不起作用,我需要什么来解决我的代码?你们的伟人!谢谢