python 2.7 - 从a到b切片列表而不是b到c,

时间:2016-12-28 17:13:24

标签: python list variables indexing slice

嗨,我只是想从积分榜中获得积分。 这些步骤也在列表中,每一步都不同。

例如:按索引从0到10获取点数,而不是11到16点。

列表就是例子。真正的清单要大得多。

这是我的代码:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *


number=[10,5,2,20,..,4)
pointlist=[point1,point2,..,point900]
result=[]
a=0
i=0

for elem in number:
    result.append(list[a:a+number[i]])
    i+=1

print = result

在我收到错误的那一刻

TypeError: expected Array[Type], got slice
in line "result.append(list[a:a+number[i]])"

我做错了什么? 任何帮助都会很棒!

以下是类似主题的链接:Explain Python's slice notation

2 个答案:

答案 0 :(得分:1)

这就是你要找的东西:

number=[10,5,2,20,12,15,6,9,12,14,4]
pointlist=[2,4,7,10]
result=[]
index = 0

for elem in pointlist:
    result.append(number[index:elem])
    index = elem
result.append(number[index:])
print(result)

输出:

[[10, 5], [2, 20], [12, 15, 6], [9, 12, 14], [4]]

答案 1 :(得分:1)

您使用了错误的名称false,但您拥有import UIKit class ViewController: UIViewController, UIScrollViewDelegate { var colorView: UIView! var scrollView: UIScrollView! override func viewDidLoad() { super.viewDidLoad() colorView = UIView() scrollView = UIScrollView() let colorButton = UIBarButtonItem(title: "Colors", style: .plain, target: self, action: #selector(colorButtonTapped)) colorButton.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 14.0), NSForegroundColorAttributeName: UIColor.black], for: UIControlState()) navigationController?.navigationBar.topItem?.rightBarButtonItem = colorButton } func colorButtonTapped() { let colorOptions = ["Blue", "Black", "Red", "Green", "Yellow", "Purple", "Orange", "Pink", "Magenta", "Lavender", "Beige", "Tan", "Burgundy", "Eggshell", "Brown"] let y = (navigationController?.navigationBar.frame.height)! + UIApplication.shared.statusBarFrame.height scrollView.frame = CGRect(x: UIScreen.main.bounds.width / 2.0, y: y, width: UIScreen.main.bounds.width / 2.0, height: UIScreen.main.bounds.height / 2.0) scrollView.delegate = self scrollView.isScrollEnabled = true let buttonHeight: CGFloat = UIScreen.main.bounds.height / 15 let contentHeight: CGFloat = CGFloat(colorOptions.count) * buttonHeight colorView.frame = CGRect(x: 0, y: 0, width: scrollView.frame.width, height: contentHeight) colorView.layer.cornerRadius = 8 colorView.clipsToBounds = true colorView.isUserInteractionEnabled = false scrollView.contentSize = CGSize(width: UIScreen.main.bounds.width / 2.0, height: contentHeight) for (index, title) in colorOptions.enumerated() { let button = UIButton(type: .custom) button.backgroundColor = .red button.frame = CGRect(x: 0, y: buttonHeight * CGFloat(index), width: UIScreen.main.bounds.width / 2.0, height: buttonHeight) button.setTitle(title, for: .normal) colorView.addSubview(button) } scrollView.addSubview(colorView) view.addSubview(scrollView) } }

您不需要list,因为您可以使用pointlist

i

结果

elem