1.list增加的使用方法
1.append使用方法
原地修改list,给list增加一个元素,append的方法的返回值是None
list.append(12)
In [4]: list.append(12)
In [5]: list
Out[5]: [1, 2, 3, 4, 5, 12]
In [8]: help(list.append)
2.insert的使用方法,根据索引位置添加.
insert操作的索引超出范围,如果是正索引,等效于append,如果是负索引.等效于insert(0, object)
In [10]: list
Out[10]: [1, 2, 3, 4, 5, 12]
In [11]: list.insert(0,7)
In [12]: list
Out[12]: [7, 1, 2, 3, 4, 5, 12]
In [13]: list.insert(3,0)
In [14]: list
Out[14]: [7, 1, 2, 0, 3, 4, 5, 12]
In [15]:
超出列表不存在的话
In [16]: list.insert(12,3)
In [17]: list
Out[17]: [7, 1, 2, 0, 3, 4, 5, 12, 3]
In [18]: list.insert(13,13)
In [19]:
3.extend的使用方法
把一个list扩展到我们的list后边
In [20]: list.extend([1,2,3])
In [21]: list
Out[21]: [7, 1, 2, 0, 3, 4, 5, 12, 3, 13, 1, 2, 3]
In [22]:
In [23]: help(list.extend)####学会使用
Help on built-in function extend:
extend(...) method of builtins.list instance
L.extend(iterable) -> None -- extend list by appending elements from the iterable
(END)
2.list删除练习
###删除的使用方法
1.pop的使用方法
index默认为-1 如果index超出索引范围.会抛出indexError
In [24]: list
Out[24]: [7, 1, 2, 0, 3, 4, 5, 12, 3, 13, 1, 2, 3]
In [25]: list.pop()
Out[25]: 3
In [26]: list
Out[26]: [7, 1, 2, 0, 3, 4, 5, 12, 3, 13, 1, 2]
In [27]:
Help on built-in function pop:
pop(...) method of builtins.list instance
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
In [29]: list.pop(0)###从正数第一个开始删除
Out[29]: 7
In [30]: list
Out[30]: [1, 2, 0, 3, 4, 5, 12, 3, 13, 1, 2]
In [31]:
In [31]: list
Out[31]: [1, 2, 0, 3, 4, 5, 12, 3, 13, 1, 2]
In [32]: list.pop(-1)###从负数第一个开始删除
Out[32]: 2
In [33]: list
Out[33]: [1, 2, 0, 3, 4, 5, 12, 3, 13, 1]
In [34]:
2.remove的方法使用
pop和remove比较
pop是针对的索引 remove是针对的值
pop 是弹出索引对应的值
remove 是删除最左边的一个值
In [34]: help(list.remove)
Help on built-in function remove:
remove(...) method of builtins.list instance
L.remove(value) -> None -- remove first occurrence of value.
Raises ValueError if the value is not present.
In [35]: list
Out[35]: [1, 2, 0, 3, 4, 5, 12, 3, 13, 1]
In [36]: list.remove(2)
In [37]: list
Out[37]: [1, 0, 3, 4, 5, 12, 3, 13, 1]
In [38]:
3.del删除的方式#使用比较少
4.clear的使用方法
####python2 没有copy这个参数
clear方式是删除所有的元素
In [38]: list
Out[38]: [1, 0, 3, 4, 5, 12, 3, 13, 1]
In [39]: list.clear()
In [40]: list
Out[40]: []
In [41]:
3.list查询统计练习
1.index的使用方法
In [43]: help(list.index)
Help on built-in function index:
index(...) method of builtins.list instance
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
In [44]: list=['a','b','c','d']
In [46]: list.index('b')
Out[46]: 1
In [47]: list.append('b')
In [48]: list.index('b')
Out[48]: 1
In [49]: list
Out[49]: ['a', 'b', 'c', 'd', 'b']
In [50]: list.index('b')
Out[50]: 1
In [51]:
In [51]: list.index('b', 2)
Out[51]: 4
In [52]:
####总结
如果不存在会抛出一个ValueError
start 包含 stop不包含
index 是根据值来找索引
2.count的使用方法
统计列表中有函数的个数
In [64]: list
Out[64]: ['a', 'b', 'c', 'd', 'b']
In [65]: list.count('b')
Out[65]: 2
In [66]:
3.len的使用方法
列表里面全部值的个数
In [67]: list
Out[67]: ['a', 'b', 'c', 'd', 'b']
In [68]: len(list)
Out[68]: 5
In [69]:
4.list修改列表练习
1.sort的使用方法
In [70]: help(list.sort)
Help on built-in function sort:
sort(...) method of builtins.list instance
L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
In [71]: help(list.sort)
In [72]: list.sort()
In [73]: list
Out[73]: ['a', 'b', 'b', 'c', 'd']
In [74]:
####倒排序
In [74]: list.sort(reverse=True)
In [75]: list
Out[75]: ['d', 'c', 'b', 'b', 'a']
In [76]:
2.reverse的使用方法
In [76]: help(list.reverse)
In [77]: list.reverse()
In [78]: list
Out[78]: ['a', 'b', 'b', 'c', 'd']
In [79]:
3.copy的使用方法
####python2 没有copy这个参数
In [80]: list1 = [1,3,2,4,6,5,7]
In [81]: list1
Out[81]: [1, 3, 2, 4, 6, 5, 7]
In [82]: list1.reverse()
In [83]: list1
Out[83]: [7, 5, 6, 4, 2, 3, 1]
In [84]: list1.reverse()
In [85]: list1
Out[85]: [1, 3, 2, 4, 6, 5, 7]
In [86]: list1.remove(2)
In [87]: list1
Out[87]: [1, 3, 4, 6, 5, 7]
In [88]:
In [88]: list1 = list
In [89]: list1
Out[89]: ['a', 'b', 'b', 'c', 'd']
In [90]: list
Out[90]: ['a', 'b', 'b', 'c', 'd']
In [93]: list
Out[93]: ['a', 'b', 'c', 'd']
In [94]: list1
Out[94]: ['a', 'b', 'c', 'd']
In [95]:
###如果为了不影响原来的可以使用list.copy,这样我们可以保证两个互相不影响
In [95]: list2 = list.copy()
In [96]: list2
Out[96]: ['a', 'b', 'c', 'd']
In [97]: list
Out[97]: ['a', 'b', 'c', 'd']
In [98]: id(list)
Out[98]: 140378675610824
In [99]: id(list2)
Out[99]: 140378675977352
In [100]: list.remove('a')
In [101]: list
Out[101]: ['b', 'c', 'd']
In [102]: list2
Out[102]: ['a', 'b', 'c', 'd']
In [103]: list2.remove('d')
In [104]: list2
Out[104]: ['a', 'b', 'c']
In [105]: list
Out[105]: ['b', 'c', 'd']
In [106]:
素数作业案例
import datetime
start = datetime.datetime.now()
In [160]: for n in range(2, 101):
.....: for x in list:
.....: if n % x == 0:
.....: break
.....: else:
.....: list.append(n)
.....: print (n)
.....:print(datetime.datetime.now()-start)