Python在嵌套的for循环中遍历字符串 [英] Python loop through string in nested for loops

查看:237
本文介绍了Python在嵌套的for循环中遍历字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道,我正在尝试进行非常简单的文本处理或简化.我想将所有空格(" "中没有这些空格)替换为一个.我还根据每个字符读取了一些语义动作,所以这就是为什么我不想使用任何正则表达式的原因.这是一种伪FSM模型.

I'm just wondering, I'm trying to make a very simple text processing or reduction. I want to replace all spaces (without these in " ") by one. I also have some semantic action dependent on each character read, so I that's why I don't want to use any regex. It's some kind of pseudo FSM model.

这是交易:

s = '''that's my     string, "   keep these spaces     "    but reduce these '''

期望的输出:

that's my string, "   keep these spaces    " but reduce these

我想做的是这样的:(为了使示例简单,我没有提到'"'情况)

What I would like to do is something like this: (I don't mention the '"' case to keep the example simple)

out = ""
for i in range(len(s)):

  if s[i].isspace():
    out += ' '
    while s[i].isspace():
      i += 1

  else:
    out += s[i]

在这种情况下,我不太了解范围是如何创建或共享的.

I don't quite understand how the scopes are created or shared in this case.

谢谢你的建议.

推荐答案

我还依赖于读取的每个字符执行一些语义操作……这是某种伪FSM模型.

I also have some semantic action dependent on each character read ... It's some kind of pseudo FSM model.

您实际上可以实施FSM:

You could actually implement an FSM:

s = '''that's my     string, "   keep these spaces     "    but reduce these '''


normal, quoted, eating = 0,1,2
state = eating
result = ''
for ch in s:
  if (state, ch) == (eating, ' '):
    continue
  elif (state,ch) == (eating, '"'):
    result += ch
    state = quoted
  elif state == eating:
    result += ch
    state = normal
  elif (state, ch) == (quoted, '"'):
    result += ch
    state = normal
  elif state == quoted:
    result += ch
  elif (state,ch) == (normal, '"'):
    result += ch
    state = quoted
  elif (state,ch) == (normal, ' '):
    result += ch
    state = eating
  else: # state == normal
    result += ch

print result

或者,数据驱动版本:

actions = {
    'normal' : {
        ' ' : lambda x: ('eating', ' '),
        '"' : lambda x: ('quoted', '"'),
        None: lambda x: ('normal', x)
    },
    'eating' : {
        ' ' : lambda x: ('eating', ''),
        '"' : lambda x: ('quoted', '"'),
        None: lambda x: ('normal', x)
    },
    'quoted' : {
        '"' : lambda x: ('normal', '"'),
        '\\': lambda x: ('escaped', '\\'),
        None: lambda x: ('quoted', x)
    },
    'escaped' : {
        None: lambda x: ('quoted', x)
    }
}

def reduce(s):
    result = ''
    state = 'eating'
    for ch in s:
        state, ch = actions[state].get(ch, actions[state][None])(ch)
        result += ch
    return result

s = '''that's my     string, "   keep these spaces     "    but reduce these '''
print reduce(s)

这篇关于Python在嵌套的for循环中遍历字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆