I have a string str1
with the following format, and I need to extract all the words starting with "SF-", without duplication.
I tried this:
newlist=[]
# Driver code
str1 = '"SF-9632":"schema,names","startAt":0,"maxResults":50,"total":58,"issues","SF-6349","total":70,"SF-6533'
for x in str1:
if "SF-" in str1:
newlist.append(x)
print(newlist)
The output inside newlist
is equal to str1
.
CodePudding user response:
You missed the split
operation which breaks your main string str1
into an array:
newlist=[]
# Driver code
str1 = '"SF-9632":"schema,names","startAt":0,"maxResults":50,"total":58,"issues","SF-6349","total":70,"SF-6533'
lstr1 = str1.split("\"")
for x in lstr1:
if "SF-" in x:
newlist.append(x)
newlist = list(set(newlist))
print(newlist)
Which then outputs:
['SF-9632', 'SF-6349', 'SF-6533']
CodePudding user response:
Thank you that is what I needed.