Python 如果值相等,则将连续的子列表分组在一起,然后提取属性为=';R';

Python 如果值相等,则将连续的子列表分组在一起,然后提取属性为=';R';,python,list,Python,List,输入数据: [[30.0, 'P'], [45.0, 'R'], [50.0, 'D']....] [[10.0, 'R'], [20.0, 'D'], [60.0, 'R']...] [[42.4, 'R'], [76.0, 'R'], [52.0, 'D']....] 这将是一个巨大的列表,包含一个浮点和一个字符串,如果它等于'R',我需要根据字符串值将子列表分组在一起。 上述列表列表是通过将数据帧转换为列表生成的(仅供参考) 因此,我必须在属性等于“R”的地方找到浮点值,然后将该值放入子

输入数据:

[[30.0, 'P'], [45.0, 'R'], [50.0, 'D']....]
[[10.0, 'R'], [20.0, 'D'], [60.0, 'R']...]
[[42.4, 'R'], [76.0, 'R'], [52.0, 'D']....]
这将是一个巨大的列表,包含一个浮点和一个字符串,如果它等于'R',我需要根据字符串值将子列表分组在一起。 上述列表列表是通过将数据帧转换为列表生成的(仅供参考)

因此,我必须在属性等于“R”的地方找到浮点值,然后将该值放入子列表中。只有当包含子列表的“R”值属性连续时,我们才将数据分组在一起。如果没有,它们应该是自己的子列表

输出数据:

仅当“R”标记数据彼此相邻或应该是单独的子列表时,“R”标记数据才应在一起

[[45.0], [10.0], [60.0], [42.4, 76.0]]
def组(列表,字符):
结果=[]
#对于每个列表
对于列表中的l:
本地_结果=[]
#对于列表中的每个元素
对于l中的n,c:
#检查字符是否相同
如果c==char:
本地_结果追加(n)
#否则,如果本地_结果有任何元素
elif局部结果:
result.append(本地\u结果)
本地_结果=[]
#修复:如果不为空,则追加最后一个结果
如果是本地结果:
result.append(本地\u结果)
返回结果
l1=[[30.0,[P'],[45.0,[R'],[50.0,[D']]
l2=[[10.0,[R'],[20.0,[D'],[60.0,[R']]
l3=[[42.4,[R'],[76.0,[R'],[52.0,[D']]
结果=组_连续([l1,l2,l3],'R')
打印(结果)
前面的代码给出了以下输出:

[[45.0]、[10.0]、[60.0][42.4、76.0]]
def组(列表,字符):
结果=[]
#对于每个列表
对于列表中的l:
本地_结果=[]
#对于列表中的每个元素
对于l中的n,c:
#检查字符是否相同
如果c==char:
本地_结果追加(n)
#否则,如果本地_结果有任何元素
elif局部结果:
result.append(本地\u结果)
本地_结果=[]
#修复:如果不为空,则追加最后一个结果
如果是本地结果:
result.append(本地\u结果)
返回结果
l1=[[30.0,[P'],[45.0,[R'],[50.0,[D']]
l2=[[10.0,[R'],[20.0,[D'],[60.0,[R']]
l3=[[42.4,[R'],[76.0,[R'],[52.0,[D']]
结果=组_连续([l1,l2,l3],'R')
打印(结果)
前面的代码给出了以下输出:

[[45.0]、[10.0]、[60.0][42.4、76.0]]

您可以使用for循环:

input_data = [
    [[30.0, 'P'], [45.0, 'R'], [50.0, 'D']],
    [[10.0, 'R'], [20.0, 'D'], [60.0, 'R']],
    [[42.4, 'R'], [76.0, 'R'], [52.0, 'D']]]

final_list = []
new_list = []

for l in [e for e in input_data]:
    if new_list:
        final_list.append(new_list)
        new_list = []
    for value, tag in l:
        if tag == 'R':
            new_list.append(value)
        elif new_list:
            final_list.append(new_list)
            new_list = []

print(final_list)     
输出:

[[45.0], [10.0], [60.0], [42.4, 76.0]]

可以使用for循环:

input_data = [
    [[30.0, 'P'], [45.0, 'R'], [50.0, 'D']],
    [[10.0, 'R'], [20.0, 'D'], [60.0, 'R']],
    [[42.4, 'R'], [76.0, 'R'], [52.0, 'D']]]

final_list = []
new_list = []

for l in [e for e in input_data]:
    if new_list:
        final_list.append(new_list)
        new_list = []
    for value, tag in l:
        if tag == 'R':
            new_list.append(value)
        elif new_list:
            final_list.append(new_list)
            new_list = []

print(final_list)     
输出:

[[45.0], [10.0], [60.0], [42.4, 76.0]]

如果我理解正确,您希望对输入数组中的每个连续元组进行分组,其中第二个元素是“R”。然后,输出应该是这些组的一个数组,因为在任何具有连续R的值的组中,在输出中显示为一个数组,在一个数组数组中。这在python中应该可以工作:

def group(input_array):
    r = []
    i = 0
    while( i < len(input_array) ):
        if(input_array[i][1] == 'R'):
            # Figure out how many consecutive R's we have then add the sublist to the return array

            group_end_index = i + 1
            if(group_end_index >= len(input_array)):
                # We've reached the end and have a new group that is one element long
                r.append([input_array[i][0]])
                break
            while(1):
                if( input_array[group_end_index][1] != 'R' ):
                    break
                group_end_index += 1

            r.append(list(map(lambda x: x[0], input_array[i:group_end_index])))
            # + 1 because we know the element at group_end_index does not have an 'R'
            i = group_end_index + 1
        else:
            # Not an 'R', ignore.
            i += 1
    return r


if __name__ == '__main__':
    print(group([[1, 'R'], [2, 'R'], [4, 'A'], [4, 'R']]))
def组(输入_数组):
r=[]
i=0
而(i=len(输入数组)):
#我们已经到了终点,有一个新的组,只有一个元素长
r、 追加([input_数组[i][0]])
打破
而(一):
如果(输入数组[组结束索引][1]!='R'):
打破
组结束索引+=1
r、 追加(列表(映射(lambda x:x[0],输入数组[i:group\u end\u index]))
#+1,因为我们知道group_end_索引处的元素没有“R”
i=组结束索引+1
其他:
#不是“R”,忽略。
i+=1
返回r
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印(组([[1,'R'],[2,'R'],[4,'A'],[4,'R']]))

这似乎是为元素列表所做的,其中元素是元组,也称为包含两个元素的列表。

如果我理解正确,您希望将输入数组中的每个连续元组分组,其中“R”是第二个元素。然后,输出应该是这些组的一个数组,因为在任何具有连续R的值的组中,在输出中显示为一个数组,在一个数组数组中。这在python中应该可以工作:

def group(input_array):
    r = []
    i = 0
    while( i < len(input_array) ):
        if(input_array[i][1] == 'R'):
            # Figure out how many consecutive R's we have then add the sublist to the return array

            group_end_index = i + 1
            if(group_end_index >= len(input_array)):
                # We've reached the end and have a new group that is one element long
                r.append([input_array[i][0]])
                break
            while(1):
                if( input_array[group_end_index][1] != 'R' ):
                    break
                group_end_index += 1

            r.append(list(map(lambda x: x[0], input_array[i:group_end_index])))
            # + 1 because we know the element at group_end_index does not have an 'R'
            i = group_end_index + 1
        else:
            # Not an 'R', ignore.
            i += 1
    return r


if __name__ == '__main__':
    print(group([[1, 'R'], [2, 'R'], [4, 'A'], [4, 'R']]))
from itertools import groupby

input_data = [
    [[30.0, 'P'], [45.0, 'R'], [50.0, 'D']],
    [[10.0, 'R'], [20.0, 'D'], [60.0, 'R']],
    [[42.4, 'R'], [76.0, 'R'], [52.0, 'D']]]

print (sum([[list(j) for i,j in
    groupby([item[0] if item[1] == 'R' else None for item in sublist],lambda x:x is not None) if i]
    for sublist in input_data],[]))
def组(输入_数组):
r=[]
i=0
而(i=len(输入数组)):
#我们已经到了终点,有一个新的组,只有一个元素长
r、 追加([input_数组[i][0]])
打破
而(一):
如果(输入数组[组结束索引][1]!='R'):
打破
组结束索引+=1
r、 追加(列表(映射(lambda x:x[0],输入数组[i:group\u end\u index]))
#+1,因为我们知道group_end_索引处的元素没有“R”
i=组结束索引+1
其他:
#不是“R”,忽略。
i+=1
返回r
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
打印(组([[1,'R'],[2,'R'],[4,'A'],[4,'R']]))
这似乎是为元素列表所做的,其中的元素是元组,也就是包含两个元素的列表

from itertools import groupby

input_data = [
    [[30.0, 'P'], [45.0, 'R'], [50.0, 'D']],
    [[10.0, 'R'], [20.0, 'D'], [60.0, 'R']],
    [[42.4, 'R'], [76.0, 'R'], [52.0, 'D']]]

print (sum([[list(j) for i,j in
    groupby([item[0] if item[1] == 'R' else None for item in sublist],lambda x:x is not None) if i]
    for sublist in input_data],[]))
结果:

[[45.0], [10.0], [60.0], [42.4, 76.0]]
起源 如果你想把一些东西分组,你应该看看能为你做些什么。为了简单起见,我们首先只使用较长列表中的一部分来计算:

i = input_data[2]
print ([(key,*lst) for key,lst in groupby(i, lambda x: x[1]=='R')])
并显示
groupby
如何为您的输入工作:

[(True, [42.4, 'R'], [76.0, 'R']), (False, [52.0, 'D'])]
因为两个
R
值在一个分组列表中,而另一个值在另一个分组列表中。您对那些
False
va不感兴趣
for i in input_data:
   ...
print ([
         [list(lst) for key,lst
          in groupby([item[0] if item[1] == 'R' else None for item in i],
          lambda x: x is not None) if key]
       for i in input_data])
[[[45.0]], [[10.0], [60.0]], [[42.4, 76.0]]]
print (sum([[[45.0]], [[10.0], [60.0]], [[42.4, 76.0]]],[]))
[[45.0], [10.0], [60.0], [42.4, 76.0]]