切片索引必须是整数或无,或者在对列表进行双节切割时具有__index__方法类型错误

问题描述 投票:0回答:1

我在尝试运行代码以截取cItems列表时遇到上述错误 - 代码取自Python 2.7,现在需要运行3.6 - 任何想法?

    cItems = [[8, 3, 9, 2, 10, 1, 7, 5, 4, 6]]

    cItems=[i[j:k] for i in cItems for j,k in ((0,len(i)/2), (len(i)/2,len(i))) if len(i)>1] # bi-section

    TypeError: slice indices must be integers or None or have an __index__ method
python hierarchical-clustering bisection
1个回答
0
投票
cItems=[i[j:k] for i in cItems for j,k in ((0,int(len(i)/2)),(int(len(i)/2),len(i))) if len(i)>1]

这将是你需要的改变

© www.soinside.com 2019 - 2024. All rights reserved.