[英]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: 想一想:
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.