0

i have a list of sentences called "data" and i carried out an operation of soundex.
i dont know how to store it in a variable.. here is my code:

for line in data:

    for word in line.split():

        print jellyfish.soundex(word)   

that gives me a list of soundex codes for all words..

how do i store the results in a variable?? i have tried:

data_new = [(jellyfish.soundex(word) for word in line.split()) for line in data]

but that did not help.

1
  • 1
    Try list comprehension instead of generator expression: data_new = [[jellyfish.soundex(word) for word in line.split()] for line in data] Commented Feb 8, 2014 at 6:21

3 Answers 3

1

Use list comprehension instead of generator expression:

data_new = [[jellyfish.soundex(word) for word in line.split()] for line in data] 

Or, if you want flat list:

data_new = [jellyfish.soundex(word) for line in data for word in line.split()]
Sign up to request clarification or add additional context in comments.

6 Comments

could i get back my original list from this??
@Sword, What do you mean the original list?
yeah.. for an entire intuition abt my problem check stackoverflow.com/questions/21628391/…
@Sword, If you mean get the data from the generated data_new. No you can't. (You may use ' '.joni(...), but it will not give you exactly same string because str.split remove consecutive whitespaces.)
so how do i get alon with it? i was thinking of computing and comparing the soundex in the for loop itself . if it is same , str.replace it with right spelling.. wats ur say?
|
1

Remove the generator expression from within the comprehension:

data_new = [jellyfish.soundex(word) for line in data for word in line.split() ]

Comments

1

Remove the parentheses around (jellyfish.soundex(word) for word in line.split()), which is a generator expression (see for example generator comprehension). The result,

data_new = [jellyfish.soundex(word) for word in line.split() for line in data]

should give you what you seem to want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.