简体   繁体   中英

Parsing a string containing # as separator

Example string -

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

I want this as a 2D array/list. like -

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

What is the most elegant way of doing this in python?

I have tried to split first on basis of ','. Then I get a list. for each item in lift, I split it on '#'. This way I can get it done. But I want a clean way of doing it.

>>> 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]]

Here is what happens at each step:

>>> 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 = "{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']]

Or a list comp and cast to int if you really want ints:

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

噪音少:

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

use a combination of re and numpy :

>>> 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]])

In contrast to the existing answers that use map , I propose a nested list comprehension:

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

Example usage:

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]]

A slightly shorter alternative would be to combine both map and a list comprehension:

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

一种简单的方法是使用列表理解:

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

This one is simple :)

For

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

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

you can get

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM