1

I have a string which looks as such:

my_str = '15(1)(635)(634)(582)(583)'

How do I get an array of values from the string?

[15,1,635,634,582,583]
Maroun
  • 91,013
  • 29
  • 181
  • 233
Jeroen
  • 633
  • 4
  • 13
  • 2
    Please share your thoughts/attempts for the solution with us to get better help. – Maroun Mar 09 '22 at 16:34
  • There are many ways to go about this; you could use regex, you could replace the parentheses with spaces and then do a `split()`, etc. Have you tried anything? We usually want people to demonstrate more effort when posting here. See: [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/6273251) – Random Davis Mar 09 '22 at 16:37

8 Answers8

2

This can easily be solved with a simple regex : \d+

import re

my_str = '15(1)(635)(634)(582)(583)'
print([int(i.group()) for i in re.finditer(r'\d+', my_str)])

output:

[15, 1, 635, 634, 582, 583]
S.B
  • 7,457
  • 5
  • 15
  • 37
1

Remove the ")" and then split on "(":

[int(x) for x in my_str.replace(")", "").split("(")]

This list comprehension also converts the strings to ints.

Chris Sears
  • 6,175
  • 5
  • 30
  • 35
0

Given this particular input, one way to simplify the problem is to just grab continguous strings of digits, e.g. using itertools.groupby:

>>> my_str = '15(1)(635)(634)(582)(583)'
>>> from itertools import groupby
>>> [int(''.join(group)) for dec, group in groupby(my_str, str.isdecimal) if dec]
[15, 1, 635, 634, 582, 583]
Samwise
  • 51,883
  • 3
  • 26
  • 36
0

You can use the re module:

import re
my_str = '15(1)(635)(634)(582)(583)'
my_str = re.findall(r"[\w']+", my_str)

for i in range(len(my_str)):
  my_str[i] = int(my_str[i])

print(my_str)

This will first split the list into Strings that are separated by special characters, then convert them to ints.

Alternatively, you can use a regular .split() function to split by "(", then remove the final ")" with substring:

my_str = '15(1)(635)(634)(582)(583)'
my_str = my_str.split("(")
for i in range(len(my_str) - 1):
  my_str[i + 1] = int(my_str[i + 1][:-1])

my_str[0] = int(my_str[0])
print(my_str)

Output for both:

[15, 1, 635, 634, 582, 583]

I hope this helped! Please let me know if you have any further questions or need clarification :)

Ani M
  • 1,868
  • 1
  • 2
  • 13
  • 1
    Curious about the `'` character in your regex. Is that a typo? Also whey `[\w]+` and not `\d+`? – Mark Mar 09 '22 at 16:42
  • It's not a typo. \d+ should work as well, but I just use [\w]+ when dealing with special characters in general – Ani M Mar 09 '22 at 16:49
  • But you didn't use `[\w]+`, you have `[\w']+` with a `'`. What does the `'` do? – Mark Mar 09 '22 at 16:53
0

This is similar to this one. The preferred solution there was to replace all instances of one of the two delimiters with the other, e.g. by new_str = my_str.replace(')', '('), which gives you a string where you have only one kind of brackest as delimiter. Then a .split() on that with that delimiter, and you get a list, which you can easily convert into an array. If you want the individual values as integers rather than strings you'll also need to cast them.

Schnitte
  • 663
  • 1
  • 15
0
my_str = '15(1)(635)(634)(582)(583)'
new_str = my_str.replace("(", " ")
new_str = new_str.replace(")", " ")
my_arr = new_str.split()
my_arr = [int(num)for num in my_arr]
print(my_arr)
0

There might be a shorter way. Here is one:

my_str = '15(1)(635)(634)(582)(583)'
my_str = my_str.replace(')(', ' ')
my_str = my_str.replace('(', ' ')
my_str = my_str.replace(')', ' ')
my_int_list = my_str.split()
S.B
  • 7,457
  • 5
  • 15
  • 37
Sumit S
  • 344
  • 2
  • 9
0

your str looks like this:

my_str = '15(1)(635)(634)(582)(583)'

so replace ')' by space

mystr = mystr.replace(')'," ")

also replace '(' by space

mystr = mystr.replace(')'," ")

finally:

my_str = "15(1)(635)(634)(582)(583)"
mystr = my_str.replace(")", " ")
mystr = mystr.replace("(", " ")
mystr = [int(x) for x in mystr.split(" ") if x]
mystr