查找字符串中的重复字符,并确定它在python中在一行中重复了多少次

查找字符串中的重复字符,并确定它在python中在一行中重复了多少次,python,python-2.7,Python,Python 2.7,我有一个只有一行的文本文件。这行文字是一堆随机数。我需要确定5的最大重复次数,并打印出重复次数。例如:numList:1234555325146555。一行中重复5次的次数最多为3次,即重复2次。这是我到目前为止的代码,它显示了在什么位置出现5。我认为这是第一步,但不知道如何继续前进 numbers = open("numbers.txt",'rU') count = -1 numString = numbers.readline() for num in numString: coun

我有一个只有一行的文本文件。这行文字是一堆随机数。我需要确定5的最大重复次数,并打印出重复次数。例如:numList:1234555325146555。一行中重复5次的次数最多为3次,即重复2次。这是我到目前为止的代码,它显示了在什么位置出现5。我认为这是第一步,但不知道如何继续前进

numbers = open("numbers.txt",'rU')
count = -1
numString = numbers.readline()
for num in numString:
    count += 1
    if num == '5':
        print count
        counter += 1

我会不断检查给定字符串中是否有一个由5组成的特定字符串,直到它不再存在为止(每次添加一个“5”)。然后我将备份1并使用字符串的
count
方法——类似这样的方法(下面是伪代码——注意这不是语法上有效的python。这取决于您,因为这是家庭作业)


下面是一个相当简单的方法来解决这个问题:

>>> import re
>>> numString = '1234555325146555'
>>> fives = re.findall(r'5+', numString)
>>> len(max(fives))          # most repetitions
3
>>> fives.count(max(fives))  # number of times most repetitions occurs
2
返回

[(1, 2), (3, 1)]

(意思是“5”被看了两次,“555”被看了一次)

我经常发现这样的任务,我问自己,如果问题太大,我什么都记不起来,没有电脑我怎么做。所以在这里,我会一直走到找到一个5。然后我会看下一个数字,如果是5,继续走,直到没有更多的5在一排。所以在你的例子中,我会在一行中找到3个5。我会记下我发现的最长的是35秒。然后,我将转到下一个5

然后我再数一数连续有多少个5。在这种情况下,我会看到只有1个。所以我不会费心做任何事情,因为我会看到它小于3。然后我将继续下一个5

我会看到连续有3个,我会回到我的论文,看看我发现的最长的长度是多少,我会看到它是3个。然后我会记下,我已经看到了两组3连成一行

如果我找到4个或更多,我会忘记所有关于3个集合的信息,从4个集合或其他集合开始


因此,试着在你的循环中实现这种想法。

你找到了正确的想法,找到了5的位置

那么,如何确定一排5的长度呢?想想:

  • 你需要知道你是否找到了一个5,如果它是一个系列的一部分。跟踪上一个号码。如果这也是一个5,那么你将继续一个系列
  • 如果你正在继续一个系列,那么有另一个计数器来记录它的长度
  • 如果达到的数字不是5,则需要重置计数器。但在重置之前,您需要存储该值
  • 对于问题的下一部分(找出5个系列中有多少个),尝试使用额外的“元”变量来跟踪到目前为止最长的系列以及您已经看到的次数
  • 祝你好运!继续问问题

    #  First step: Find at most how many times 5 comes in a row.
    # For this I have a counter which increases by 1 as long 
    # as I am dealing with '5'. Once I find a character other 
    # than '5' I stop counting, see if my counter value is greater
    # than what I have found so far and start counting from zero again.
    
    numbers = open("numbers.txt",'rU')
    count = -1
    numString = numbers.readline()
    maximum = -1;
    
    for num in numString:
        count +=1
        if num== '5':
            counter += 1
        else:
            maximum=max(maximum, counter)
            counter = 0;
    
    #  Second step: Find how many times this repeats.
    # Once I know how much times it comes in a row, I find consequent fives
    # with the same method and see if the length of them is equal to my maximum
    
    count=-1
    amount = 0
    for num in numString:
        count +=1
        if num== '5':
            counter += 1
        else:
            if maximum == counter:
                amount += 1
            counter = 0;
    

    希望,这有帮助:)

    这被标记为家庭作业,不只是给出答案可能更好?
    [(1, 2), (3, 1)]
    
    #  First step: Find at most how many times 5 comes in a row.
    # For this I have a counter which increases by 1 as long 
    # as I am dealing with '5'. Once I find a character other 
    # than '5' I stop counting, see if my counter value is greater
    # than what I have found so far and start counting from zero again.
    
    numbers = open("numbers.txt",'rU')
    count = -1
    numString = numbers.readline()
    maximum = -1;
    
    for num in numString:
        count +=1
        if num== '5':
            counter += 1
        else:
            maximum=max(maximum, counter)
            counter = 0;
    
    #  Second step: Find how many times this repeats.
    # Once I know how much times it comes in a row, I find consequent fives
    # with the same method and see if the length of them is equal to my maximum
    
    count=-1
    amount = 0
    for num in numString:
        count +=1
        if num== '5':
            counter += 1
        else:
            if maximum == counter:
                amount += 1
            counter = 0;