【问题标题】:Split list of strings into multiple sublists if they are the same如果字符串相同,则将字符串列表拆分为多个子列表
【发布时间】:2020-06-22 20:12:40
【问题描述】:

我想知道是否可以在 python 中将字符串列表拆分为多个子列表,如果它们是相同的字符串。例如:

输入:

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

输出:

['Red','Red']

['Green','Green']

['Yellow','Yellow']

['Blue','Blue']

['Purple']

我需要它能够每次使用不同的值来执行此操作。

我只能考虑将每个字符串与每个报价进行比较并将其附加到不同的列表中,但如果有超过 5 个不同的值,那么我认为这行不通。

希望有人能帮忙

【问题讨论】:

标签: python python-3.x string list


【解决方案1】:

您可以轻松使用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']]

【讨论】:

  • 非常感谢您的帮助
【解决方案2】:

试试这个,使用 Counter 对象:

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']]

答案将是一个列表列表,其中每种颜色的重复次数与在原始输入列表中相同。

【讨论】:

    【解决方案3】:

    您可以使用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)
    

    输出:

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

    【讨论】:

      【解决方案4】:

      您可以使用Counter 对元素进行分组,然后构建输出结果。这将避免对输入列表进行排序。

      >>> 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']]
      

      【讨论】:

        【解决方案5】:

        虽然其他答案是正确的,但这是另一种方法,无需以简单易懂的方式使用任何外部包 -

        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)
        

        输出:

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

        或者,如果你不想生成二维列表,你可以直接打印每个元素的列表,它们分别出现的频率为 -

        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)
            
        

        输出:

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

        您将获得与您在问题中提出的完全相同的输出。如果您愿意在没有任何外部包的情况下完成任务,这将是最好的方法。

        【讨论】:

          【解决方案6】:

          从 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()])
          

          输出

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

          【讨论】:

            猜你喜欢
            • 2015-09-01
            • 1970-01-01
            • 2021-07-19
            • 2019-04-16
            • 1970-01-01
            • 1970-01-01
            • 2018-10-24
            • 2011-11-13
            • 1970-01-01
            相关资源
            最近更新 更多