简体   繁体   中英

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)*")']
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!

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