2-3 列表

1.引出list

如何通过一个变量存储公司所有员工的名字?

    

 

2.列表的功能

 

 

 

3.列表的定义和创建

  定义:[]内以逗号分隔,按照索引,存放各种数据类型,每个位置代表一个元素

# list创建1 (推荐)
>>> names = []
>>> names
[]

# list创建2
>>> list()
[]
>>> names = list()
>>> names
[]

# list创建3
>>> names = list(['alex',1])
>>> names
['alex', 1]

 

 

4.列表的特点和常用操作

特性:

  1.可存放多个值

  2.按照从左到右的顺序定义列表元素,下标从0开始顺序访问,有序

  

  3.可修改指定索引位置对应的值,可变

 

5.查询

>>> names = ['alex','jack','shanshan',4,4,5,6,7,7,8,9,4,4]

>>> names[0]     # 通过索引取值
'alex'
>>> names[-1]    # 通过索引从list右边开始取值
4
>>> names[-2]
4
>>> names[-3]
9


>>> names.index(6)  #返回制定元素的索引值,从左右查找,找到第一个匹配值 则返回
6
>>> names.index(4)
3


>>> i = names.index(6)
>>> names[i]
6
>>> names[names.index(6)]
6

 

 

 

>>> dir(names)   #list的所有方法
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__'
, '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__'
, '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__iter__', '__le__'
, '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_e
x__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__s
izeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'ex
tend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
>>>

>>> names.count(4)      #统计制定元素的个数
4
>>> names.count(6)
1
>>> names.count(7)
2

 

 

6.切片

 

>>> names
['alex', 'jack', 'shanshan', 4, 4, 5, 6, 7, 7, 8, 9, 4, 4]
>>>
>>> names[0-2]   # names[-1]
4
>>> names[0-3]   # names[-2]
9
>>> names[0:3]
['alex', 'jack', 'shanshan']
>>>

 

 

   步长问题http://blog.csdn.net/u011508527/article/details/46389741

>>> names
['alex', 'jack', 'shanshan', 4, 4, 5, 6, 7, 7, 8, 9, 4, 4]

>>> names[3:0:-1]
[4, 'shanshan', 'jack']

  

   

 

7.增加和修改

  追加

  

  插入

  

  修改

  

  批量修改

  

 

 8.删除

   pop删除最后一个元素

  

  remove删除从左找到的第一个值

  

  del 全局删除

  

 

9.循环

  

>>> names
['alex', 5, 6, 7, 8, 9, 4, 4]
>>>
>>> for k in names:
...     print('loop',k)
...
loop alex
loop 5
loop 6
loop 7

loop 8
loop 9
loop 4
loop 4
>>>

 

  •    for循环有边界
  •   while循环可以死循环下去

 

10 range

现在已经学了列表了,那现在同学们来一起创建一个从1-100的列表。

等1分钟,你们都打算怎么创建啊?

是直接从1写到100还是用循环?如果从1写到100未免太傻气了,用循环好像还好一点儿。但是用循环写还不够简单,在python里有一个现成的方法,可以直接生成一个1-100的数。

>>> range(1,100)
range(1, 100)
>>> list(range(1,100))
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 
 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56,
 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 
 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99]
>>> list(range(10))
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> list(range(0,100,2))
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 
56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98]

 

 

# python2
>>> range(0,10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> 
>>> for i in range(10):
...     print(i)
... 
0
1
2
3
4
5
6
7
8
9
>>> 
>>> type(range(0,10))
<type 'list'>

 

 

# python3
>>> range(0,10)
range(0, 10)
>>>
>>> for i in range(10):
...     print(i)
...
0
1
2
3
4
5
6
7
8
9
>>> range(0,10)
range(0, 10)
>>>
>>> type(range(0,10))
<class 'range'>

11.排序:按ASCII码值排序

  list是有序的,

    

  反转

    

    

 

 

7.clear

>>> n1.clear()
>>> n1
[]

 

 

8.copy

  

  

 

9. len,in,split和join

#长度
>>> len(l)
3

#包含
>>> 'seven' in l
True
>>> 'yuan' in l
False

 

#分割
>>> s = 'hello world'
>>> s.split(' ')
['hello', 'world']
>>> s2= 'hello,world'
>>> s2.split(',')

#连接
>>> l = ['hi','eva']
>>> '!'.join(l)
'hi!eva'

 

 

 

10.深copy,浅copy

 

  浅copy

>>> n1
['a', 'b', 'c', ['alex', 'jack']]

>>> n2 = n1.copy()
>>> n2
['a', 'b', 'c', ['alex', 'jack']]

>>> id(n1)
12269064
>>> id(n2)
12269704

>>> n1[-1][-1] = 'tom'
>>> n1
['a', 'b', 'c', ['alex', 'tom']]
>>> n2
['a', 'b', 'c', ['alex', 'tom']]

>>> id(n1[-1])
12269512
>>> id(n2[-1])
12269512

    

>>> import copy
>>> n1
['a', 'b', 'c', ['alex', 'tom']]

>>> n3 = copy.copy(n1)
>>> n3
['a', 'b', 'c', ['alex', 'tom']]

>>> n1[-1][-1] = '1111'
>>> n1
['a', 'b', 'c', ['alex', '1111']]
>>> n3
['a', 'b', 'c', ['alex', '1111']]

 

  

 

   深copy

>>> n1
['a', 'b', 'c', ['alex', '1111']]

>>> n4 = copy.deepcopy(n1)
>>> n4
['a', 'b', 'c', ['alex', '1111']]

>>> n1[-1][-1] = '222'
>>> n1
['a', 'b', 'c', ['alex', '222']]
>>> n4
['a', 'b', 'c', ['alex', '1111']]

>>> id(n1[-1])
12269512
>>> id(n4[-1])
12269960

  

 

 

 

 11.list练习题

   

 

posted @ 2018-01-26 14:19  venicid  阅读(214)  评论(0编辑  收藏  举报