简体   繁体   中英

string to list conversion in python?

I have a string like :

searchString = "u:sads asdas asdsad n:sadasda as:adds sdasd dasd a:sed eee"

what I want is list :

["u:sads asdas asdsad","n:sadasda","as:adds sdasd dasd","a:sed eee"]

What I have done is :

values = re.split('\s', searchString)
mylist = []
word = ''
for elem in values:
  if ':' in elem:
    if word:
      mylist.append(word)
    word = elem
  else:
    word = word + ' ' + elem
list.append(word)
return mylist

But I want an optimized code in python 2.6 .

Thanks

Use regular expressions:

import re
mylist= re.split('\s+(?=\w+:)', searchString)

This splits the string everywhere there's a space followed by one or more letters and a colon. The look-ahead ( (?= part) makes it split on the whitespace while keeping the \\w+: parts

You can use "look ahead" feature offered by a lot of regular expression engines. Basically, the regex engines checks for a pattern without consuming it when it comes to look ahead.

import re
s = "u:sads asdas asdsad n:sadasda as:adds sdasd dasd a:sed eee"
re.split(r'\s(?=[a-z]:)', s)

This means, split only when we have a \\s followed by any letter and a colon but don't consume those tokens.

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