0

Python String Split

Hi. I want to make a program that check the string and takes everything in < > and add it to a list.if find a phrase outside of the <> add it to another list. E.g: input: '<hello>xskj<world>hello' output: ['hello', 'world'] , ['xskj', 'hello']

7th Apr 2020, 3:37 PM
Ramin.RX7
Ramin.RX7 - avatar
3 Antworten
+ 1
Like: text = ... a,b = [],[] for i in text.split('>'): if i[0] != '<': #to list b then a b.append(i[:i.index('<')]) a.append(i[index('<')+1:]) else: #to list a a.append(i[1:]) That should do the trick
7th Apr 2020, 5:03 PM
Mensch
Mensch - avatar
+ 1
import re mytring = '<hello>xskj<world>hello' wordsInside = re.findall(r"<(\w+)", mytring) wordsOutside = re.findall(r">(\w+)", mytring) print(wordsInside) print(wordsOutside)
7th Apr 2020, 5:38 PM
rodwynnejones
rodwynnejones - avatar
+ 1
rodwynnejones and JumpingMensch Thank you both. Is it possible to do sth like this? input: '<jack<new>>asdf <zach>' output: ['jack', ['new']],'zach'] , ['asdf'] I mean 'jack' is a list that the second member is list of other <> in jack.
7th Apr 2020, 6:30 PM
Ramin.RX7
Ramin.RX7 - avatar