python入门(3)
列表方法
def append(self, p_object): # real signature unknown; restored from __doc__
""" L.append(object) -> None -- append object to end """
#添加对象到列表后面
pass
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
index,insert,pop
list2 = [1,2,3,4,5,6,7,7]
print(list2)
# 1 2 3 4 5 6 7 7
ret1=list2.index(4)
list2.insert(ret1,999)
print(list2)
# 1 2 3 999 4 5 6 7 7
ret1=list2.pop(3)
print(ret1)
# 3
print(list2)
# 1 2 999 4 5 6 7 7
删除列表
list2 = [1,2,3,4,5,6,7,7]
print(list2)
list2.remove()
print(list2)
list2.clear()
print(list2)
del list2
循环遍历完列表
list3 = [1,2,3,[1,2,3],4,5,7,[6,7,8]]
for i in list3:
if type(i)!=int:
for ii in i:
print(ii,end=" ")
else:
print(i,end=" ")
print()
for i in list3:
if isinstance(i,list):
for ii in i:
print(ii,end=" ")
else:
print(i,end=" ")
# 1 2 3 1 2 3 4 5 7 6 7 8
zip()
list1 = [1,2,3,4,5]
list2 = [1,2,3,4,5]
ret1=zip(list1,list2)
for i in ret1:
print(i)
# (1, 1)
# (2, 2)
# (3, 3)
# (4, 4)
# (5, 5)
enumerate()
ret2=enumerate(list1)
for i in ret2:
print(i,end=" ")
# (10, 1) (11, 2) (12, 3) (13, 4) (14, 5)