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']
3 Réponses
+ 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
+ 1
import re
mytring = '<hello>xskj<world>hello'
wordsInside = re.findall(r"<(\w+)", mytring)
wordsOutside = re.findall(r">(\w+)", mytring)
print(wordsInside)
print(wordsOutside)
+ 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.