国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python的列表

2019-11-08 01:33:56
字體:
來源:轉載
供稿:網友
python中的列表類似于C語言的數組(列表就是打了激素的數組)>>> #創建列表,不需要聲明類型(Python變量標識符沒有類型)>>> 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) 0123while循環處理任意大小列表>>> number=[0,1,2]>>> count = 0>>> while count < len(number): print(number[count]) count = count+1 012注意:相比與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) abcdir(__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 函數名(參數):    函數組代碼
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 涿鹿县| 梨树县| 大名县| 建水县| 晋宁县| 汉沽区| 夏河县| 子洲县| 湛江市| 永安市| 遵义市| 图们市| 夏河县| 剑川县| 双江| 翁牛特旗| 阆中市| 邻水| 福建省| 正蓝旗| 灵台县| 怀集县| 福安市| 天津市| 海南省| 太谷县| 德格县| 定结县| 舟山市| 全州县| 福海县| 普兰店市| 栾川县| 江安县| 尚志市| 开江县| 泾源县| 社旗县| 镇巴县| 法库县| 古田县|