简体   繁体   English

在python列表中对连续的类似项进行分组

[英]Group consecutive similar items in a python list

I have a list containing different data types, say numbers and strings: 我有一个包含不同数据类型的列表,比如数字和字符串:

foo = [5,2,'a',8,4,'b','y',9, 'd','e','g']

Let's say I want to find all consecutive strings in the the list, and group them together: 假设我想在列表中找到所有连续的字符串,并将它们组合在一起:

bar = [ ['a'],['b','y'],['d','e','g'] ]

How can I do this 我怎样才能做到这一点

This is a wonderful opportunity to use groupby : 这是使用groupby的绝佳机会:

from itertools import groupby


foo = [5,2,'a',8,4,'b','y',9, 'd','e','g']
bar = [list(g) for k, g in groupby(foo, key=lambda x: isinstance(x, str)) if k]

which produces the desired: 产生所需的:

[['a'], ['b', 'y'], ['d', 'e', 'g']]

Iterate through each element in the list, if it is of type str , append it to one_d_array , otherwise, append one_d_array to two_d_array , provided one_d_array is not empty. 通过每个元件列表中的迭代,如果它是类型的str ,附加它one_d_array ,否则,附加one_d_arraytwo_d_array ,提供one_d_array不为空。 Reset one_d_array whenever the element is not of type str 每当元素不是str类型时重置one_d_array

lst = [5,2,'a',8,4,'b','y',9, 'd','e','g', 3]
ind = 0
two_d_arr = []
one_d_arr = []
while(ind < len(lst)):
    cur_element = lst[ind]
    if(isinstance(cur_element, str) == True):
        one_d_arr.append(cur_element)
    else:
        if(len(one_d_arr) != 0):
            two_d_arr.append(one_d_arr)
        one_d_arr = []
    ind = ind+1
if(len(one_d_arr) != 0):
            two_d_arr.append(one_d_arr)
print(two_d_arr)

Without using any import, you can do it through a good old "for loop" iterating over the elements of the lists. 在不使用任何导入的情况下,您可以通过迭代列表元素的旧的“for循环”来完成。 Here is a code working also for any type you want, not only string: 这是一个代码,也适用于您想要的任何类型,不仅仅是字符串:

def group_list(a_list, a_type):
    res = []
    sublist = []
    for elem in a_list:
        if isinstance(elem, a_type):
            # Here the element is of  type a_type: append it to a sublist
            sublist.append(elem)
        else:
            # Here the element is not of type a_type: append the sublist (if not empty) to the result list
            if sublist:
                res.append(sublist)
                sublist = []
    # If the last element of the list is of type a_type, the last sublist has not been appended: append it now
    if sublist:
        res.append(sublist)

    return res


foo = [5,2,'a',8,4,'b','y',9, 'd','e','g']
print(group_list(foo,str)) 
# [['a'], ['b', 'y'], ['d', 'e', 'g']]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM