简体   繁体   中英

Split line into category and text

I have list that looks like this:

foo = ["neg * , This is a sentence","pos * , This is another sentence"]

I need to split the sentences in such a way that one value will become the category, neg or pos , and one the sentence. I tried:

for text in foo:
    text = text.split("*")
    for a,b in text:
        cat=a
        text=b

However I get a "too many values to unpack", anyone have an idea?

Your problem is your loop is horribly constructed (which is excusable, since you are obviously new to the whole thing)

Try this safer method (a list-comprehension):

>>> foo = ["neg * , This is a sentence","pos * , This is another sentence"]
>>> [p.split('*', 1) for p in foo]
[['neg ', ' , This is a sentence'], ['pos ', ' , This is another sentence']]

Now you have a list of [CAT, TEXT] items.

>>> l = [p.split('*', 1) for p in foo]
>>> for cat, text in l:
    print 'cat: %s, text: %s' % (cat, text)

cat: neg , text:  , This is a sentence
cat: pos , text:  , This is another sentence

The line for a,b in text: isn't appropriate. A better choice is a,b=text . The former code operates on a list of pairs, the latter operates on a single pair.

Applying that advice, and removing redundancies:

foo = ["neg * , This is a sentence","pos * , This is another sentence"]
for text in foo:
    a,b = text.split("*")
    # Now do something with 'a' and 'b'

If you really want to re-use the text variable, this works:

for text in foo:
    a, text = text.split("*")
    # Now do something with 'a' and 'text'

You're doing the assignment part in the inner loop wrong. Here, try this

lines = ["neg * , This is a sentence","pos * , This is another sentence"]
for line in lines:
    category, sentence = line.split("*", 1)

You're iterating over strings in the second loop

for text in foo:
    text = text.split("*")
    a,b = text:

In that case, you assign a to the first element of text and b the second one. Otherwise you're splitting the string into characters and you don't have the same number of variables as the number of characters

textList = []
catList = []
for str in foo:
    (cat,text) = str.split('*')
    textList.append(text)
    catList.append(cat)

Then textList is a list of text strings and catList is a list of cat strings. Otherwise you won't be able to access all the different cats and texts.

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