Python 列表索引中的冒号 (:)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4012340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 13:50:55  来源:igfitidea点击:

Colon (:) in Python list index

python

提问by kuriouscoder

I'm new to Python. I see :used in list indices especially when it's associated with function calls.

我是 Python 的新手。我看到:在列表索引中使用,特别是当它与函数调用相关联时。

Python 2.7 documentation suggests that lists.appendtranslates to a[len(a):] = [x]. Why does one need to suffix len(a)with a colon?

Python 2.7 文档建议lists.append转换为a[len(a):] = [x]. 为什么需要以len(a)冒号作为后缀?

I understand that :is used to identify keys in dictionary.

我知道它:用于识别字典中的键。

采纳答案by soulseekah

:is the delimiter of the slice syntax to 'slice out' sub-parts in sequences , [start:end]

:是切片语法的分隔符,用于“切出”序列中的子部分, [start:end]

[1:5] is equivalent to "from 1 to 5" (5 not included)
[1:] is equivalent to "1 to end"
[len(a):] is equivalent to "from length of a to end"

Watch https://youtu.be/tKTZoB2Vjuk?t=41m40sat around 40:00 he starts explaining that.

在 40:00 左右观看https://youtu.be/tKTZoB2Vjuk?t=41m40s,他开始解释。

Works with tuples and strings, too.

也适用于元组和字符串。

回答by joni

slicing operator. http://docs.python.org/tutorial/introduction.html#stringsand scroll down a bit

切片运算符。http://docs.python.org/tutorial/introduction.html#strings并向下滚动一点

回答by GISman

a[len(a):]- This gets you the length of a to the end. It selects a range. If you reverse a[:len(a)]it will get you the beginning to whatever is len(a).

a[len(a):]- 这会让你得到 a 到最后的长度。它选择一个范围。如果你扭转 a[:len(a)]它会让你开始到任何事情len(a)