摘要:列表嵌套列表,相當于其他語言中的二維數組列表的基本操作宮保雞丁宮保雞丁糖醋里脊粉絲蒸蝦魚香肉絲魚香肉絲宮保雞丁糖醋里脊粉絲蒸蝦魚香肉絲宮保雞丁糖醋里脊宮保雞丁糖醋里脊粉絲蒸蝦魚香肉絲魚香肉絲宮保雞丁糖醋里脊粉絲蒸蝦魚香肉絲魚香肉絲宮保雞
列表(list)
>>> type([1,2,3,4,5,6])>>> type(["hello","worls",1,9]) >>> type(["hello","worls",1,9,True,False]) >>> type([[1,2],[3,4],[True,False]])//嵌套列表,相當于其他語言中的二維數組
"宮保雞丁" >>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"][3] "魚香肉絲" >>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"][0:2] ["宮保雞丁", "糖醋里脊"] >>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"][-1] "魚香肉絲" >>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"][-1:] ["魚香肉絲"] >>> ["宮保雞丁","糖醋里脊","粉絲蒸蝦","魚香肉絲"]+["拍黃瓜","炒豆芽"] ["宮保雞丁", "糖醋里脊", "粉絲蒸蝦", "魚香肉絲", "拍黃瓜", "炒豆芽"] >>> ["拍黃瓜","炒豆芽"]*["拍黃瓜","炒豆芽"] Traceback (most recent call last): File "元組(tuple)", line 1, in ["拍黃瓜","炒豆芽"]*["拍黃瓜","炒豆芽"] TypeError: can"t multiply sequence by non-int of type "list" >>> ["拍黃瓜","炒豆芽"]*3 ["拍黃瓜", "炒豆芽", "拍黃瓜", "炒豆芽", "拍黃瓜", "炒豆芽"] >>> ["拍黃瓜","炒豆芽"]-["拍黃瓜"] Traceback (most recent call last): File " ", line 1, in ["拍黃瓜","炒豆芽"]-["拍黃瓜"] TypeError: unsupported operand type(s) for -: "list" and "list"
>>> (1,2,3,4,5) (1, 2, 3, 4, 5) >>> (1,"-1",True) (1, "-1", True) >>> (1,2,3,4)[0] 1 >>> (1,2,3,4)[0:2] (1, 2) >>> (1,2,3)+(4,5,6) (1, 2, 3, 4, 5, 6) >>> (1,2,3)*3 (1, 2, 3, 1, 2, 3, 1, 2, 3) >>> type((1,2,3))str list tuple都是序列>>> type(1) >>> type([1,2,3]) >>> type("hello") >>> type(("hello")) >>> type((1))//如果括號里只有一個元素,python會默認為是要做數學運算,等同于type(1) >>> (1+1)*2 4 >>> (1,2,3) (1, 2, 3) >>> (1) 1 >>> (1,) (1,) >>> type((1,)) >>> type(()) >>> type([1])
>>> "hello world"[2] "l" >>> [1,2,3][2] 3 >>> (1,2,3)[2] 3切片
>>> [1,2,3,4,5][0:3] [1, 2, 3] >>> [1,2,3,4,5][-1:] [5] >>> >>> "hello world"[0:8:0] Traceback (most recent call last): File "判斷一個序列中是否包含某個元素", line 1, in "hello world"[0:8:0] ValueError: slice step cannot be zero >>> "hello world"[0:8:1] "hello wo" >>> "hello world"[0:8:2] "hlow" >>> "hello world"[0:8:3] "hlw" >>> "hello world"[0:8:4] "ho" >>> "hello world"[0:8:5] "h " >>> "hello world"[0:8:6] "hw" >>> "hello world"[0:8:7]
>>> 3 in [1,2,3,4,5,6] True >>> 7 in [1,2,3,4,5,6] False >>> 3 not in [1,2,3,4,5,6] False序列的其他操作
>>> len([1,2,3,4,5,6]) 6 >>> len("hello world") 11 >>> max([1,2,3,4,5,6]) 6 >>> min([1,2,3,4,5,6]) 1 >>> max("hello world") "w" >>> min("hello world") " " >>> min("helloworld") "d"ASCII碼
>>> ord("w") 119 >>> ord("d") 100 >>> ord(" ") 32set 集合 集合的特性
>>> type({1,2,3,4,5,6})集合的操作>>> {1,2,3,4,5,6}[0] //集合是無序的,沒有像str,list,tuple的架構索引,不能切片 Traceback (most recent call last): File " ", line 1, in {1,2,3,4,5,6}[0] TypeError: "set" object does not support indexing >>> {1,2,3,4,5,6}[0:2] Traceback (most recent call last): File " ", line 1, in {1,2,3,4,5,6}[0:2] TypeError: "set" object is not subscriptable >>> {1,1,2,2,3,3,4,4} //集合的第二個特性:不重復 {1, 2, 3, 4}
>>> len({1,2,3}) 3 >>> len({1,1,1}) 1 >>> len({1,2,2}) 2 >>> 1 in {1,2,3} True >>> 1 not in {1,2,3} False >>> {1,2,3,4,5,6} - {3,4} //求兩個集合的差集 {1, 2, 5, 6} >>> {1,2,3,4,5,6} & {3,4} //交集 {3, 4} >>> {1,2,3,4,5,6} | {3,4,7} //合集|并集 {1, 2, 3, 4, 5, 6, 7} //定義一個空的集合set() >>> type({})dict 字典>>> type(set()) >>> len(set()) 0
>>> {"menu1":"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}[0] Traceback (most recent call last): File "value的類型可以是str | int | float | list | set | dict", line 1, in {"menu1":"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}[0] KeyError: 0 >>> {"menu1":"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}["menu1"] "宮保雞丁" >>> {"menu1":"宮保雞丁","menu1":"白斬雞","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"}["menu1"] "白斬雞" >>> {"menu1":"宮保雞丁","menu1":"白斬雞","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"} {"menu1": "白斬雞", "menu2": "糖醋里脊", "menu3": "粉絲蒸蝦", "menu4": "魚香肉絲"} >>> {1:"宮保雞丁","1":"白斬雞","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"} //key也可以是數字 {1: "宮保雞丁", "1": "白斬雞", "menu2": "糖醋里脊", "menu3": "粉絲蒸蝦", "menu4": "魚香肉絲"}
>>> type({1:"宮保雞丁","1":"白斬雞","menu2":{1:1},"menu3":"粉絲蒸蝦",})key必須是不可變的類型(str、tuple)
>>> {[1,2]:"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"} Traceback (most recent call last): File "", line 1, in {[1,2]:"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"} TypeError: unhashable type: "list" >>> {(1,2):"宮保雞丁","menu2":"糖醋里脊","menu3":"粉絲蒸蝦","menu4":"魚香肉絲"} {(1, 2): "宮保雞丁", "menu2": "糖醋里脊", "menu3": "粉絲蒸蝦", "menu4": "魚香肉絲"}
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/44780.html
摘要:示例有理數的算術有理數可表示為整數的比值,并且它組成了實數的一個重要子類。有理數的值需要兩部分來描述。這里的重要概念是,通過將有理數表示為整數的比值,我們能夠完全避免近似問題。返回有理數的分子。 2.2 數據抽象 來源:2.2 Data Abstraction 譯者:飛龍 協議:CC BY-NC-SA 4.0 由于我們希望在程序中表達世界中的大量事物,我們發現它們的大多數都具有...
摘要:一的基本語法縮進統一個或者個空格。中的數據類型中有個標準類型數字字符串列表元組集合字典數字復數在中,只有一種整數類型,表示長整型。如則會顯示,并不是換行。空行與代碼縮進不同,空行并不是語法的一部分。我們將首行及后面的代碼組稱為一個子句。 一、python3的基本語法 1、縮進統一(1個tab或者4個空格)。 for i in range(10): print (i) ...
摘要:標題標簽會將其中的文本加粗加黑顯示,并從到依次減弱。換行標簽換行符。可在標簽內使用。規定表頭單元格可橫跨的行數。HTML的概念 概念: HTML 是用來描述網頁的一種語言。 HTML 指的是超文本標記語言 (Hyper Text Markup Language) HTML 不是一種編程語言,而是一種標記語言 (markup language) 標記語言是一套標記標簽 (markup t...
摘要:的鎖是非公平鎖,默認情況下也是非公平鎖,但可以通過帶布爾值的構造函數要求使用公平鎖。有序性,是保證線程內串行語義,避免指令重排等。公平性是減少線程饑餓個別線程長期等待鎖,但始終無法獲取情況發生的一個辦法。 目錄介紹 1.Synchronize和ReentrantLock區別 1.1 相似點 1.2 區別 1.3 什么是線程安全問題?如何理解 1.4 線程安全需要保證幾個基本特性 ...
閱讀 929·2021-11-22 12:09
閱讀 3704·2021-09-27 13:36
閱讀 1390·2021-08-20 09:37
閱讀 4007·2019-12-27 12:22
閱讀 2353·2019-08-30 15:55
閱讀 2358·2019-08-30 13:16
閱讀 2817·2019-08-26 17:06
閱讀 3433·2019-08-23 18:32