Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.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 元组乘积_Python_Python 2.7 - Fatal编程技术网

Python 元组乘积

Python 元组乘积,python,python-2.7,Python,Python 2.7,作为一名Python新手,这可能是一个愚蠢的问题,但我找不到解决方案。 我正在做一个元组的乘积,并且工作得非常完美,如下所示: list = list(itertools.product(["this", "the"],["example", "test"],["is not", "isnt"],[" working", "correct"])) print(list) 但如果一个变量声明另一个变量,它将不再工作: test = ["this", "the"],["example", "tes

作为一名Python新手,这可能是一个愚蠢的问题,但我找不到解决方案。 我正在做一个元组的乘积,并且工作得非常完美,如下所示:

list = list(itertools.product(["this", "the"],["example", "test"],["is not", "isnt"],[" working", "correct"]))
print(list)
但如果一个变量声明另一个变量,它将不再工作:

test = ["this", "the"],["example", "test"],["is not", "isnt"],[" working", "correct"]
list = list(itertools.product(test))
print(list)
我检查了
type()
函数以获得该类,它是一个元组


我在Python3.x中运行它,但我想让它与2.7兼容。首先,使用
list
作为变量名是不好的,因为这会覆盖默认的
list
函数

在第一个示例中,您将多个参数传递给
itertools.product
,然后它将每个参数组合在一起,以获得所需的输出。在非工作示例中,只传递一个参数,即tuple
test
。谢天谢地,您可以使用Python的元组解包语法将元组的每个元素扩展为一个参数:

test = ["this", "the"],["example", "test"],["is not", "isnt"],[" working", "correct"]
# The * before test unpacks the tuple into separate arguments
result2 = list(itertools.product(*test))
print(result2)
[('this', 'example', 'is not', ' working'), ('this', 'example', 'is not', 'correct'), ('this', 'example', 'isnt', ' working'), ('this', 'example', 'isnt', 'correct'), ('this', 'test', 'is not', ' working'), ('this', 'test', 'is not', 'correct'), ('this', 'test', 'isnt', ' working'), ('this', 'test', 'isnt', 'correct'), ('the', 'example', 'is not', ' working'), ('the', 'example', 'is not', 'correct'), ('the', 'example', 'isnt', ' working'), ('the', 'example', 'isnt', 'correct'), ('the', 'test', 'is not', ' working'), ('the', 'test', 'is not', 'correct'), ('the', 'test', 'isnt', ' working'), ('the', 'test', 'isnt', 'correct')]

首先,使用
list
作为变量名是不好的,因为这会覆盖默认的
list
函数

在第一个示例中,您将多个参数传递给
itertools.product
,然后它将每个参数组合在一起,以获得所需的输出。在非工作示例中,只传递一个参数,即tuple
test
。谢天谢地,您可以使用Python的元组解包语法将元组的每个元素扩展为一个参数:

test = ["this", "the"],["example", "test"],["is not", "isnt"],[" working", "correct"]
# The * before test unpacks the tuple into separate arguments
result2 = list(itertools.product(*test))
print(result2)
[('this', 'example', 'is not', ' working'), ('this', 'example', 'is not', 'correct'), ('this', 'example', 'isnt', ' working'), ('this', 'example', 'isnt', 'correct'), ('this', 'test', 'is not', ' working'), ('this', 'test', 'is not', 'correct'), ('this', 'test', 'isnt', ' working'), ('this', 'test', 'isnt', 'correct'), ('the', 'example', 'is not', ' working'), ('the', 'example', 'is not', 'correct'), ('the', 'example', 'isnt', ' working'), ('the', 'example', 'isnt', 'correct'), ('the', 'test', 'is not', ' working'), ('the', 'test', 'is not', 'correct'), ('the', 'test', 'isnt', ' working'), ('the', 'test', 'isnt', 'correct')]

@BradtheBrutalist你是不是建议我在回答的顶部添加
import itertools
?在最初的示例中没有显示它,所以我假设它是隐含的。否则我不确定我是否理解这个问题,你能澄清一下吗?不,我只是说我对itertools很陌生。谢谢你的解释,我还有很多关于Python的知识要学习哈哈。变量的名称只是为了快速而肮脏的测试。@bradthebrutalist你是不是建议我在答案的顶部添加
import itertools
?在最初的示例中没有显示它,所以我假设它是隐含的。否则我不确定我是否理解这个问题,你能澄清一下吗?不,我只是说我对itertools很陌生。谢谢你的解释,我还有很多关于Python的知识要学习哈哈。变量的名称只是为了快速而肮脏的测试。