1

Let's say, there is a string that can be:

  • "3d6"
  • "t3d6"
  • "3h4d6"
  • "t3h4d6"
  • "6 t3d6"
  • "6 t3h4d6+6" and similar possible variations.

Is there a way to run through the string and add the items from it to the list based on them being letters/numbers/symbols/other stuff? For example, "6 t3h10d6+6" would result in [6, " ", "t", 3, "h", 10, "d", 6, "+", 6] as the list.

I'm working on a telegram bot that uses the python to answer users' inputs with dice calculations results and the hardest part is processing the user input. Right now the input is being processed through the clumsy if statements complex. There might be a better way to process the user input and I would be glad to hear your advice!

Not a duplicate to this question. The question is about breaking a string into list of characters, basically turning a string into a list of strings. My question is about breaking string into list of different items that can be both strings and integers.

  • Possible duplicate of [Break string into list of characters in Python](https://stackoverflow.com/questions/9833392/break-string-into-list-of-characters-in-python) – samiles Sep 04 '17 at 14:10

3 Answers3

2

You could also use regex for that (by specifying to find all digit and non digit characters):

>>> x
'6 t3h4df6d+!643'
>>> __import__("re").findall('\d+|\s+|\D+', x)
['6', ' ', 't', '3', 'h', '4', 'df', '6', 'd+!', '643']

As you can see the above expression doesn't separate d from +!. If that is a problem you can slightly modify the above regular expression to:

>>> x = '6 t3h4df6d+!643'
>>> re.findall('\d+|\s+|[a-zA-Z]+|\D+', x)
['6', ' ', 't', '3', 'h', '4', 'df', '6', 'd', '+!', '643']

which separates them completely!

Update

If you want to split strings as single characters (e.g. "xy" as ['x','y']) you can change the above regex expression to:

>>> x = '6 t3h4df6d+!643'
>>> __import__("re").findall('\d+|\s+|\w|\D+', x)
['6', ' ', 't', '3', 'h', '4', 'd', 'f', '6', 'd', '+!', '643']
coder
  • 12,832
  • 5
  • 39
  • 53
1

If you are comfortable using list comprehensions List comprehensions

  [ int(c) if c.isdigit() else c for c in "6 t3h4d6+6" ]

Output

[6, ' ', 't', 3, 'h', 4, 'd', 6, '+', 6]
Nikhil Rupanawar
  • 4,061
  • 10
  • 35
  • 51
1

You could use groupby, isdigit and list comprehensions to achieve the desired result :

from itertools import groupby
text = "6 t3h10d6+6"
substrings = groupby(text, lambda c: (c.isdigit(), c.isspace()))
print([int(''.join(l)) if d else ''.join(l) for (d, s), l in substrings])
# => [6, ' ', 't', 3, 'h', 10, 'd', 6, '+', 6]

Note that '10' is parsed as 10, not '10' or [1, 0].

Eric Duminil
  • 52,989
  • 9
  • 71
  • 124