python 《第二談》

環(huán)境:mac os x

1、創(chuàng)建簡單的python列表


先從簡單的顏色列表入手:
white
black
blue
red
yellow
上面列表,用python能理解的方式來寫:
colors = ["white",
          "black",
          "blue",
          "red",
          "yellow"]

為了把人可讀的列表轉(zhuǎn)換成python可讀的列表,需要遵循以下四個步驟:

1.在數(shù)據(jù)兩邊加引號,將各個顏色名稱轉(zhuǎn)換成字符串。

2.用逗號將列表項與下一項分隔開。

3.在列表的倆邊加上開始和結(jié)束中括號。

4.使用賦值操作符(=)將這個列表賦值一個標(biāo)識符(以上代碼中的colors)。

$ colors = ["white",
          "black",
          "blue",
          "red",
          "yellow"]
列表就像是數(shù)組:在colos中,white是第0項、black是第1項...

2、訪問列表數(shù)據(jù)


1) 打印列表中所有數(shù)據(jù)
$ print(colors)

$ print(colors)

['white', 'black', 'blue', 'red', 'yellow']

2) 打印列表中單個數(shù)據(jù),使用中括號偏移量記法訪問列表數(shù)據(jù)
$ print(colors[1])

$ print(colors[1])

black

3) 得到列表中含有多少個數(shù)據(jù)項
$ print(len(colors))

$ print(len(colors))

5

3、編輯列表


1) 在列表末尾增加一個數(shù)據(jù)項 append()
$ colors.append("green")

$ colors.append("green")

$ print(len(colors))

6

2) 從列表末尾刪除一個數(shù)據(jù)項 pop()
$ colors.pop()

$ colors.pop()

'green'

$ print(len(colors))

5

3) 在列表末尾增加一個數(shù)據(jù)項集合 extend()
$ colors.extend(["green","gray"])

$ colors.extend(["green","gray"])

$ print(colors)

['white', 'black', 'blue', 'red', 'yellow', 'green', 'gray']

4) 在列表某個特定的位置增加一個數(shù)據(jù)項 insert()
$ colors.insert(1,"orange")

$ colors.insert(1,"orange")

$ print(colors)

['white', 'orange', 'black', 'blue', 'red', 'yellow', 'green', 'gray']

5) 在列表刪除一個特定的數(shù)據(jù)項 remove()
$ colors.remove("orange")

$ colors.remove("orange")

$ print(colors)

['white', 'black', 'blue', 'red', 'yellow', 'green', 'gray']

3、處理列表數(shù)據(jù)


1) 迭代列表 for in
for 目標(biāo)標(biāo)識符 in 列表: //冒號放在列表名稱后:指示列表處理代碼開始
    列表處理代碼
    
$ for each_color in colors:
    print(each_color)

$ for each_color in colors:

print(each_color)

white

black

blue

red

yellow

green

gray

注:使用for循環(huán)是可伸縮的,適用于任意大小的列表。

2) 迭代列表 while
$  count = 0    
$  while count < len(colors):
    print(colors[count])
    count = count + 1
    

count = 0

$ while count < len(colors):

print(colors[count])

count = count + 1

white

black

blue

red

yellow

green

gray

4、列表嵌套列表


1) 定義一個列表,并嵌套列表
$ colors = ["white",
         "red",
         "black",
         ["#fff","#ff2600","#000"]]
$ print(colors)

$ colors = ["white","red","black",["#fff","#ff2600","#000"]]

$ print(colors)

['white', 'red', 'black', ['#fff', '#ff2600', '#fff']]

2) 往列表中,插入添加另一個列表
    
$ colors = ["white",
            "red",
            "black"]
$ colors1 = ["#fff",
             "#ff2600",
             "#000"]
$ colors.append(colors1)
$ print(colors)

$ colors = ["white","red","black"]

$ colors1 = ["#fff","#ff2600","#000"]

$ colors.append(colors1)

$ print(colors)

['white', 'red', 'black', ['#fff', '#ff2600', '#fff']]

5、復(fù)雜數(shù)據(jù)處理


1) 處理嵌套列表
$ for each_color in colors:
    if isinstance(each_color,list):
        for nest_color in each_color:
            print(nest_color)
    else:
        print(each_color)

white

red

black

#fff

#ff2600

#fff

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

友情鏈接更多精彩內(nèi)容