为了账号安全,请及时绑定邮箱和手机立即绑定

如何确定索引是否为列表/数组?

如何确定索引是否为列表/数组?

莫回无 2022-06-14 14:59:23
我本质上是在尝试确定指定的索引是否是列表。我有一个嵌入到索引中的列表。这是我到目前为止所拥有的    if len(hashTable[hashed-1])>1:        hashTable[hashed-1].append(inVal)    else:        tempTable = [hashTable[hashed-1], inVal]          hashTable[hashed-1] = tempTable        print(inVal,hashed)        print(hashTable)列表的输出是:(0 和 10 无关)[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, [1, 3], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10]错误是:    if len(hashTable[hashed-1])>1:TypeError: object of type 'int' has no len()
查看完整描述

1 回答

?
慕码人2483693

TA贡献1860条经验 获得超9个赞

您必须使用isinstance来检查您的对象是否为列表类型,如下所示:

isinstance(hashTable[hashed-1], list)

在你的情况下:

if isinstance(hashTable[hashed-1], list) and len(hashTable[hashed-1]) > 1

Python 使用惰性评估来评估此 if 语句中的条件。所以只有在islen(hashTable[hashed-1])时才会被调用。isinstance(hashTable[hashed-1], list)True


查看完整回答
反对 回复 2022-06-14
  • 1 回答
  • 0 关注
  • 120 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号