user8539705
user8539705

Reputation:

Convert string into list of tuples

I need to convert tuples into an array of strings with those tuples.

So this:

(A, "DEFAULT"), (B, "a$"), (C, "aa$"), (D, "(a|b|c)*aab(a|b|c)*")`

Should become this.

['(A, "DEFAULT")', '(B, "a$")', '(C, "aa$")', '(D, "(a|b|c)*aab(a|b|c)*")']

Upvotes: 0

Views: 83

Answers (1)

Geancarlo Murillo
Geancarlo Murillo

Reputation: 507

string = '(A, "DEFAULT"), (B, "a$"), (C, "aa$"), (D, "(a|b|c)*aab(a|b|c)*")'
split = string.split('),')
result = []
for i in range(len(split)):
  if i != len(split) - 1:
    text = split[i] + ')'
    result.append(text)
  else:
    result.append(split[i])

Got it!

Upvotes: 1

Related Questions