Python08:列表

列表:python中以[]表示一个列表,取其中一个值是变量名[n]n是代表列表中的下标值。

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
print(names)

 

输出:

['One', 'Two', 'Three', 'four']

 

Process finished with exit code 0

解释:

names = ["One","Two","Three","four"]:定义列表变量names,并设定names的值

print(names):打印变量names

 

操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
print(names[2])

 

输出:

Three

 

Process finished with exit code 0

解释:

print(names[2]):查询下标为2的元素(下标从0开始)。

 

切片操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
print(names[1:2])

 

输出:

['Two']

 

Process finished with exit code 0

解释:

print(names[1:2]):切片操作,起始位置为1,终止位置为2,包含起始而不包含终止位置。

 

查询操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
print(names[-1])

 

输出:

four

 

Process finished with exit code 0

解释:

print(names[-1]):查询最后一个元素。

 

切片操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
print(names[-1:-3])

 

输出:

[]

 

Process finished with exit code 0

解释:

print(names[-1:-3]):错误的切片操作,起始位置要在终止位置的前边,故输出为空。

 

切片操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
print(names[-3:-1])

 

输出:

['Two', 'Three']

 

Process finished with exit code 0

解释:

print(names[-3:-1]):同上一步比较,这个是正常的用负数作切片查询操作。

 

切片操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
print(names[-2:])

 

输出:

['Three', 'four']

 

Process finished with exit code 0

解释:

print(names[-2:]):最后一位参数为空,则取到列表最后,此时包含最后一位元素。

 

切片操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
print(names[0:3])
print(names[:3])

 

输出:

['One', 'Two', 'Three']

['One', 'Two', 'Three']

 

Process finished with exit code 0

解释:
print(names[:3]):如果起始索引不写,则从0开始查找,同print(names[0:3])一样的结果。

 

添加操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
names.append("LeiMing")
print(names)

 

输出:

['One', 'Two', 'Three', 'four', 'LeiMing']

 

Process finished with exit code 0

解释:

names.append("LeiMing"):向列表最后追加元素。

 

添加操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
names.insert(1,"Buck")
print(names)

 

输出:

['One', 'Buck', 'Two', 'Three', 'four']

 

Process finished with exit code 0

解释:

names.insert(1,"Buck"):向指定位置添加元素,第一个参数是下标位置,第二个参数是添加的值。

 

修改操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
names[2] = "Join"
print(names)

输出:

['One', 'Two', 'Join', 'four']

 

Process finished with exit code 0

解释:

names[2] = "Join":元素的修改,找到修改的下标,直接赋值。

 

删除操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
names.remove("Three")
print(names)

 

输出:

['One', 'Two', 'four']

 

Process finished with exit code 0

解释:

names.remove("Three"):删除元素,参数填写要删除的值,直接删除。

 

删除操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
del names[1]
print(names)

 

输出:

['One', 'Three', 'four']

 

Process finished with exit code

解释:

del names[1]:删除下标为1的元素。

 

删除操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
names.pop()
print(names)

 

输出:

['One', 'Two', 'Three']

 

Process finished with exit code 0

解释:

names.pop():删除操作,不写参数默认删除最后一位元素。

 

删除操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
names.pop(1)
print(names)

 

输出:

['One', 'Three', 'four']

 

Process finished with exit code 0

解释:

names.pop(1):删除元素,填写数字,删除对应的下标的元素。

 

操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
print(names.index("Two"))

 

输出:

1

 

Process finished with exit code 0

解释:

print(names.index("Two")):查询列表中元素是”Two”的下标。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four","Two","Two"]
print(names.count("Two"))

 

输出:

3

 

Process finished with exit code 0

解释:

print(names.count("Two")):统计元素个数,此次统计为”Two”的元素个数。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","four"]
names.reverse()
print(names)

 

输出:

['four', 'Three', 'Two', 'One']

 

Process finished with exit code 0

解释:

names.reverse():反转列表中的元素。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","2Two","#Three","four"]
names.sort()
print(names)

 

输出:

['#Three', '2Two', 'One', 'four']

 

Process finished with exit code 0

解释:

names.sort():给列表中的元素排序,默认原则是以ASCII码的顺序,特殊字符、数字、大写字母、小写字母常见的。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","2Two","#Three","four"]
names.clear()
print(names)

 

输出:

[]

 

Process finished with exit code 0

解释:

names.clear():清空列表。

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","2Two","#Three","four"]
names2 = ["HanMei","LiLei","FengHua"]
names.extend(names2)
print(names)
print(names2)

 

输出:

['One', '2Two', '#Three', 'four', 'HanMei', 'LiLei', 'FengHua']

['HanMei', 'LiLei', 'FengHua']

 

Process finished with exit code 0

解释:

names.extend(names2):将列表names2追加到names

 

方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","2Two","#Three","four"]
names2 = ["HanMei","LiLei","FengHua"]
del names2
print(names)
print(names2)

 

输出:

['One', '2Two', '#Three', 'four']

Traceback (most recent call last):

  File "E:/python_code/s14/day2/names.py", line 10, in <module>

    print(names2)

NameError: name 'names2' is not defined

 

Process finished with exit code 1

解释:

del names2:删除names2,打印列表names2的时候报错,因为已删除。

 

浅拷贝方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","Four","Five"]
names2 = names.copy()

print(names)
print(names2)

 

输出:

['One', 'Two', 'Three', 'Four', 'Five']

['One', 'Two', 'Three', 'Four', 'Five']

 

Process finished with exit code 0

解释:

names2 = names.copy():列表的拷贝,此方法是浅拷贝,何为浅拷贝,参考后边讲解。

 

浅拷贝方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","Four","Five",["JACK","ROSE"]]
names2 = names.copy()
names2[2] = "mclind"
names2[5][0] = "jack"

print(names)
print(names2)

 

输出:

['One', 'Two', 'Three', 'Four', 'Five', ['jack', 'ROSE']]

['One', 'Two', 'mclind', 'Four', 'Five', ['jack', 'ROSE']]

 

Process finished with exit code 0

解释:

可以看到,拷贝完列表,当改变names2当中的字符串时,只改变names2中的字符串,names当中的字符串不变,当改变names2列表当中的列表时,names当中的列表也跟着变化了。这是因为在浅复制当中,复制的是地址,并不是新创建了地址空间。字符串的是无法改变的(要想修改指向的字符串,变量名看起来不变,其实是新创建了一个字符串,变量名指向了新创建的字符串的地址,变量的值也就跟着变化了,而原来字符串的地址是不变的),所以names[2]指向的是字符串, 修改names[2]是指向了新的地址,而names没有修改,地址不变,值不受影响。当修改names[5][0]的时候,修改的是列表中列表,而列表中的列表的地址并没有改变,由于names[5]指向的地址没有改变,但names2修改了其中的内容,所以names指向的地址的内容也变化了,所以names2[5][0]的修改也影响了names的内容。

用指针更容易理解和解释,但python中并没有指针的概念。

浅拷贝还有别的方法:names2=copy.copy(names) 注:需导入copy模块。

                    names2=names[:]

                    names2=list(names)三种结果一样,浅拷贝。

 

 

深复制方法操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind

import copy

names = ["One","Two","Three","Four","Five",["JACK","ROSE"]]
names2 = copy.deepcopy(names)
names2[2] = "mclind"
names2[5][0] = "jack"

print(names)
print(names2)

 

输出:

['One', 'Two', 'Three', 'Four', 'Five', ['JACK', 'ROSE']]

['One', 'Two', 'mclind', 'Four', 'Five', ['jack', 'ROSE']]

 

Process finished with exit code 0

解释:

names2 = copy.deepcopy(names):导入copy模块,再使用深拷贝方法deepcopy(),深拷贝是完全独立的复制出来列表,开辟新的内存空间,一般不使用。

 

for循环的别样操作:

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#Author:Mclind


names = ["One","Two","Three","Four","Five",["JACK","ROSE"]]
for i in names:
    print(i)

 

输出:

One

Two

Three

Four

Five

['JACK', 'ROSE']

 

Process finished with exit code 0

解释:

遍历列表中的元素,然后打印出来。这时候也可以用步长切片。

posted @ 2018-04-04 09:33  mclind  阅读(69)  评论(0编辑  收藏  举报