【问题标题】:Parsing a string containing # as separator解析包含 # 作为分隔符的字符串
【发布时间】:2016-05-11 08:54:58
【问题描述】:

示例字符串 -

"{1#2#3,4#5#6,7#8#9,10#11#12}"

我希望这是一个二维数组/列表。 喜欢-

[
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [10, 11, 12]
]

在 python 中最优雅的方式是什么?

我尝试先根据“,”进行拆分。然后我得到一个清单。对于电梯中的每个项目,我将其拆分为“#”。这样我就可以搞定了。 但我想要一种干净的方式。

【问题讨论】:

  • 不是我,但到目前为止你尝试了什么?
  • 我认为您被否决了,因为您没有在帖子中包含您所做的努力。
  • 现在我已经添加了投入的精力,请为这个问题投票。
  • “最优雅的方式”是自以为是的。对一个人来说优雅的东西被另一个人称为“不可读”或“混淆”。

标签: python regex list parsing


【解决方案1】:
>>> s="{1#2#3,4#5#6,7#8#9,10#11#12}"
>>> list(map(lambda x:list(map(int, x.split('#'))), s.strip('{}').split(',')))
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]

以下是每个步骤发生的情况:

>>> s
'{1#2#3,4#5#6,7#8#9,10#11#12}'
>>> s.strip('{}')
'1#2#3,4#5#6,7#8#9,10#11#12'
>>> s.strip('{}').split(',')
['1#2#3', '4#5#6', '7#8#9', '10#11#12']
>>> list(map(lambda x:x.split('#'), s.strip('{}').split(',')))
[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10', '11', '12']]

【讨论】:

  • 我想我会用更清晰和安全的s.lstrip("{").rstrip("}") 替换你的s[1:-1]
【解决方案2】:

噪音更小:

map(lambda x: map(int, x.split('#')), s.strip("{}").split(','))

【讨论】:

    【解决方案3】:

    使用renumpy 的组合:

    >>> import re
    >>> import numpy as np
    >>> s = "{1#2#3,4#5#6,7#8#9,10#11#12}"
    >>> digits = re.findall('\d+', s)
    >>> rows = s.count(',') + 1
    >>> np.array(digits, dtype=int).reshape(rows, len(digits)/rows)
    array([[ 1,  2,  3],
           [ 4,  5,  6],
           [ 7,  8,  9],
           [10, 11, 12]])
    

    【讨论】:

    • 你同意这对于这个简单的案例来说真的是矫枉过正,不是吗?
    • @ByteCommander 我认为这个任务的过度切片、分割和映射是脆弱和过度的。
    【解决方案4】:
    s = "{1#2#3,4#5#6,7#8#9,10#11#12}"
    print(list(map(str.split, s.strip("{}").replace("#"," ").split(",")))
    [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10', '11', '12']]
    

    如果你真的想要 int,也可以使用列表 comp 并转换为 int:

    print([list(map(int,_s.split("#"))) for _s in  s.strip("{}").split(",")])
    

    【讨论】:

      【解决方案5】:

      与使用map 的现有答案相比,我建议使用嵌套列表理解:

      [[int(x) for x in row.split("#")] for row in s.strip("{}").split(',')]
      

      示例用法:

      s = "{1#2#3,4#5#6,7#8#9,10#11#12}"
      l = [[int(x) for x in row.split("#")] for row in s.strip("{}").split(',')]
      print(l)
      # Output:  [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
      

      一个稍微短一点的选择是结合map 和列表理解:

      [list(map(int, row.split("#"))) for row in s.strip("{}").split(',')]
      

      【讨论】:

        【解决方案6】:

        一个简单的方法是使用列表推导:

        [list(map(int, sub.split('#'))) for sub in s.strip('{}').split(',')]
        

        【讨论】:

        • 谢谢,我没注意,现在更新了。
        【解决方案7】:

        这个很简单:)

        对于

        s="{1#2#3,4#5#6,7#8#9,10#11#12}"

        [el.split('#') for el in s[1:-1].split(',')]

        你可以得到

        [['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['10' , '11', '12']]

        【讨论】:

          猜你喜欢
          • 2014-07-12
          • 1970-01-01
          • 2013-05-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-17
          • 1970-01-01
          相关资源
          最近更新 更多