首页 > 解决方案 > 如何切片 Python 列表项?

问题描述

我在 Python 中尝试了这段代码:

listepositive = soup.findAll('span', attrs{'class':'search_review_summary'})

pos = listpositive[0][3:5]

print(pos)

获取 listpositive 列表 0 char 3 到 5. Got TypeError: unhashable type: 'slice'...?!

你能给我一个提示吗?

标签: pythonbeautifulsoup

解决方案


将 numpy 导入为 np

  #pos = listpositive[0,2:6] you can do this if listpositive is already a numpy array
    newpos = np.array([listpositive])
    newpos[0,2:6]

如果你没有安装 numpy 并且你没问题


推荐阅读