Python 学习笔记04【列表】

列表中内容可以使不同的类型,如 list2 = [1,2,3,'asdf','sd']

4.1.1 用下标取得列表中的单个值

spam=['cat', 'dog', 'rat', 'elephant']
spam[1]
'dog'

4.1.2 负数下标

下标0:代表从前数的第一个值

下表-1:代表从后数的第一个值

spam=['cat', 'dog', 'rat', 'elephant']
spam[-1]
'elephant'

spam[-2]
'rat'

4.1.3 利用切片取得子列表

1 spam[1:3] #不包括下表为3的元素
2 ['dog', 'rat']

4.1.4 使用len()取得列表的长度

4.1.5 使用下标改变列表的值

spam[1] = 'change'

4.1.6 列表连接和列表复制

[1,2,3] + [5,6,7,8]

[1,2,3]*3

4.1.7 使用del语句从列表中删除值

spam=['cat', 'dog', 'rat', 'elephant']
del spam[3]
spam ###
['cat', 'dog', 'rat']

4.2 列表使用

通过输入内容,加入列表

 1 catNames = []
 2 while True:
 3     print('Enter the name of cat #' + str(len(catNames) + 1) + ' Or enter nothing to stop: ')
 4     name = input()
 5     if name == '':
 6         break
 7     catNames = catNames + [name] #注意此处用法!!!
 8 print('The cat names are:')
 9 for catName in catNames:
10     print(' ' + catName)

4.2.1 列表用于循环

1 for i in [1,2,3,4,5,6,7,8,9]
2     print(i)

4.2.2 in 和 not in操作符

确定一个值是否在类表内

4.2.3 多重赋值技巧

>>> cat = ['fat','black','loud']
>>> size, color, disposition = cat
>>> print(size,color,disposition, sep='::')
fat::black::loud

4.2.4 增强的赋值操作

+=, -=, *=, /=, %=

4.4 方法 

4.4.1 用index()方法在列表中查找值: spam.index('hello'),列表中有多个hello,返回第一个hello的下标

4.4.2 用append() 和 insert()方法在列表中添加值

spam=['cat', 'dog', 'rat', 'elephant']
spam.append('moose')
spam.insert(1, 'chicken')

 4.4.3 用remove() 方法从列表中删除值

>>> spam.remove('rat')
>>> spam
['cat', 'dog']

亦可使用 del语句删除一些值,如何区分:

如果知道想要删除的值在列表中的下标,使用del语句

如果知道想要从列表中删除的值,使用remove()方法

4.4.4 使用sort()方法对列表排序

数值 或 字符 列表均可排序

1 >>> spam=['cat', 'dog', 'rat', 'elephant']
2 >>> spam.sort()
3 >>> spam
4 ['cat', 'dog', 'elephant', 'rat']
>>> spam.sort(reverse=True)
>>> spam
['rat', 'elephant', 'dog', 'cat']

sort()方法使用注意事项:

  • sort方法对列表当场排序
  • sort方法不能对既含有数字又含有字符串的列表排序
  • sort方法对字符串排序时,使用‘ASCII’字符顺序,即 大写字母在小写字母前。
    • 如果需要按照普通的字典顺序进行排序,就在sort调用时,将关键字key 设置为 str.lower,如下:
    • 1 >>> spam = ['y','T','f','A']
      2 >>> spam.sort()
      3 >>> spam
      4 ['A', 'T', 'f', 'y']#大写字母在前
      5 >>> spam.sort(key=str.lower)
      6 >>> spam
      7 ['A', 'f', 'T', 'y']#按照字典顺序排列,大小写混排,但实际上并没有改变列表的值

 4.6 类似列表的类型:字符串和元组

  • 字符串是单个文本字符的列表

4.6.1 可变和不可变数据类型

4.6.2 元组数据类型 : 元组定义使用(),而不是[],但是元组依然通过[下标]来获取元素

>>> eggs = ('hello','kitty',2)
>>> eggs[2]
2
>>> eggs[1]
'kitty'
  • 列表是可变数据类型
  • 字符串是不可变数据类型
  • 元组是不可变

 元组和列表的对比:

  • 如果使用永不改变的序列,使用元组
  • Python针对元组做了优化,使用元组的代码会比使用列表代码更快速

4.6.3 用list() 和 tuple()函数来转换类型

1 >>> tuple(['chicken','fish','dogs'])
2 ('chicken', 'fish', 'dogs')
3 
4 >>> list(('chicken', 'fish', 'dogs'))
5 ['chicken', 'fish', 'dogs']

4.7 引用

4.7.1 传递引用

 1 >>> spam2
 2 ['A', 'B', 'C', 'D', [1, 2, 3, 4]]
 3 >>> spam3 = spam2 #引用传递
 4 >>> spam3
 5 ['A', 'B', 'C', 'D', [1, 2, 3, 4]]
 6 >>> spam3[1] = 'dd'
 7 >>> spam3
 8 ['A', 'dd', 'C', 'D', [1, 2, 3, 4]]
 9 >>> spam2 # spam3的变化影响到spam2
10 ['A', 'dd', 'C', 'D', [1, 2, 3, 4]]

4.7.2 copy模块的copy()函数和deepCopy()函数

使用copy函数,而不是直接使用“=”来进行引用传递,主要是不希望两个变量指向同一个引用,彼此之间的变化不会相互影响。

 1 >>> import copy
 2 
 3 >>> spam = ['A','B','C','D']
 4 >>> cheese = copy.copy(spam)
 5 >>> spam == cheese
 6 True
 7 >>> cheese[1] = 42
 8 >>> spam
 9 ['A', 'B', 'C', 'D']
10 >>> cheese
11 ['A', 42, 'C', 'D']
12 >>> spam == cheese
13 False

经过copy.copy(), spam和cheese指向了不同的列表,因此spam和cheese的彼此变化不会相互影响。

猜你喜欢

转载自www.cnblogs.com/wooluwalker/p/11622801.html
今日推荐