python day2 列表 元组 字典 字符串
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python day2 列表 元组 字典 字符串相关的知识,希望对你有一定的参考价值。
列表
#列表事例
>>> li = list((1,2,3,4,5))
>>> print li
[1, 2, 3, 4, 5]
>>> li2 = [‘a‘,‘b‘,‘c‘,‘d‘]
>>> print li2
[‘a‘, ‘b‘, ‘c‘, ‘d‘]
>>>
#列表尾部增加元素
>>> li.append(6)
>>> li
[1, 2, 3, 4, 5, 6]
#清空列表内的元素,适用于py3
li.clear()
print(li)
[]
#列表的拷贝(深浅拷贝)(忽略)
li = [1,2,3,4,]
li3 = li.copy()
li3.append(678,)
print(li)
print(li3)
liid = id(li)
liid3 = id(li3)
print(liid,liid3)
[1, 2, 3, 4]
[1, 2, 3, 4, 678]
35707080 35737992
#extend合并两个列表或者把列表和元组合并
>>> li.extend([1,2])
>>> li
[1, 2, 3, 4, 5, 6, 1, 2]
#count判断某个元素出现的次数
>>> li.count(1)
2
#index获取列表内元素的索引下表
>>> li.index(6)
5
#insert指定下标,插入元素
>>> li.insert(2,‘abc‘)
>>> li
[1, 2, ‘abc‘, 3, 4, 5, 6, 1, 2]
#pop移除某一项,加下标
>>> li
[1, 2, ‘abc‘, 3, 4, 5, 6, 1, 2]
>>> res = li.pop(3)
>>> print res
3
>>> print li
[1, 2, ‘abc‘, 4, 5, 6, 1, 2]
#remove删除--pop 加值,pop是加下标
>>> li.remove(‘abc‘)
>>> li
[1, 2, 4, 5, 6, 1, 2]
#reverse反向排序
>>> li
[1, 2, 4, 5, 6, 1, 2]
>>> li.reverse()
>>> li
[2, 1, 6, 5, 4, 2, 1]
#sort排序
>>> li
[2, 1, 6, 5, 4, 2, 1]
>>> li.sort()
>>> li
[1, 1, 2, 2, 4, 5, 6]
>>>
英文介绍
class list(object): """ list() -> new empty list list(iterable) -> new list initialized from iterable‘s items """ def append(self, p_object): # real signature unknown; restored from __doc__ """ L.append(object) -> None -- append object to end """ pass def clear(self): # real signature unknown; restored from __doc__ """ L.clear() -> None -- remove all items from L """ pass def copy(self): # real signature unknown; restored from __doc__ """ L.copy() -> list -- a shallow copy of L """ return [] def count(self, value): # real signature unknown; restored from __doc__ """ L.count(value) -> integer -- return number of occurrences of value """ return 0 def extend(self, iterable): # real signature unknown; restored from __doc__ """ L.extend(iterable) -> None -- extend list by appending elements from the iterable """ pass def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__ """ L.index(value, [start, [stop]]) -> integer -- return first index of value. Raises ValueError if the value is not present. """ return 0 def insert(self, index, p_object): # real signature unknown; restored from __doc__ """ L.insert(index, object) -- insert object before index """ pass def pop(self, index=None): # real signature unknown; restored from __doc__ """ L.pop([index]) -> item -- remove and return item at index (default last). Raises IndexError if list is empty or index is out of range. """ pass def remove(self, value): # real signature unknown; restored from __doc__ """ L.remove(value) -> None -- remove first occurrence of value. Raises ValueError if the value is not present. """ pass def reverse(self): # real signature unknown; restored from __doc__ """ L.reverse() -- reverse *IN PLACE* """ pass def sort(self, key=None, reverse=False): # real signature unknown; restored from __doc__ """ L.sort(key=None, reverse=False) -> None -- stable sort *IN PLACE* """ pass
字典
明天续~~
以上是关于python day2 列表 元组 字典 字符串的主要内容,如果未能解决你的问题,请参考以下文章