Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列出返回的3个值_Python_List_List Comprehension - Fatal编程技术网

Python 列出返回的3个值

Python 列出返回的3个值,python,list,list-comprehension,Python,List,List Comprehension,我想做一个列表理解,因为使用for循环会使程序变慢,所以我希望将其转换为列表理解。但是,我希望它返回3个值,主要是I、ii和word。我尝试了我的方法,但收到如下错误: 错误: ValueError: not enough values to unpack (expected 3, got 0) 我的代码类型简介: words: Is a list[List[string]] # [["I","have","something","to","buy"]] word: is a word in

我想做一个列表理解,因为使用for循环会使程序变慢,所以我希望将其转换为列表理解。但是,我希望它返回3个值,主要是I、ii和word。我尝试了我的方法,但收到如下错误:

错误:

ValueError: not enough values to unpack (expected 3, got 0)
我的代码类型简介:

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

预期产出:

0 0 I
[]
0 0 I

例如,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. 
我想知道是否有任何较短的版本,我如何才能解决我目前的问题

我的问题的完整版本:

我的代码:

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)
我的当前输出:

0 0 I
[]
0 0 value
预期产出:

0 0 I
[]
0 0 I

我不知道为什么当使用列表理解时,“ll”的值会变得不同

您可以使用
zip
*
操作符来解压缩序列:

i, ii, word = zip(*([a, b, c] for ...))
这假设您可以用有意义的内容填写
。特别要注意的是,不需要中介列表构造。可以使用由括号代替方括号表示的生成器表达式

从技术上讲,您的结果将是元组而不是列表。对于列表,您可以使用
map

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

您可以将
zip
*
操作符一起使用来解压缩序列:

i, ii, word = zip(*([a, b, c] for ...))
这假设您可以用有意义的内容填写
。特别要注意的是,不需要中介列表构造。可以使用由括号代替方括号表示的生成器表达式

从技术上讲,您的结果将是元组而不是列表。对于列表,您可以使用
map

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

我想这就是你想要做的:

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]
为了清晰起见,我更改了一些内容:

  • 为了便于解释,我给了变量更清晰(但更长)的名称
  • 我假设您的“wordy”列表是一个嵌套列表,因为在您的实际示例中,您需要多个列表,所以我在示例中添加了另一个列表来演示它的用法

我想这就是你想要做的:

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]
为了清晰起见,我更改了一些内容:

  • 为了便于解释,我给了变量更清晰(但更长)的名称
  • 我假设您的“wordy”列表是一个嵌套列表,因为在您的实际示例中,您需要多个列表,所以我在示例中添加了另一个列表来演示它的用法


我很难看到您想要返回的内容。看起来你想在最后列出3张清单?每个列表应该包含什么?如果你可以使用for循环,也许可以在这里发布,我们可以帮助你把它变成一个列表。这样,我们就可以更好地理解返回列表的输出,并将其分配给3个变量。你能给出一个例子和预期的结果吗?可能是循环顺序错误,例如,i列表将包含单词的索引,ii列表将包含w的索引,而word只是类似于wwd的字符串列表。我很难看到你试图返回的内容。看起来你想在最后列出3张清单?每个列表应该包含什么?如果你可以使用for循环,也许可以在这里发布,我们可以帮助你把它变成一个列表。这样,我们就可以更好地理解返回列表的输出,并将其分配给3个变量。您能给出一个例子和预期结果吗?可能是循环顺序错误,例如,i列表将包含单词的索引,ii列表将包含w的索引,而word只是类似于wwd的字符串列表。
zip
可能是一个好主意。但是,此代码不起作用。列表理解仍然需要一些时间work@Stephen,你确定吗<代码>i,ii,word=zip(*(范围(5)中的uu的列表('abc'))工作正常。这不一样吗?酷!让我也试试。
name错误:名称“c”未定义
@初学者,我无法复制。例如,
i,ii,word=zip(*(范围(5)中的uu的列表('abc'))
起作用,这大致就是您正在做的。
zip
可能是一个好主意。但是,此代码不起作用。列表理解仍然需要一些时间work@Stephen,你确定吗<代码>i,ii,word=zip(*(范围(5)中的uu的列表('abc'))工作正常。这不一样吗?酷!让我也试试。
name错误:名称“c”未定义
@初学者,我无法复制。例如,
i,ii,word=zip(*(范围(5)中的uu的列表('abc'))
起作用,这大致就是您正在做的。我明白了。问题来了,因为我的字典循环在列表理解之外。顺便说一句,如果我的单词表=[['I','have','something','to','buy','from','home','king']],那么现在我们有两个类似的单词。发生这种情况时,我不知道如何调用每个单词的值。例如,我的输出变成了[[(0,),(0,),('I')],[(1,),(8,),('king')]]。如何分别调用wlist\U索引、word\U索引和word?谢谢你帮我重新编辑它。你可以改变循环的输出,也可以只打印每个输出列表的第三个值,它将始终是单词的值。如果你是这个意思的话?新的参考词表=[['I','have','something','to','buy','from','home',','king']。例如,我们的输出为[(0,),(0,),('I')],[(1,),(8,),('king')]。然后我想把这些输出分配给wlist_索引,word_索引,word。比如说,当我们打印wlist_索引时,我们将得到0和1,因为0表示“I”,1表示“king”。那么单词_索引是0表示“I”,8表示“king”。单词=‘我’和‘国王’当然。有没有这样的方法来赋值?wlist_index=[0,1],word_index=[0,8],word=['I,'king']?。是的,绝对是。您尝试了什么,但什么不起作用?这是我尝试的,listcompoutput=[zip([wlist\u index,word\u index,word])用于word\u索引,word in enumerate(wlist)用于wlist\u I