简体   繁体   English

在字符串中查找重复的字符,并确定在python中重复连续的次数

[英]Find a repeated character in a string and determining how many times in a row it is repeated in python

I have a text file with a single line in it. 我有一个文本文件,其中包含一行。 The line of text is a whole bunch of random numbers. 文本行是一大堆随机数。 I need to determine the most amount of times a 5 is repeated and print how many times it's repeated. 我需要确定重复5次的最多次并打印重复的次数。 For example: numList: 1234555325146555. The most amount of times 5 is repeated in a row is 3 and that happens 2 times. 例如:numList:1234555325146555。连续重复5次的次数为3次,发生次数为2次。 Here is the code I have so far, it shows me at what positions 5 occurs. 这是我到目前为止的代码,它向我展示了5发生的位置。 I think this is the first step but can't figure out how to move on. 我认为这是第一步,但无法弄清楚如何继续前进。

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

You got the right idea for finding out which position the 5 is in. 你有正确的想法找出5在哪个位置。

So how do you find out how long a row of 5's is? 那你怎么知道5排的排了多长时间? Think about: 想一想:

  1. You need to know if you've found a 5, if its part of a series. 你需要知道你是否找到了5,如果它是系列的一部分。 Keep track of the previous number. 跟踪以前的号码。 If that's also a 5, then you're continuing a series. 如果那也是5,那么你将继续一系列。
  2. If you're continuing a series, then have another counter to keep track how long it is. 如果你继续一个系列,那么有另一个计数器来跟踪它的持续时间。
  3. You need to reset the counter if you reach a number that is not a 5. But before resetting, you need to store that value. 如果您的数字不是5,则需要重置计数器。但在重置之前,您需要存储该值。
  4. For the next part of the problem (finding out how many series of 5's there are), try using additional "meta" variables that keeps track of the longest series you have so far and how many times you've seen that. 对于问题的下一部分(找出有多少系列的5个),尝试使用额外的“元”变量来跟踪你到目前为止最长的系列以及你看过多少次。

Good luck! 祝好运! and keep on asking questions 并继续提问

I often find with tasks like this I ask myself, how would I do this without a computer if the problem were big enough I couldn't remember everything. 我经常发现这样的任务,我问自己,如果问题不够大,我怎么会在没有电脑的情况下做到这一点我不记得一切。 So here, I would go till I found a 5. Then I would look at the next number, and if it was a 5, keep going till there were no more 5's in a row. 所以在这里,我会去,直到我找到一个5.然后我会看下一个数字,如果它是5,继续前进,直到连续不再有5个。 So in your example, I would have found 3 5's in a row. 所以在你的例子中,我会连续找到3个5。 I would make a note that the longest I have found was 3 5's. 我会记下,我找到的最长时间是3 5。 I would then move on to the next 5. 然后我会继续下一个5。

I would then again count how many 5's in a row there were. 然后我会再计算连续多少5个。 In this case I would see that there was only 1. So I would not bother doing anything because I would see that it is less than 3. Then I would move on to the next 5. 在这种情况下,我会看到只有1.所以我不打算做任何事情因为我会看到它小于3.然后我会继续下一个5。

I would see that there were 3 in a row, I would go back to my paper to see how long the longest I have found was, and I would see that it was 3. So then I would then make a note that I have seen 2 sets of 3 in a row. 我会看到连续3个,我会回到我的论文,看看我发现的最长时间是多久,我会看到它是3.那么我会记下我已经看到的2套3连续。

If I ever found 4 or more I would forget all that info I had about sets of 3 and start over with sets of 4 or whatever. 如果我发现4个或更多,我会忘记所有关于3组的信息,并从4组或其他任何东西开始。

So try implementing this sort of idea in your loop. 所以尝试在循环中实现这种想法。

Here is a fairly straightforward way to figure this out: 这是一个相当直接的方法来解决这个问题:

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

I would continually check to see if a particular string of 5's was in the given string until it wasn't anymore (adding a '5' each time). 我会不断检查特定的5字符串是否在给定的字符串中,直到它不再存在(每次添加'5')。 Then I'd back up 1 and use the count method of strings -- Something like this (pseudo-code follows -- Note this is not syntactically valid python. That's up to you since this is homework.) 然后我回来1并使用字符串的count方法 - 像这样的东西(伪代码如下 - 注意这不是语法上有效的python。由于这是功课,这取决于你。)

str5='5'
while str5 in your_string
    concatenate '5' with str5

#your string is too long by 1 element
max_string=str5 minus the last '5'
yourstring.count(max_string)
from collections import defaultdict, Counter
from itertools import groupby

num_str = '112233445556784756222346587'

res = defaultdict(Counter)
for dig,seq in groupby(num_str):
    res[dig][len(list(seq))] += 1

print res['5'].most_common()

returns 回报

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

(meaning that '5' was seen twice and '555' was seen once) (意思是'5'被看到两次,'555'被看到一次)

#  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;

Hope, it helps :) 希望能帮助到你 :)

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

相关问题 如何找到一个单词在字符串中重复的次数? - How to find how many times a word is repeated in a string? 如何使用Python查找字符串中的首个字母重复多少次? - How to find how many times the first letter is repeated in a string using Python? 查找字符串中的第一个重复字母及其重复次数 - Find the first repeated letter in a string and the times it is repeated 如何打印一个字符串,显示字符串中每个字符重复的次数? - how to print a string showing the number of times each character is repeated in a string? Python re:如何设置模式重复次数的限制 - Python re: how to set a limit to how many times a pattern is repeated 如何找到一个单词在给定字符串中连续重复的最大次数? - How to find the largest number of times a word is repeated consecutively in a given string? 如何在 python 的文本文件中查找重复 5 次的单词? - How to find words repeated 5 times in a text file in python? 如何在 Python 中重复多次的字符串中插入变量 - How to insert a variable in a string that is repeated multiple times in Python 查找字符串中的第一个非重复字符 - Find the first non-repeated character in a string 如何用交替字符'“'和'”'(python)替换字符串中随机重复的字符'"'? - How to replace character ' " ' randomly repeated in a string with alternating characters ' “ ' and ' ” ' (python)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM