Python---list列表

列表是一种可变的(mutable),属于容器的一种。list作容器的一种,可以容纳不同的数据。对于list的可变性,就像C++里的vector一样,长度是可变的。
list作为一种数据类型,拥有多种方法:

1.list的方法

  • list.append(x)
    将一个数据项附加到列表的末尾,相当于 a[len(a):] = [x]
>>>l = ['f','z',1,'a',5,'b','d','c','abd','ead']
>>>l.append(5)
>>>l
['f', 'z', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead', 5]
>>> l.append(3,3)                  //只接受一个参数哦
Traceback (most recent call last):
  File "<pyshell#116>", line 1, in <module>
    l.append(3,3)
TypeError: append() takes exactly one argument (2 given)
  • list.extend(L)
    对列表进行扩展,将L逐个添加到列表中,若L不是列表,则会把L转化为列表.相当于a[len(a):] = L
>>> l.extend([0,'1'])
>>> l
['f', 'z', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead', 5, 0, '1']
  • list.insert(i, x)
    将一个数据项插入到列表的指定位置.第一个参数是插入位置(按照偏移量计算),第二个参数是要插入的数据。
>>>l = ['f','z',1,'a',5,'b','d','c','abd','ead']
>>> l.insert(0,'0')
>>> l
['0', 'f', 'z', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead']
  • list.remove(x)
    将指定的元素从list中移除,如果list中不存在这个元素则报错。无返回值。
>>> l
['0', 'f', 'z', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead']
>>> l.remove('0')
>>> l
['f', 'z', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead']
>>> l.remove('0')
Traceback (most recent call last):
  File "<pyshell#129>", line 1, in <module>
    l.remove('0')
ValueError: list.remove(x): x not in list
  • list.pop([i])
    从list中移除一个元素,如果制定了移除元素的位置,则移除指定位置的元素,若没有指定则移除最后一个元素,并返回被移除的元素。
>>> l
['f', 'z', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead']
>>> l.pop()           //删除最后一个元素
'ead'
>>> l
['f', 'z', 1, 'a', 5, 'b', 'd', 'c', 'abd']
>>> l.pop(1)          //删除指定位置的元素
'z'
>>> l
['f', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead']
  • list.clear()
    将list清空,等价于 del a[:]

  • list.index(x)
    返回指定元素在列表中的偏移量(从0开始计数),如果存在多个相同的元素,则返回第一个元素的下标,如果不存在指定元素,则返回错误。

>>> l.index('f')
0
  • list.count(x)
    返回指定元素在列表中出现的次数。
>>> l.count('a')
1
  • list.sort(key=None, reverse=False)
    对列表进行排序,会改变列表本身的顺序,无返回值。也可以指定参数,sort默认是按照升序来排序的,指定reverse = true,则按照降序排序;指定参数key,可以按照key的值进行排序。这个key可以是按照元素的某个属性,也可以是对元素进行某种操作后,按照操作后的结果进行排序。
>>> l.sort()             //若列表中数据类型不同,是不可排序的,但是仍然可以指定key,按照某种方法进行排序
Traceback (most recent call last):
  File "<pyshell#145>", line 1, in <module>
    l.sort()
TypeError: unorderable types: int() < str()
>>> l.sort(key = lambda x:len(str(x)))
>>> l
['f', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead']
>>> sl = ['aa','c','wefw','1223','23333','']
>>> sl
['aa', 'c', 'wefw', '1223', '23333', '']
>>> sl.sort(key = len)
>>> sl
['', 'c', 'aa', 'wefw', '1223', '23333']
>>> sl.sort(reverse = True)
>>> sl
['wefw', 'c', 'aa', '23333', '1223', '']
  • list.reverse()
    对列表中的元素逆序排列。

  • list.copy()
    对列表进行潜复制,并返回潜复制。

>>> l
['f', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead']
>>> l_copy = l.copy()
>>> l_copy
['f', 1, 'a', 5, 'b', 'd', 'c', 'abd', 'ead']

2.list的构建方法

list的元素是包含在 ‘[ ]‘中的。
1.直接创建

>>> l = ['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
>>> l
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

2.用字符串进行创建

>>> s = 'hello world!'
>>> l = [x for x in s]
>>> l
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']
>>> l = list(s)
>>> l
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd', '!']

这种方法是列表推倒的一种,列表推导可以帮助我们把一个序列或者其他可迭代类型中的元素过滤或是加工,然后在新建一个列表。

>>> color = ['red','blue','green']
>>> size = ['s','l','m']
>>> tshirts = [(c,s) for c in color for s in size]
>>> tshirts
[('red', 's'), ('red', 'l'), ('red', 'm'), ('blue', 's'), 
('blue', 'l'), ('blue', 'm'), ('green', 's'), ('green', 'l'), ('green', 'm')]

由上可知,用列表推导可以生成两个或两个以上的可迭代类型的笛卡尔积。

猜你喜欢

转载自blog.csdn.net/miha_singh/article/details/81075693