【问题标题】:Highest number of a certain consecutively repeating value in a list列表中某个连续重复值的最大数量
【发布时间】:2019-10-27 09:56:55
【问题描述】:

很抱歉标题令人困惑,但我无法用更好的词来形容它。

所以,假设我有这个列表:

My_list = [1,1,1,0,0,0,1,1,1,1,0,0]

如何显示最多连续重复的 1 和 0?

我想显示 4,因为连续重复 1 的最长链是 4。

并且显示3,因为连续重复0的最长链是3。

【问题讨论】:

  • 单词建议-连续。 “自觉地”甚至不是一个词。
  • 到目前为止您尝试过什么?这个问题有很多可能的解决方案。
  • 老实说,我不知道该尝试什么,我还是个初学者。我进行了研究,但没有得出任何结果。我的一位朋友建议我在这里问,所以我做了
  • 我昨天做了,link 的重复问题。只需在 print 中删除 max...

标签: python


【解决方案1】:

一个简单的解决方案,可能在某个地方的 itertools 中更优雅。

x = [1,1,1,0,0,0,1,1,1,1,0,0,0,0,0]

d = {}
c = x[0]
c_cnt = 1

for i in x[1:]:
    if i == c:
        c_cnt +=1
    else:
        d[c] = max(d.get(c,0),c_cnt)
        c=i
        c_cnt=1
d[c] = max(d.get(c,0),c_cnt)

print(d)

【讨论】:

    【解决方案2】:

    Similar previous problem searching would find

    from itertools import groupby
    
    mylist = [1,1,1,0,0,0,1,1,1,1,0,0]
    #count_ones = (max(list(group),  for _, group in groupby(mylist)), len)
    
    runs = [list(group) for _, group in groupby(mylist)] #create sublists of same values (ones of zeros)
    ones = [g for g in runs if g[0]  == 1]  # ones only sublists
    zeros = [g for g in runs if g[0]  == 0] # zeros only sublists
    
    print(len(max(ones,key=len)))           # max sublists of ones -> 4
    print(len(max(zeros,key=len)))          # max sublists of zeros -> 3
    

    【讨论】:

    • 对此进行了测试,count_zeros 为 4。
    【解决方案3】:

    只是一般情况下,不仅仅是1 or 0

    from itertools import groupby
    
    My_list = [1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 0, 0]
    result = {}
    for element, groups in groupby(My_list):
        count = sum(1 for _ in groups)
        if element in result:
            if count > result[element]:
                result[element] = count
        else:
            result[element] = count
    
    for e, count in result.items():
        print('longest chains for {} is {}'.format(e, count))
    

    输出:

    longest chains for 1 is 4
    longest chains for 0 is 3
    

    我尽量保持代码简单以便理解逻辑。

    【讨论】:

    • 你可以写sum(1 for _ in groups)而不是sum(1 for _ in groups),这样会更节省空间。
    • @AlexR 感谢诅咒的建议,它好多了。我只是在寻找这个stackoverflow.com/questions/390852/…
    【解决方案4】:

    我有一些代码可以做到这一点。我不确定这是否是处理很长列表的最佳方法,但它应该适用于您发布的列表。

    ps。没有 numpy :)

    def unique(list1): 
    
        # intilize a null list 
        unique_list = [] 
    
        # traverse for all elements 
        for x in list1: 
            # check if exists in unique_list or not 
            if x not in unique_list: 
                unique_list.append(x) 
        # print list 
        return unique_list
    
    
    My_list = [1,1,1,0,0,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1]
    
    #Create an array with the unique values from your list
    unique_list = unique(My_list)
    #Create a list with size = amount of unique values
    zeros = [0 for _ in range(len(unique_list))]
    
    
    count = 1
    for i in range(0,len(My_list)-1):
        #Is the following consecutive value equal to the current value? 
        if (My_list[i] == My_list[i+1]) and (i != len(My_list)-2): 
            #count how many consecutive repetitions you have
            count = count + 1
        else:
            idx = unique_list.index(My_list[i])
            #If it's the highest amount of repetitions, save it
            if zeros[idx] < count:
                zeros[idx] = count    
            count = 1
    
    for i in range(0, len(unique_list)):
        print("Highest consecutive "+ str(unique_list[i]) + "'s are: " + str(zeros[i]))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多