Python 基于值的重复对列表的元素进行分组

Python 基于值的重复对列表的元素进行分组,python,Python,我对Python真的很陌生,我在解决下面的问题时遇到了一个问题 我有这样一个清单: my_list = ['testOne:100', 'testTwo:88', 'testThree:76', 'testOne:78', 'testTwo:88', 'testOne:73', 'testTwo:66', 'testThree:90'] 我想根据以“testOne”开头的元素的出现情况对元素进行分组 预期结果: new_list=[['testOne:100', 'testTwo:88', '

我对Python真的很陌生,我在解决下面的问题时遇到了一个问题

我有这样一个清单:

my_list = ['testOne:100', 'testTwo:88', 'testThree:76', 'testOne:78', 'testTwo:88', 'testOne:73', 'testTwo:66', 'testThree:90']
我想根据以“testOne”开头的元素的出现情况对元素进行分组

预期结果:

new_list=[['testOne:100', 'testTwo:88', 'testThree:76'], ['testOne:78', 'testTwo:88'], ['testOne:73', 'testTwo:66', 'testThree:90']]

使用分隔符
|

step1="|".join(my_list)
基于
'testOne'

step2=step1.split("testOne")
“testOne”
附加到列表元素以获得结果

new_list=[[i for i in str('testOne'+i).split("|") if len(i)>0] for i in step2[1:]]

使用分隔符
|

step1="|".join(my_list)
基于
'testOne'

step2=step1.split("testOne")
“testOne”
附加到列表元素以获得结果

new_list=[[i for i in str('testOne'+i).split("|") if len(i)>0] for i in step2[1:]]

这不是一款很酷的单衬里,但它也适用于更通用的标签:

result = [[]]
seen = set()
for entry in my_list:
    test, val = entry.split(":")
    if test in seen:
        result.append([entry])
        seen = {test}
    else:
        result[-1].append(entry)
        seen.add(test)
在这里,我们跟踪在
集合中已经看到的测试标签,并在遇到同一列表中已经看到的标签时启动一个新列表

或者,假设列表总是以
testOne
开头,只要标签是
testOne
,您就可以开始一个新列表:

result = []
for entry in my_list:
    test, val = entry.split(":")
    if test == "testOne":
        result.append([entry])
    else:
        result[-1].append(entry)

这不是一款很酷的单衬里,但它也适用于更通用的标签:

result = [[]]
seen = set()
for entry in my_list:
    test, val = entry.split(":")
    if test in seen:
        result.append([entry])
        seen = {test}
    else:
        result[-1].append(entry)
        seen.add(test)
在这里,我们跟踪在
集合中已经看到的测试标签,并在遇到同一列表中已经看到的标签时启动一个新列表

或者,假设列表总是以
testOne
开头,只要标签是
testOne
,您就可以开始一个新列表:

result = []
for entry in my_list:
    test, val = entry.split(":")
    if test == "testOne":
        result.append([entry])
    else:
        result[-1].append(entry)

有一个简单的一行是很好的,但我想如果我尝试的话,它会看起来有点太复杂。以下是我的想法:

# Create a list of the starting indices:
ind = [i for i, e in enumerate(my_list) if e.split(':')[0] == 'testOne']

# Create a list of slices using pairs of indices:
new_list = [my_list[i:j] for (i, j) in zip(ind, ind[1:] + [None])]

有一个简单的一行是很好的,但我想如果我尝试的话,它会看起来有点太复杂。以下是我的想法:

# Create a list of the starting indices:
ind = [i for i, e in enumerate(my_list) if e.split(':')[0] == 'testOne']

# Create a list of slices using pairs of indices:
new_list = [my_list[i:j] for (i, j) in zip(ind, ind[1:] + [None])]

只需在每个
testOne
上启动一个新列表

>>> new_list = []
>>> for item in my_list:
        if item.startswith('testOne:'):
            new_list.append([])
        new_list[-1].append(item)

>>> new_list
[['testOne:100', 'testTwo:88', 'testThree:76'], ['testOne:78', 'testTwo:88'], ['testOne:73', 'testTwo:66', 'testThree:90']]

只需在每个
testOne
上启动一个新列表

>>> new_list = []
>>> for item in my_list:
        if item.startswith('testOne:'):
            new_list.append([])
        new_list[-1].append(item)

>>> new_list
[['testOne:100', 'testTwo:88', 'testThree:76'], ['testOne:78', 'testTwo:88'], ['testOne:73', 'testTwo:66', 'testThree:90']]

不是很复杂,但它可以工作:

my_list = ['testOne:100', 'testTwo:88', 'testThree:76', 'testOne:78', 'testTwo:88', 'testOne:73', 'testTwo:66', 'testThree:90']

splitting_word = 'testOne'
new_list = list()
partial_list = list()
for item in my_list:
    if item.startswith(splitting_word) and partial_list:
        new_list.append(partial_list)
        partial_list = list()
    partial_list.append(item)
new_list.append(partial_list)

不是很复杂,但它可以工作:

my_list = ['testOne:100', 'testTwo:88', 'testThree:76', 'testOne:78', 'testTwo:88', 'testOne:73', 'testTwo:66', 'testThree:90']

splitting_word = 'testOne'
new_list = list()
partial_list = list()
for item in my_list:
    if item.startswith(splitting_word) and partial_list:
        new_list.append(partial_list)
        partial_list = list()
    partial_list.append(item)
new_list.append(partial_list)

那么到目前为止你做了什么呢?加入空格并使用。它总是变成“一,二,三”,还是这些只是例子?也可能有“一,三”,或者以“二”开头?那么到目前为止你做了什么?加入空格并使用。它总是以“一,二,三”开头,还是这些只是例子?是否也会有“一、三”或从“二”开始?我能得到否决票的原因吗?代码正在工作,并提供了所需的输出不是我,但不,它没有提供所需的输出。另外,它还相当粗糙。
new_list=['testOne:100','testTwo:88','testThree:76'],['testOne:78','testTwo:88'],['testOne:73','testTwo:66','testThree:90']
。这就是所需的输出,我的输出给出了相同的结果,您使用的是什么Python版本?在我的Python3.5.2中,最后缺少了
'testThree:90'
。我可以得到否决票的原因吗?代码正在工作,并提供了所需的输出不是我,但不,它没有提供所需的输出。另外,它还相当粗糙。
new_list=['testOne:100','testTwo:88','testThree:76'],['testOne:78','testTwo:88'],['testOne:73','testTwo:66','testThree:90']
。这就是所需的输出,我的输出给出了相同的结果,您使用的是什么Python版本?在我的Python3.5.2中,最后缺少了
'testThree:90'
。如果e.split(':')[0]='testOne']
,那么第一部分可以重写为
ind=[i for i,e In enumerate(my_list)]。。。第二部分和第三部分到
[我的列表[i:j]中的(i,j)在zip中(ind,ind[1:][None])
。我真的很喜欢这种方法。如果e.split(':')[0]=='testOne']
,那么第一部分可以重写为
ind=[I for I,e in enumerate(my_list)]。。。第二部分和第三部分到
[我的列表[i:j]中的(i,j)在zip中(ind,ind[1:][None])
。我真的很喜欢这种方法。它很有魅力!非常感谢:)非常有魅力!非常感谢:)