Python 3 教程 在线

1068Python3 列表

Python 列表函数&方法的对象不仅可以是字符串也可以是列表。

list1 = ["Googl",'facesho',1997,2002]
list2 = [1,2,3,4,5,6]
list2.append(list1)print ("list2:",list2)

输出结果如下:

list2: [1, 2, 3, 4, 5, 6, ['Googl', 'facesho', 1997, 2002]]
简单理解:python 在操作对象的时候,会根据自身对对象的定义进行操作,这里 list1 被定义的就是列表,那么 list.append(obj)在操作 list1 时就作为列表追加。

1067Python3 列表

这个例子说明了 Python 列表是链式存储结构,并非顺序存储。

a=[1,2,3,4]
for i in range(len(a)):
    print(id(a[i]))
a[1]=100
print("----------")
for i in range(len(a)):
    print(id(a[i]))

输出结果:

4471499232
4471499264
4471499296
4471499328
----------
4471499232
4471502400
4471499296
4471499328

1066Python3 列表

1 通过列表切片方式复制列表:

1.1 列表复制

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods[:]
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

输出:

My favorite foods are:
['pizza', 'falafel', 'carrot cake'] 
My friend's favorite foods are: 
['pizza', 'falafel', 'carrot cake']

1.2 验证确实实现了两个列表

my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:")
print(my_foods)
print("\nMy friend's favorite foods are:")
print(friend_foods)

输出:

My favorite foods are: 
 ['pizza', 'falafel', 'carrot cake', 'cannoli'] 
My friend's favorite foods are: 
 ['pizza', 'falafel', 'carrot cake', 'ice cream']

可以看出通过切片方式复制列表,结果是生成了两个列表。

2 通过简单赋值方式复制列表:

my_foods = ['pizza', 'falafel', 'carrot cake']
friend_foods = my_foods
my_foods.append('cannoli')
friend_foods.append('ice cream')
print("My favorite foods are:") 
print(my_foods) 
print("\nMy friend's favorite foods are:") 
print(friend_foods)

输出:

My favorite foods are: 
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream'] 
My friend's favorite foods are: 
['pizza', 'falafel', 'carrot cake', 'cannoli', 'ice cream']

可以看出两个列表是相同的,这并非我们想要的结果。

1065Python3 列表

在此教程中未提到列表的切片,在这里简单的说明一下。

格式: 【start:end:step】

  • start:起始索引,从0开始,-1表示结束
  • end:结束索引
  • step:步长,end-start,步长为正时,从左向右取值。步长为负时,反向取值

举例说明:

>>> a=[1,2,3,4]
>>> b='abcdef'
>>> print(a[1:2])
[2]
>>> print(b[2:])
cdef
>>> print(a[::-1])
[4, 3, 2, 1]
>>> print(b[::-1])
fedcba

如果不是同样追求语法细节的“老鸟”,这段代码的作用恐怕不能第一眼看出来,实际上为了更好的体现 pythonic 的代码是充分利用 python 库里的 reversed() 函数。

>>> print(list(reversed(a)))
[4, 3, 2, 1]
>>> print(list(reversed(b)))
['f', 'e', 'd', 'c', 'b', 'a']

1064Python3 列表

感觉少了列表推导式的讲解 (这个功能很方便的)

1、列表推导式书写形式:  

[表达式 for 变量 in 列表]
或者
[表达式 for 变量 in 列表 if 条件]

2、举例说明:

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

li = [1,2,3,4,5,6,7,8,9]
print ([x**2 for x in li])

print ([x**2 for x in li if x>5])

print (dict([(x,x*10) for x in li]))


print ([ (x, y) for x in range(10) if x % 2 if x > 3 for y in range(10) if y > 7 if y != 8 ])

vec=[2,4,6]
vec2=[4,3,-9]
sq = [vec[i]+vec2[i] for i in range(len(vec))]
print (sq)

print ([x*y for x in [1,2,3] for y in  [1,2,3]])

testList = [1,2,3,4]
def mul2(x):
    return x*2
print ([mul2(i) for i in testList])

结果:

[1, 4, 9, 16, 25, 36, 49, 64, 81]
[36, 49, 64, 81]
{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60, 7: 70, 8: 80, 9: 90}
[(5, 9), (7, 9), (9, 9)]
[6, 7, -3]
[1, 2, 3, 2, 4, 6, 3, 6, 9]
[2, 4, 6, 8]

3、总结:

我觉得就是通过 for 语句处理表达式里面的变量,如果还要加条件,就加 if 条件就可以了。