dict.pop上的Python3类型错误

dict.pop上的Python3类型错误,python,dictionary,python-3.5,Python,Dictionary,Python 3.5,我试图反映一个对称群的所有排列的平方。 正方形的“坐标”在字典中用作键。价值观 现在应该沿x轴反射。但出于某种原因 .pop()只想接受整数?我认为它接受任何不包含不可变的类型,如果字典中没有键,则会引发一个键错误。这是我的密码: def y_reflection(insquare): """Function that reflects a square against it's y axis: <----- reflection

我试图反映一个对称群的所有排列的平方。 正方形的“坐标”在字典中用作键。价值观 现在应该沿x轴反射。但出于某种原因 .pop()只想接受整数?我认为它接受任何不包含不可变的类型,如果字典中没有键,则会引发一个键错误。这是我的密码:

def y_reflection(insquare):
    """Function that reflects a square against it's y axis:
                <----- reflection

                            --> x
    n   n-1 ... 2 1 |   1 2 ... n-1 n
    n-1 n-2 ... 1 n | | n 1 ... n-2 n-1
    ............... | v ................
    2   1   ... 4 3 | y 3 4 ... 1   2 
    1   n   ... 3 2 |   2 3 ... n   1"""

    for y in range (0, SQUARE_SIZE()):
        for x in range (0, SQUARE_SIZE()):
            insquare[(y,x)]=insquare.pop((y,(SQUARE_SIZE()-x)))
    return insquare

    insquare[(x,y)]=insquare.pop((x,(SQUARE_SIZE()-y)))
TypeError: 'tuple' object cannot be interpreted as an integer
def y_反射(insquare):
“”“相对于y轴反射正方形的函数:
x
n-1…2 1 | 12…n-1 n
n-1n-2…1n | | n1…n-2n-1
五。。。。。。。。。。。。。。。。
2 1…4 3 | y 3 4…1 2
1 n…3 2 | 2 3…n 1“
对于范围(0,平方_SIZE())内的y:
对于范围(0,平方_SIZE())内的x:
insquare[(y,x)]=insquare.pop((y,(SQUARE_SIZE()-x)))
返回方
insquare[(x,y)]=insquare.pop((x,(SQUARE_SIZE()-y)))
TypeError:“tuple”对象不能解释为整数

你知道发生了什么吗?

你绝对肯定
insquare
dict
?如果
insquare
是一个
列表
,则会出现此错误

旁注:您在这段代码中重新计算了大量的
SQUARE\u SIZE();一次用于外部
范围
(不是问题),然后
方形大小()
倍用于内部
范围
,然后
方形大小()**2倍用于内部循环体。即使
SQUARE\u SIZE()
返回一个常量值,简单调用函数的开销也可能是运行时的一个主要因素。如果
SQUARE\u SIZE()。为了避免大量不必要的
range
创建,
SQUARE\u SIZE()
调用,并取消循环,您可能需要
导入itertools
并在函数中更改为类似以下内容:

sqsize = SQUARE_SIZE()
for y, x in itertools.product(range(sqsize), repeat=2):
    # I removed a bunch of unnecessary parens here as well
    insquare[y, x]=insquare.pop((y, sqsize - x))
return insquare

您是否绝对确定
insquare
dict
?如果
insquare
是一个
列表
,则会出现此错误

旁注:您在这段代码中重新计算了大量的
SQUARE\u SIZE();一次用于外部
范围
(不是问题),然后
方形大小()
倍用于内部
范围
,然后
方形大小()**2倍用于内部循环体。即使
SQUARE\u SIZE()
返回一个常量值,简单调用函数的开销也可能是运行时的一个主要因素。如果
SQUARE\u SIZE()。为了避免大量不必要的
range
创建,
SQUARE\u SIZE()
调用,并取消循环,您可能需要
导入itertools
并在函数中更改为类似以下内容:

sqsize = SQUARE_SIZE()
for y, x in itertools.product(range(sqsize), repeat=2):
    # I removed a bunch of unnecessary parens here as well
    insquare[y, x]=insquare.pop((y, sqsize - x))
return insquare

我认为错误在于:

insquare[(x,y)]

这:
(x,y)
是一个元组。

我认为错误在于:

insquare[(x,y)]

这:
(x,y)
是一个元组。

我已经测试了你的代码。错误不在该函数内。错误出现在定义
insquare
的地方。从错误的外观来看,您需要检查是否正在传递字典。我尝试了下面的代码,效果非常好

def y_reflection(insquare):
    """Function that reflects a square against it's y axis:
                <----- reflection

                            --> x
    n   n-1 ... 2 1 |   1 2 ... n-1 n
    n-1 n-2 ... 1 n | | n 1 ... n-2 n-1
    ............... | v ................
    2   1   ... 4 3 | y 3 4 ... 1   2 
    1   n   ... 3 2 |   2 3 ... n   1"""

    for y in range (0, 1):
        for x in range (0, 1):
            insquare[(y,x)]=insquare.pop((y,(1-x)))
    return insquare

    insquare[(x,y)]=insquare.pop((x,(1-y)))

d = {
        (0,0): 0,
        (0,1): 1,
        (1,0): 1,
        (1,1): 0
    }
print (y_reflection(d))
def y_反射(insquare):
“”“相对于y轴反射正方形的函数:
x
n-1…2 1 | 12…n-1 n
n-1n-2…1n | | n1…n-2n-1
五。。。。。。。。。。。。。。。。
2 1…4 3 | y 3 4…1 2
1 n…3 2 | 2 3…n 1“
对于范围(0,1)中的y:
对于范围(0,1)内的x:
insquare[(y,x)]=insquare.pop((y,(1-x)))
返回方
insquare[(x,y)]=insquare.pop((x,(1-y)))
d={
(0,0): 0,
(0,1): 1,
(1,0): 1,
(1,1): 0
}
打印(y_反射(d))

测试链接-

我已经测试了你的代码。错误不在该函数内。错误出现在定义
insquare
的地方。从错误的外观来看,您需要检查是否正在传递字典。我尝试了下面的代码,效果非常好

def y_reflection(insquare):
    """Function that reflects a square against it's y axis:
                <----- reflection

                            --> x
    n   n-1 ... 2 1 |   1 2 ... n-1 n
    n-1 n-2 ... 1 n | | n 1 ... n-2 n-1
    ............... | v ................
    2   1   ... 4 3 | y 3 4 ... 1   2 
    1   n   ... 3 2 |   2 3 ... n   1"""

    for y in range (0, 1):
        for x in range (0, 1):
            insquare[(y,x)]=insquare.pop((y,(1-x)))
    return insquare

    insquare[(x,y)]=insquare.pop((x,(1-y)))

d = {
        (0,0): 0,
        (0,1): 1,
        (1,0): 1,
        (1,1): 0
    }
print (y_reflection(d))
def y_反射(insquare):
“”“相对于y轴反射正方形的函数:
x
n-1…2 1 | 12…n-1 n
n-1n-2…1n | | n1…n-2n-1
五。。。。。。。。。。。。。。。。
2 1…4 3 | y 3 4…1 2
1 n…3 2 | 2 3…n 1“
对于范围(0,1)中的y:
对于范围(0,1)内的x:
insquare[(y,x)]=insquare.pop((y,(1-x)))
返回方
insquare[(x,y)]=insquare.pop((x,(1-y)))
d={
(0,0): 0,
(0,1): 1,
(1,0): 1,
(1,1): 0
}
打印(y_反射(d))

测试链接-

正方形的确切尺寸是多少?和
insquare
?正方形大小为n。轴的长度。Insquare是我描述的字典。它只是一个坐标(x,y)作为键和一个值0,
SQUARE\u SIZE
?和
insquare
?正方形大小为n。轴的长度。Insquare是我描述的字典。仅仅是坐标(x,y)作为键和值0,这对于
dict
来说实际上不是错误。
dict
可以在
元组上键入。甚至不需要父母
insquare[x,y]
的工作原理是相同的。你是对的,没有读到
insquare
是一个dict,我以为它是一个列表。对于
dict
,这实际上不是一个错误。
dict
可以在
元组上键入。甚至不需要父母
insquare[x,y]
的工作原理相同。你说得对,没有读到
insquare
是一个dict,我以为它是一个列表。这只是我定义常量的方式。因为我不希望值被弄乱,但是在python3中,除非使用函数,否则永远无法确定。但它只是在整个时间内返回相同的值。是的,insquare不是