繁体   English   中英

Python TypeError:元组索引必须是整数或切片,而不是元组?

[英]Python TypeError: Tuple indices must be integers or slices, not tuple?

试图让一大块代码工作,我的一个测试用例失败了。 它应该返回:

Invalid Point! (7, 8, 9, 10)
Invalid Point! ('hi', 2, 3)
((1, 2, 3), (4, 5, 6))

相反,我得到了标题中提到的 TypeError 和Invalid Point! (1, 2, 3) Invalid Point! (1, 2, 3)

以下代码供参考。

def collection_check(a_coll, coll_template):
#Receives a collection to check and a template collection to check against.  The procedure
#raises errors if the collection to check is the incorrect type, is the wrong length, or 
#contains data types that do not match the data types of the provided template.
    if not type(a_coll)==type(coll_template):
        out_str = "TypeError:{} and template are different classes."
        raise TypeError(out_str.format(a_coll))
    if not(len(a_coll)==len(coll_template)):
        out_str = "ValueError:{} and template are different lengths." 
        raise ValueError(out_str.format(a_coll))
    for i in range(len(a_coll)):
        if not(type(a_coll[i])==type(coll_template[i])):
            out_str = "TypeError:{} is not type {}"
            raise TypeError(out_str.format(a_coll[i],type(coll_template[i])))

def clean_points(point_tup):
#Receives a tuple of tuples containing values and returns a new nested tuple with all 
#of the original tuples that contain exactly three numbers.
    new_points = []
    for point in point_tup:
        try:
            collection_check(point_tup[point],(0,0,0))
            new_points.append(point[:])
        except (TypeError,ValueError) as e:
            print("Invalid Point! {}".format(point))
            break
    return list(new_points)

###    
#Test cases:
#   >>> clean_points(((1,2,3),(4,5,6),(7,8,9,10),('hi',2,3)))
#   Invalid Point! (7, 8, 9, 10)
#   Invalid Point! ('hi', 2, 3)
#   ((1, 2, 3), (4, 5, 6))
#   >>> clean_points((('h',0),(1,1,1),(2,2,2)))
#   Invalid Point! ('h', 0)
#   ((1, 1, 1), (2, 2, 2))
###

这是怎么回事? 我需要解决代码中的其他问题,但这是我发现的第一个障碍。

修改您的代码以打印错误 msg 最终显示了您在标题中提到的错误消息:

def collection_check(a_coll, coll_template):
#Receives a collection to check and a template collection to check against.  The procedure
#raises errors if the collection to check is the incorrect type, is the wrong length, or 
#contains data types that do not match the data types of the provided template.
    if not type(a_coll)==type(coll_template):
        out_str = "TypeError:{} and template are different classes."
        raise TypeError(out_str.format(a_coll))
    if not(len(a_coll)==len(coll_template)):
        out_str = "ValueError:{} and template are different lengths." 
        raise ValueError(out_str.format(a_coll))
    for i in range(len(a_coll)):
        if not(type(a_coll[i])==type(coll_template[i])):
            out_str = "TypeError:{} is not type {}"
            raise TypeError(out_str.format(a_coll[i],type(coll_template[i])))

def clean_points(point_tup):
#Receives a tuple of tuples containing values and returns a new nested tuple with all 
#of the original tuples that contain exactly three numbers.
    new_points = []
    for point in point_tup:
        try:
            collection_check(point_tup[point],(0,0,0))
            new_points.append(point[:])
        except (TypeError,ValueError) as e:
            print(e) # this will show the true error ========== right here 
            print("Invalid Point! {}".format(point))
            break
    return list(new_points)

您正在尝试使用元组访问元组元组,这对 python 没有意义,因为您需要使用整数索引访问元组元组。 像这样循环时

for point in point_tup:

点变量包含您的point_tup变量中的第 i 个元组,在您的point_tup之后,点将在每次迭代中等于以下

(1, 2, 3)
(4, 5, 6)
(7, 8, 9, 10)
('hi', 2, 3)

所以你不需要像这样得到点元组

point_tup[point]

你已经在point变量中拥有它

你的问题是这些行:

for point in point_tup:
    try:
        collection_check(point_tup[point],(0,0,0))

point是一个元组(通过遍历point_tup分配,而不是整数,所以你不能用它来索引point_tup 。很可能你只想要collection_check(point, (0, 0, 0))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM