Python re.match 字数统计

Python re.match word count

我想查找并打印所有以 M 开头且仅包含一次 i 或 I 的状态。我试图先找到 M 个包含状态并计算第 i 个序列..

import re
pattern = 'i'
state_names=['Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut',
'Delaware','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana',
'Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts',
'Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada',
'New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio',
'Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee',
'Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin',
'Wyoming']
m_word=['']

for k in range (len(state_names)):
  state=state_names[k]
  mword=m_word[k]
  mword.append(re.search('^M',state))

  left = 0
  count = 0
  while True:
      match = re.search(pattern, mword)
      if not match:
          break
      count += 1
      left += match.start() + 1
      for i in range (len(m_word)):
          if (count==1):
              print(mword[i])
  print(count)

任务代码很多 - 我会做一些更简单的事情,例如:

m_states = [state for state in state_names if state[0] == "M"]
answers = [m_state for m_state in m_states if m_state.count("i") + m_state.count("I") == 1]

编辑:感谢佩德罗的评论 - 更新:

m_states = [state for state in state_names if state.startswith("M")]
answers = [m_state for m_state in m_states if m_state.lower().count("i") == 1]

编辑:而且,也许更简单一点:

answers = [state for state in state_names if state.startswith("M") and state.lower().count("i") == 1]

正则表达式必然会使它变得更复杂。 这是简单的解决方案。

>>> state_names=['Alabama','Alaska','Arizona','Arkansas','California','Colorado','Connecticut',
... 'Delaware','Florida','Georgia','Hawaii','Idaho','Illinois','Indiana',
... 'Iowa','Kansas','Kentucky','Louisiana','Maine','Maryland','Massachusetts',
... 'Michigan','Minnesota','Mississippi','Missouri','Montana','Nebraska','Nevada',
... 'New Hampshire','New Jersey','New Mexico','New York','North Carolina','North Dakota','Ohio',
... 'Oklahoma','Oregon','Pennsylvania','Rhode Island','South Carolina','South Dakota','Tennessee',
... 'Texas','Utah','Vermont','Virginia','Washington','West Virginia','Wisconsin',
... 'Wyoming']
>>> 
>>> for word in state_names:
...     if word.startswith('M'):
...             letters = [ l.lower() for l in word]
...             if letters.count('i')==1:
...                     print(word)
... 
Maine
Minnesota
>>> 

您可以使用 filter、returns iterator :

list(filter(lambda x: x.startswith('M') and (x.count("i") == 1), state_names))