简体   繁体   English

如果字符串相同,则将字符串列表拆分为多个子列表

[英]Split list of strings into multiple sublists if they are the same

I was wondering if it was possible in python to split a list of strings into multiple sublists if they are the same string.我想知道在 python 中是否可以将字符串列表拆分为多个子列表,如果它们是相同的字符串。 For example:例如:

Input:输入:

['Red','Green','Yellow','Blue','Blue','Green','Red','Yellow','Purple']

Output: Output:

['Red','Red']

['Green','Green']

['Yellow','Yellow']

['Blue','Blue']

['Purple']

I need it to be able to do this with different values each time.我需要它每次都能使用不同的值来做到这一点。

I can only think of comparing each string to each offer and appending it to different lists but if there are more than the 5 different values then I don't think that would work.我只能考虑将每个字符串与每个报价进行比较并将其附加到不同的列表中,但如果有超过 5 个不同的值,那么我认为这不会起作用。

Hope someone can help希望有人能帮忙

You may use itertools.groupby() to it easily您可以轻松地使用itertools.groupby()

from itertools import groupby    
values = ['Red','Green','Yellow','Blue','Blue','Green','Red','Yellow','Purple']

result = [list(v) for k,v in groupby(sorted(values))]    
print(result) 
# [['Blue', 'Blue'], ['Green', 'Green'], ['Purple'], ['Red', 'Red'], ['Yellow', 'Yellow']]

You can use a Counter to group elements and then build the output result.您可以使用Counter对元素进行分组,然后构建 output 结果。 This would avoid the need of sorting the input list.这将避免对输入列表进行排序。

>>> from collections import Counter
>>> c = Counter(l)
>>> res = [[k]*v for k,v in c.items()]
>>> res
[['Red', 'Red'], ['Green', 'Green'], ['Yellow', 'Yellow'], ['Blue', 'Blue'], ['Purple']]

Try this, using a Counter object:试试这个,使用Counter object:

from collections import Counter

lst = ['Red','Green','Yellow','Blue','Blue','Green','Red','Yellow','Purple']
counter = Counter(lst)
[[color] * num for color, num in counter.items()]
=> [['Blue', 'Blue'], ['Purple'], ['Green', 'Green'], ['Yellow', 'Yellow'], ['Red', 'Red']]

The answer will be a list of lists, where each color is repeated as many times as it was in the original input list.答案将是一个列表列表,其中每种颜色的重复次数与原始输入列表中的一样多。

You can use Counter() from the collections module:您可以使用collections模块中的Counter()

from collections import Counter

lst = ['Red','Green','Yellow','Blue','Blue','Green','Red','Yellow','Purple']

c = Counter(lst)

lsts = [[l]*c[l] for l in c]

print(lsts)

Output: Output:

[['Red', 'Red'], ['Green', 'Green'], ['Yellow', 'Yellow'], ['Blue', 'Blue'], ['Purple']]

Although the other answers are correct, here's another approach without using any outside package in a simple understandable manner -尽管其他答案是正确的,但这是另一种方法,无需以简单易懂的方式使用任何外部 package -

lst= ['Red','Green','Yellow','Blue','Blue','Green','Red','Yellow','Purple']

dictionary = {}
# Store the frequency of each element that occurs in list
for i in lst :
    if(dictionary.get(i)==None):
        dictionary[i]=1
    else :
        dictionary[i]+=1

ans=[]
# Generate the final answer by making list with each element occurring according to their frequency
for k in dictionary.keys():
    tmp = [k]*dictionary[k]
    ans.append(tmp)
    
print(ans)

Output: Output:

[['Red', 'Red'], ['Green', 'Green'], ['Yellow', 'Yellow'], ['Blue', 'Blue'], ['Purple']]

Or, if you don't want to generate 2-d list, you can directly print the list of each element where they occur their frequency number of times respectively as -或者,如果您不想生成二维列表,您可以直接打印它们出现频率的每个元素的列表,分别为 -

lst= ['Red','Green','Yellow','Blue','Blue','Green','Red','Yellow','Purple']
dictionary = {}

# Same as in last example... just a more pythonic way of doing it
for i in lst :
    dictionary[i]=dictionary.get(i,0)+1

for k in dictionary.keys():
    elements = [k]*dictionary[k]
    print(elements)
    

Output: Output:

['Red', 'Red']
['Green', 'Green']
['Yellow', 'Yellow']
['Blue', 'Blue']
['Purple']

You will get the exact output as you had asked in the question.正如您在问题中提出的那样,您将得到确切的 output。 This would be the best way if you are willing to accomplish the task without any external packages.如果您愿意在没有任何外部软件包的情况下完成任务,这将是最好的方法。

As of Python 3.7 Counter inherits the capability of dict to remember insertion order, so I finally have a use for the elements() method of Counter :截至 Python 3.7 Counter继承了dict记住插入顺序的能力,所以我终于可以使用Counterelements()方法了:

from collections import Counter
from itertools import islice

array = ['Red', 'Green', 'Yellow', 'Blue', 'Blue', 'Green', 'Red', 'Yellow', 'Purple']

counter = Counter(array)

elements = counter.elements()

print([[*islice(elements, count)] for count in counter.values()])

OUTPUT OUTPUT

> python3 test.py
[['Red', 'Red'], ['Green', 'Green'], ['Yellow', 'Yellow'], ['Blue', 'Blue'], ['Purple']]
>

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

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