今天我在Dan H.实施How to get all possible combinations of a list’s elements?解决方案时遇到了一个奇怪的错误 我一直收到这个错误:
TypeError:'itertools.combinations'对象不可调用
有谁知道为什么会这样,以及如何修复它?我使用以下代码:
def all_subsets(ss):
return chain(*map(lambda x: combinations(ss, x), range(0, len(ss)+1)))
for subset in all_subsets([1,2,3,4]):
在此之后我得到一个错误。任何人都可以帮助我吗?
EDIT1:
人们要求提供完整的代码,所以这里是代码错误的一些相关部分:
from scapy.all import *
import itertools
from itertools import *
def all_combinations(input):
return chain(*map(lambda y: combinations(input, y), range(0, len(input)+1)))
extensionHeader=["IPv6ExtHdrHopByHop","IPv6ExtHdrRouting","IPv6ExtHdrDestOpt","IPv6ExtHdrFragment"]
ip=IPv6(src="2a00:1450:4009:807::1002", dst="2a00:1450:4009:807::1002")
combinations = itertools.combinations(extensionHeader, 1)
for subset in all_combinations(extensionHeader):
index=0
finalForm=""
while index<len(subset):
substring=subset[index:index+17]
if substring is "IPv6ExtHdrRouting":
if index==0:
ip.nh=43
此处还有完整的错误:
WARNING: No route found for IPv6 destination :: (no default route?)
Traceback (most recent call last):
File "C:\Users\Martinooos\workspace\testing\scapyTest.py", line 18, in <module
>
for subset in all_combinations(extensionHeader):
File "C:\Users\Martinooos\workspace\testing\scapyTest.py", line 11, in all_com
binations
return chain(*map(lambda y: combinations(input, y), range(0, len(input)+1)))
File "C:\Users\Martinooos\workspace\testing\scapyTest.py", line 11, in <lambda
>
return chain(*map(lambda y: combinations(input, y), range(0, len(input)+1)))
TypeError: 'itertools.combinations' object is not callable
答案 0 :(得分:1)
编辑:
这是你的问题:
combinations = itertools.combinations(extensionHeader, 1)
您不希望将结果保存为组合,因为当您尝试使用combinations()
中的all_combinations()
函数时,Python会尝试将您的组合变量用作函数,因此您会得到你的错误:
'itertools.combinations'对象不可调用
要修复,只需将变量重命名为其他内容,例如:
combs = itertools.combinations(extensionHeader, 1)
或修改您的all_combinations()
以使用itertools.combinations
而非combinations
:
def all_combinations(input):
return itertools.chain(*map(lambda y: itertools.combinations(input, y), range(0, len(input)+1)))