gpt4 book ai didi

python - 如何从切片对象中提取第一个 int

转载 作者:行者123 更新时间:2023-11-30 23:22:02 25 4
gpt4 key购买 nike

我有一个切片对象,我想从中提取第一个 int 。该对象类型似乎是只读/不可变的。使用Python 2.7

foo = slice(22,24)

foo
Out[44]: slice(22, 24, None)

foo[0]
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-45-d8976430aff2> in <module>()
----> 1 foo[0]

TypeError: 'slice' object has no attribute '__getitem__'

我正在尝试将 22 作为 int 进行访问,以便稍后用于位置索引。大家有什么想法吗。
非常感谢

最佳答案

如果您引用slice(start, stop[, step])部分,它提到

Slice objects have read-only data attributes start, stop and step which merely return the argument values (or their default).

>>> foo = slice(22,24)
>>> foo.start
22

关于python - 如何从切片对象中提取第一个 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24691720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com