python valueerror index_Python index error value not in list...on .index(value)

这篇博客是一名Python初学者在尝试处理二维数据集时遇到的问题。他们在检查子列表是否包含在主列表中时遇到了`ValueError: [0,3] is not in list`的错误。代码片段展示了他们如何遍历数据并尝试添加符合条件的元素到子集中,但出现了问题。他们使用了`subset.index(currentPosition)`来检查`[0,3]`是否在子集中,导致了错误。
摘要由CSDN通过智能技术生成

2012-08-23 17:25:36

0

I am a beginner at Python, and to those who holds negative thoughts against my post, please leave. I am simply seeking help here and trying to learn. I'm trying to check within a simple data set the 0s and 1s. This will be used towards defining voids and solids on floor plans to define zones in buildings... eventually 0s and 1s will be swapped out with coordinates.

I am getting this error: ValueError: [0, 3] is not in list

I am simply checking if one list is contained in the other.

currentPosition's value is [0, 3]

subset, [[0, 3], [0, 4], [0, 5], [1, 3], [1, 4], [1, 5], [2, 1], [3, 1], [3, 4], [3, 5], [3, 6], [3, 7]]

Here's the code snippet:

def addRelationship(locale, subset):

subset = []; subSetCount = 0

for rowCount in range(0, len(locale)):

for columnCount in range (0, int(len(locale[rowCount])-1)):

height = len(locale)

width = int(len(locale[rowCount]))

currentPosition = [rowCount, columnCount]

currentVal = locale[rowCount][columnCount]

print "Current position is:" , currentPosition, "=", currentVal

if (currentVal==0 and subset.index(currentPosition)):

subset.append([rowCount,columnCount])

posToCheck = [rowCount, columnCount]

print "*********************************************Val 0 detected, sending coordinate to check : ", posToCheck

newPosForward = checkForward(posToCheck)

newPosBackward = checkBackward(posToCheck)

newPosUp = checkUpRow(posToCheck)

newPosDown = checkDwnRow(posToCheck)

I am using subset.index(currentPosition) to check and see if [0,3] is in subset but getting the [0,3] is not in list. How come?

回答: 当你在Python中遇到"ValueError: too many values to unpack"错误时,通常是因为你尝试在遍历字典时同时遍历键和值。然而,Python只允许对字典键进行遍历,所以这种遍历方式是错误的。\[1\] 如果你想根据特定的键找到对应的值,你可以使用字典的get()方法。例如,如果你有一个字典d和一个包含特定键的列表l,你可以使用以下代码找到对应的值并打印出来: ```python d = {'张三': 'xxx', '李四': 'xxx'} l = \['张三', '李四'\] for name in l: try: print(d.get(name)) except KeyError: pass ``` 这样,你就可以根据特定的键找到对应的值,并将其打印出来。\[2\] 如果你想获取字典中所有的值,你可以使用字典的values()方法。例如,如果你有一个字典zidian,你可以使用以下代码获取所有的值: ```python zidian = {"country1": "america", "country2": "australia", "country3": "germany"} values = zidian.values() ``` 这样,你就可以得到一个包含字典中所有值的列表values。\[3\] #### 引用[.reference_title] - *1* [Python异常 ValueError](https://blog.csdn.net/weixin_48419914/article/details/122949553)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [pythonvalue的意思](https://blog.csdn.net/weixin_31842775/article/details/114939182)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值