[英]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. 我想进行列表理解,因为使用for循环会使程序变慢,因此我希望将其转换为列表理解。 However, I wanted it to return 3 values mainly the i, ii and word.
但是,我希望它返回3个值,主要是i,ii和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 list将包含单词的索引,ii list将包含w的索引,而word只是类似于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. 我不知道为什么当使用列表推导时,“ ll”的值会变得不同。
You can use zip
with the *
operator to unpack sequences: 您可以将
zip
与*
运算符配合使用来解压缩序列:
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
: 对于列表,可以使用
map
:
i, ii, word = map(list(zip(*...)))
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: 为了清楚起见,我更改了一些内容:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.