简体   繁体   English

Python integer 无限切片

[英]Python integer infinity for slicing

I have defined a slicing parameter in a config file:我在配置文件中定义了一个切片参数:

max_items = 10

My class slices a list according to this parameter:我的 class 根据这个参数切片一个列表:

items=l[:config.max_itmes]

When max_items = 0 , I want all items to be taken from l .max_items = 0时,我希望从l中获取所有项目。 The quick and dirty way is:快速而肮脏的方法是:

config.max_items=config.max_items if config.max_items>0 else 1e7

Assuming that there will be less then 1e7 items.假设将有少于1e7个项目。 However, I don't fancy using magic numbers.但是,我不喜欢使用幻数。 Is there a more Pythonic way of doing it, like an infinity integer constant?有没有更 Pythonic 的方法,比如无穷大 integer 常数?

There is no "infinity integer constant" in Python, but using None in a slice will cause it to use the default for the given position, which are the beginning, the end, and each item in sequence, for each of the three parts of a slice. Python 中没有“无穷大 integer 常量”,但在切片中使用None将导致它使用给定 position 的默认值,依次是每个项目的开头和结尾的三个部分一片。

>>> 'abc'[:None]
'abc'

Have you tried with sys.maxint ?您是否尝试过sys.maxint

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM