Python 100例
题目:将一个列表的数据复制到另一个列表中。
程序分析:使用列表[:]。
程序源代码:
以上实例输出结果为:
[1, 2, 3]
白木潇潇夕
[email protected]
使用 Python2 的参考解法:
>>> import copy >>> a = [1, 2, 3] >>> b=copy.copy(a) >>> print(b) [1, 2, 3]
使用 Python3 的参考解法:
>>> a = [1, 2, 3] >>> b=a.copy() >>> print(b) [1, 2, 3]
Chivalry
使用 for 循环出列表元素,并逐以放到新的列表中:
#!/usr/bin/python # -*- coding: UTF-8 -*- l = [1,2,3,4,5] p = [] for i in range(len(l)): p.append(l[i]) print p
白木潇潇夕
[email protected]
使用 Python2 的参考解法:
使用 Python3 的参考解法:
白木潇潇夕
[email protected]
Chivalry
[email protected]
使用 for 循环出列表元素,并逐以放到新的列表中:
Chivalry
[email protected]