Counting the frequency of ‘BB’ or ‘EE’ in a string using Python

huangapple go评论97阅读模式
英文:

Counting the frequency of 'BB' or 'EE' in a string using Python

问题

我正在尝试计算给定字符串中某个字符串出现的次数。

我正在尝试计算'BB'或'EE'出现的次数,例如BB会返回1,而BBB会从'BBB'和B'BB'中返回2。我该如何简化这个代码?我尝试使用count,但它只计算一次。

  1. s = input()
  2. for i in range(len(s) + 1):
  3. if i > 1:
  4. if s[i - 2:i] == 'BB':
  5. a += 1
  6. if s[i - 2:i] == 'EE':
  7. b += 1
英文:

I'm trying to count every time a string appears in a given string

I'm trying to count the number of times 'BB' or 'EE' appear, for example BB would give 1, and BBB would give 2 from 'BB'B and B'BB'. How can I shorten this down? I tried using count but it only counts it once.

  1. s=input()
  2. for i in range(len(s)+1):
  3. if i>1:
  4. if s[i-2:i]=='BB':
  5. a+=1
  6. if s[i-2:i]=='EE':
  7. b+=1

答案1

得分: 1

你可以使用正向预查的正则表达式来实现这个,我认为

  1. print("FOUND:", len(re.findall("([BE])(?=\)", text)))

[BE] 匹配B或E并将其存储... ?= 是向前预查... 它需要匹配但不消耗它... \\1 表示“匹配第一次存储的内容(要么是B要么是E)”

英文:

you can use regular expression with a positive lookahead for this i think

  1. print("FOUND:",len(re.findall("([BE])(?=\)",text)))

[BE] matches either B or E and stores it ... ?= is a forward lookahead... it requires a match but does not consume it ... \\1 is saying "match that first thing you stored (either B or E)"

答案2

得分: 0

  1. s=input()
  2. for i,_ in enumerate(s):
  3. case s[i:i+2]:
  4. when "BB":
  5. a += 1
  6. when "EE":
  7. b += 1

Not tested, though.

英文:
  1. s=input()
  2. for i,_ in enumerate(s):
  3. case s[i:i+2]:
  4. when "BB":
  5. a += 1
  6. when "EE":
  7. b += 1

Not tested, though.

答案3

得分: 0

  1. 一个有点棘手的情况:

a = s.split('B')[1:-1].count('')
b = s.split('E')[1:-1].count('')

  1. 例如,'fooBBbarBBBquxB'.split('B')`会得到`['foo', '', 'bar', '', '', 'qux', '']`。通过B分割时,每当连续出现两个B时都会产生一个空字符串,所以我统计这些空字符串。如果字符串以B开头/结束,它还会生成一个空字符串,因此我从列表中移除第一个和最后一个元素。
英文:

A somewhat tricky one:

  1. a = s.split('B')[1:-1].count('')
  2. b = s.split('E')[1:-1].count('')

For example, 'fooBBbarBBBquxB'.split('B') gives ['foo', '', 'bar', '', '', 'qux', '']. Splitting by B gives an empty string whenever two consecutive B appear, so I count those empty strings. It also gives an empty string at start/end if the string starts/ends with B, so I remove the first and last element from the list.

答案4

得分: 0

保持跟踪循环中的上一个字符,并与当前字符一起使用,以测试是否匹配了你的字母对之一。

  1. text = input()
  2. lastch = ""
  3. aa = ee = 0
  4. for ch in text:
  5. test = lastch + ch
  6. aa += test == "BB"
  7. ee += test == "EE"
  8. lastch = ch

在循环的最后一次迭代中,lastch(上一个字符)被初始化为空字符串,因此第一次循环时,没有上一个字符,test不会与任何两个字符的字符串匹配,因为它只有一个字符。

英文:

Keep track of the last character you saw in the loop, and use it with the current character to test whether you have matched one of your letter pairs.

  1. text = input()
  2. lastch = ""
  3. aa = ee = 0
  4. for ch in text:
  5. test = lastch + ch
  6. aa += test == "BB"
  7. ee += test == "EE"
  8. lastch = ch

The character from the last iteration of the loop (lastch) is initialized to an empty string, so the first time through, when there's no previous character, test doesn't match any of the two-character strings because it only has one character.

huangapple
  • 本文由 发表于 2023年6月5日 06:59:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402733.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定