>>> information= ["student0", "student1", "student2"]
>>> PRint(information)
['student0', 'student1', 'student2']
>>> print(information[0])
student0
>>> print(information[1])
student1
>>> print(information[2])
student2注意:在python中雙引號和單引號沒有區別列表操作使用append()添加或pop()刪除列表末尾數據選項>>> information.append("student3")
>>> print(information)
['student0', 'student1', 'student2', 'student3']
>>> information.pop()
'student3'
>>> print(information)
['student0', 'student1', 'student2']使用extend()在列表末尾增加一個數據集合>>> information.extend(["student3","student4"])
>>> print(information)
['student0', 'student1', 'student2', 'student3', 'student4']使用remove()刪除或insert()增加列表一個特定位置的數據選項>>> information.remove("student2")
>>> print(information)
['student0', 'student1', 'student3', 'student4']
>>> information.insert(2,2222)
>>> print(information)
['student0', 'student1', 2222, 'student3', 'student4']注意:py列表可以包含混合數據dir(list)可查看到列表的更多用法(此處暫不詳述)>>> dir(list)
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__', '__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']列表迭代操作for循環處理任意大小列表格式:for 目標標識符 in 列表 列表處理代碼>>> number=[0,1,2,3]
>>> for i in number:
print(i)
0
1
2
3while循環處理任意大小列表>>> number=[0,1,2]
>>> count = 0
>>> while count < len(number):
print(number[count])
count = count+1
0
1
2注意:相比與C原因用{}界定代碼段,python用縮進符界定。注意:迭代處理時,能用for就不要用while,避免出現"大小差1"錯誤在列表中存儲列表(列表嵌套)>>> information=['a',['b','c']]
>>> for i in information:
print(i)
a
['b', 'c']從列表中查找列表 先之前先介紹BIF中的函數isintance(),檢測某個特定標識符是否包含某個特定類型數據。(即檢測列表本身識是不是列表)>>> name=['sudent']
>>> isinstance(name,list)
True
>>> num_name=len(name)
>>> isinstance(num_name,list)
False 通過下面程序實現把列表中所有內容顯示>>> information=['a',['b','c']]
>>> for i in information:
if isinstance(i,list):
for j in i:
print(j)
else:
print(i)
a
b
cdir(__builtins__)查看內建函數BIF有哪些多重嵌套函數處理(使用遞歸函數)>>> information=['a',['b',['c']]]
>>> def print_lol(the_list):
for i in the_list:
if isinstance(i,list):
print_lol(i)
else:
print(i)
>>> print_lol(information)
abc定義函數標準格式def 函數名(參數): 函數組代碼新聞熱點
疑難解答