【〇】学习环境
操作系统: Mac OS X 10.10.5 python: Python 3.5.1 IDE:PyCharm4.5
【一】列表(LIST):
下面是从help中摘录的一部分常用的方法:
#创建列表 list() -> new empty list #追加列表 | append(...) | L.append(object) -> None -- append object to end #清除列表 | clear(...) | L.clear() -> None -- remove all items from L #复制... | copy(...) | L.copy() -> list -- a shallow copy of L #计数 | count(...) | L.count(value) -> integer -- return number of occurrences of value #增加 | extend(...) | L.extend(iterable) -> None -- extend list by appending elements from the iterable #索引 | index(...) | L.index(value, [start, [stop]]) -> integer -- return first index of value. | Raises ValueError if the value is not present. #插入 | insert(...) | L.insert(index, object) -- insert object before index #显示最后一个元素,并删除 | pop(...) | L.pop([index]) -> item -- remove and return item at index (default last). | Raises IndexError if list is empty or index is out of range. #删除 | remove(...) | L.remove(value) -> None -- remove first occurrence of value. | Raises ValueError if the value is not present. #反转 | reverse(...) | L.reverse() -- reverse *IN PLACE* #排序 | sort(...) | L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE*
方法示例如下:
#创建列表 >>> name_list = [‘user01‘, ‘user02‘, ‘user03‘] #追加列表 >>> name_list.append(‘user04‘) >>> name_list [‘user01‘, ‘user02‘, ‘user03‘, ‘user04‘] #按照索引显示内容,从0开始 >>> name_list[0] ‘user01‘ >>> name_list[1] ‘user02‘ #删除成员 >>> name_list.remove(‘user03‘) >>> name_list [‘user01‘, ‘user02‘, ‘user04‘] >>> name_list.append(‘user01‘) >>> name_list.append(‘user01‘) >>> name_list.append(‘user02‘) >>> name_list [‘user01‘, ‘user02‘, ‘user04‘, ‘user01‘, ‘user01‘, ‘user02‘] #成员排序 >>> name_list.sort() >>> name_list [‘user01‘, ‘user01‘, ‘user01‘, ‘user02‘, ‘user02‘, ‘user04‘] #反转列表 >>> name_list.reverse() >>> name_list [‘user04‘, ‘user02‘, ‘user02‘, ‘user01‘, ‘user01‘, ‘user01‘] #删除最后一个成员,并打印出来 >>> name_list.pop() ‘user01‘ >>> name_list [‘user04‘, ‘user02‘, ‘user02‘, ‘user01‘, ‘user01‘] #在索引值为5的位置插入成员user05 >>> name_list.insert(5,‘user05‘) >>> name_list [‘user04‘, ‘user02‘, ‘user02‘, ‘user01‘, ‘user01‘, ‘user05‘] #给成员user01计数 >>> name_list.count(‘user01‘) 2 #删除成员,索引值最小的那个 >>> name_list.remove(‘user05‘) >>> name_list [‘user04‘, ‘user02‘, ‘user02‘, ‘user01‘, ‘user01‘] #清空列表,但是列表并没有被删除 >>> name_list.clear() >>> name_list [] >>>
需要注意的地方:对于列表中的sort方法,python3.x与python2.x有些区别
Python 3.5.1 (v3.5.1:37a07cee5969, Dec 5 2015, 21:12:44) [GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> a = [‘user01‘,123,‘*‘,‘1uuuu‘] >>> a.sort() Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: unorderable types: int() < str() #只有相同的数据类型才能进行比较 >>> a [‘user01‘, 123, ‘*‘, ‘1uuuu‘]
对于python2.x却能够直接排序:
Python 2.7.10 (default, Jul 14 2015, 19:46:27) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.39)] on darwin Type "help", "copyright", "credits" or "license" for more information. >>> >>> a = [‘user01‘,123,‘*‘,‘1uuuu‘] >>> a.sort() >>> a [123, ‘*‘, ‘1uuuu‘, ‘user01‘]
【二】元组
元组使用(),元组的功能和列表相似,但是元组创建之后不能修改,因此就下面几个常用的方法:
#创建元组 | tuple() -> empty tuple#计数 | count(...) | T.count(value) -> integer -- return number of occurrences of value | #索引 | index(...) | T.index(value, [start, [stop]]) -> integer -- return first index of value.
方法示例如下:
>>> a = (100,‘us‘,‘china‘,‘*‘,100) >>> a[1] ‘us‘ >>> a.count(100) 2 >>>
元组和列表也能相互转化:
>>> a (100, ‘us‘, ‘china‘, ‘*‘, 100) >>> b = list(a) #将元组转换为列表 >>> b [100, ‘us‘, ‘china‘, ‘*‘, 100] >>> c = tuple(b) #将列表转换为元组 >>> c (100, ‘us‘, ‘china‘, ‘*‘, 100)
未完待续...
时间: 2024-10-12 12:22:17