开发者

Colon (:) in Python list index [duplicate]

开发者 https://www.devze.com 2023-01-22 07:32 出处:网络
This question already has answers here: Understanding slicing (38 answers) Closed 8 years ago. I\'m new to Python. I see : used in list indices especially when it\'s associated with funct
This question already has answers here: Understanding slicing (38 answers) Closed 8 years ago.

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

Python 2.7 documentation suggests that lists.append translates to a[len(a):] = [x]. Why does o开发者_如何学JAVAne need to suffix len(a) with a colon?

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


: is the delimiter of the slice syntax to 'slice out' sub-parts in sequences , [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=41m40s at around 40:00 he starts explaining that.

Works with tuples and strings, too.


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


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号