Beginner
Beginner

Reputation: 147

list comprehension returning 3 values

I would like to make a list comprehension, since using for loop will make the program slow so I wish to convert it to list comprehensions. However, I wanted it to return 3 values mainly the i, ii and word. I tried my method but i received errors like:

Errors:

ValueError: not enough values to unpack (expected 3, got 0)

Brief about my codes type:

words: Is a list[List[string]] # [["I","have","something","to","buy"]]
word: is a word in string type

Code:

i, ii, word = [[i, ii, word] for i, w in enumerate(words) for ii, ww in
                     enumerate(w) for wwd in ww if word == wwd]

Expected output:

For example, i list will contain the index for words, ii list will contain the index for w, while word is just list of strings that are similar to wwd.

i = [0,1,2,3,4,5,6,7,8,9,10 ~] # this index should be relevant to the matching of wwd == word
ii = [0,1,2,3,4,5,6,7,8,9,10 ~] # this index should be relevant to the matching of wwd == word
word = ["I", "have", "something"] # this is received when comparing the wwd with word

#Another example: i= 2, ii= 4, word = "have" and then store it to the variables for each matching of word. 

I am wondering whether is there any shorter version and how can I solve my current issue?

Full Version of my problem:

My code:

wordy = [['I', 'have', 'something', 'to', 'buy', 'from', 'home', ',']]
key = {'I': 'We', 'king': 'man', 'value': 'time'}
a = []

def foo(a,b,c): return a,b,c

for ll in key.keys():
    for ii, l in enumerate(wordy):
        for i, word in enumerate(l):
            for wordd in word:
                if ll == wordd:
                    a.append(list(zip([ii, i, ll])))

for x in a:
    i, ii, ll = foo(*x)

print(i,ii,ll)



for ll in key.keys():
    a = [[i, ii, ll]for i, words in enumerate(wordy) for ii, word in enumerate(words) for wordd in word if ll == wordd]
print(a)
for x in a:
    i, ii, ll = foo(*x)
print(i, ii, ll)

My current output:

0 0 I
[]
0 0 value

Expected output:

0 0 I
[]
0 0 I

I do not know why when use list comprehensions then the value of "ll" become different.

Upvotes: 0

Views: 761

Answers (2)

girlvsdata
girlvsdata

Reputation: 1644

I think this is what you are trying to do:

wordlist = [['I', 'have', 'something', 'to', 'buy', 'from', 'home', ','],['You', 'king', 'thing', 'and', 'take', 'to', 'work', ',']]
dictionary = {'I': 'We', 'king': 'man', 'value': 'time'}
forloopoutput = []
listcompoutput = []

#For Loop
for key in dictionary.keys():
    for wlist_index, wlist in enumerate(wordlist):
        for word_index, word in enumerate(wlist):
            if key == word:
                forloopoutput.append([wlist_index, word_index, word])

#List comprehension
listcompoutput = [[wlist_index, word_index, word] for word_index, word in enumerate(wlist) for wlist_index, wlist in enumerate(wordlist)for key in dictionary.keys() if key==word]

I've changed a few things for clarity:

  • I've given the variables clearer (but lengthier) names to make for an easier explanation.
  • I'm assuming your "wordy" list is a nested list because in your real life example you are expecting multiple lists, so I've added another list to my example to demonstrate it's use.

Upvotes: 1

jpp
jpp

Reputation: 164623

You can use zip with the * operator to unpack sequences:

i, ii, word = zip(*([a, b, c] for ...))

This assumes you can fill in ... with something that makes sense. Note, in particular, that intermediary list construction isn't required. You can use a generator expression, indicated by the parentheses in place of square brackets.

Technically, your results will be tuples rather than lists. For lists, you can use map:

i, ii, word = map(list(zip(*...)))

Upvotes: 1

Related Questions