Python:从列表中获取前 10 个结果

标签 python

有没有一种方法可以从列表中获取前 10 个结果。可能是这样的:

list = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20]

list.fetch(10)

?

最佳答案

list[:10]

将使用切片为您提供此列表的前 10 个元素。

但是,请注意,最好不要使用 list 作为变量标识符,因为它已被 Python 使用:list()

要了解有关此类操作的更多信息,您可以找到 tutorial on lists有帮助,@DarenThomas 提供了链接 Explain Python's slice notation - 谢谢达人)

关于Python:从列表中获取前 10 个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10897339/

相关文章:

python - 使用模块 __getattr__ 时如何回退到默认值

python - XPath 通过正则表达式运行 text() 仅返回数字

python - Django crontab 不在后台运行

python - 如何在Python中声明32位二进制数和移位列表?

二进制搜索算法的 Python 代码无法编译

python - 从 google places API 获取开放时间

Python 错误 :'numpy.float64' 对象不可调用

python - 使用 tkinter 生成随机数

python - Django 管理员说 : Select a valid choice. That choice is not one of the available choices

python - 使用 SymPy 的求解函数反汇编 Python 程序(幕后发生了什么?)