很抱歉,您没有提供需要改写的句子。请提供您希望改写的句子,我将为您改写为一个长尾词的。
- 内容介绍
- 文章标签
- 相关推荐
本文共计685个文字,预计阅读时间需要3分钟。
Python 支持多种复合数据类型,可把不同值组合在一起。最常用的是列表,用方括号[]标示,元素间用逗号分隔。列表可包含不同类型的元素,但通常情况下,各元素类型相同:+s
Python 支持多种复合数据类型,可将不同值组合在一起。最常用的列表,是用方括号标注,逗号分隔的一组值。列表可以包含不同类型的元素,但一般情况下,各个元素的类型相同:
>>> squares
[1, 4, 9, 16, 25]
和字符串(及其他内置sequence类型)一样,列表也支持索引和切片:
>>> squares[0] # indexing returns the item1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
切片操作返回包含请求元素的新列表。以下切片操作会返回列表的浅拷贝:
>>> squares[:][1, 4, 9, 16, 25]
列表还支持合并操作:
>>> squares + [36, 49, 64, 81, 100][1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
与immutable字符串不同, 列表是mutable类型,其内容可以改变:
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
append()方法可以在列表结尾添加新元素(详见后文):
>>> cubes.append(216) # add the cube of 6>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
为切片赋值可以改变列表大小,甚至清空整个列表:
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
内置函数len()也支持列表:
>>> letters = ['a', 'b', 'c', 'd']>>> len(letters)
4
还可以嵌套列表(创建包含其他列表的列表),例如:
>>> a = ['a', 'b', 'c']>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'
本文共计685个文字,预计阅读时间需要3分钟。
Python 支持多种复合数据类型,可把不同值组合在一起。最常用的是列表,用方括号[]标示,元素间用逗号分隔。列表可包含不同类型的元素,但通常情况下,各元素类型相同:+s
Python 支持多种复合数据类型,可将不同值组合在一起。最常用的列表,是用方括号标注,逗号分隔的一组值。列表可以包含不同类型的元素,但一般情况下,各个元素的类型相同:
>>> squares
[1, 4, 9, 16, 25]
和字符串(及其他内置sequence类型)一样,列表也支持索引和切片:
>>> squares[0] # indexing returns the item1
>>> squares[-1]
25
>>> squares[-3:] # slicing returns a new list
[9, 16, 25]
切片操作返回包含请求元素的新列表。以下切片操作会返回列表的浅拷贝:
>>> squares[:][1, 4, 9, 16, 25]
列表还支持合并操作:
>>> squares + [36, 49, 64, 81, 100][1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
与immutable字符串不同, 列表是mutable类型,其内容可以改变:
>>> cubes = [1, 8, 27, 65, 125] # something's wrong here>>> 4 ** 3 # the cube of 4 is 64, not 65!
64
>>> cubes[3] = 64 # replace the wrong value
>>> cubes
[1, 8, 27, 64, 125]
append()方法可以在列表结尾添加新元素(详见后文):
>>> cubes.append(216) # add the cube of 6>>> cubes.append(7 ** 3) # and the cube of 7
>>> cubes
[1, 8, 27, 64, 125, 216, 343]
为切片赋值可以改变列表大小,甚至清空整个列表:
>>> letters = ['a', 'b', 'c', 'd', 'e', 'f', 'g']>>> letters
['a', 'b', 'c', 'd', 'e', 'f', 'g']
>>> # replace some values
>>> letters[2:5] = ['C', 'D', 'E']
>>> letters
['a', 'b', 'C', 'D', 'E', 'f', 'g']
>>> # now remove them
>>> letters[2:5] = []
>>> letters
['a', 'b', 'f', 'g']
>>> # clear the list by replacing all the elements with an empty list
>>> letters[:] = []
>>> letters
[]
内置函数len()也支持列表:
>>> letters = ['a', 'b', 'c', 'd']>>> len(letters)
4
还可以嵌套列表(创建包含其他列表的列表),例如:
>>> a = ['a', 'b', 'c']>>> n = [1, 2, 3]
>>> x = [a, n]
>>> x
[['a', 'b', 'c'], [1, 2, 3]]
>>> x[0]
['a', 'b', 'c']
>>> x[0][1]
'b'

