Python_9_Codecademy_9_Lists and Functions

<a href="http://www.itdecent.cn/p/54870e9541fc">總目錄</a>


課程頁(yè)面:https://www.codecademy.com/
內(nèi)容包含課程筆記和自己的擴(kuò)展折騰


Lists

  • 復(fù)制lists: list * n
n = ["ZHANG.Y"]
n = n * 5
print n
Output:
['ZHANG.Y', 'ZHANG.Y', 'ZHANG.Y', 'ZHANG.Y', 'ZHANG.Y']
  • List elements modification
n = [1, 6, 9]
n[1] = n[1] * 3
print n

Output:
[1, 18, 9]

  • Appending a new element
n = [1, 6, 9]
n.append(12)
print n

Output:
[1, 6, 9, 12]

  • list.pop(index)
n = [1, 6, 9]
x = n.pop(2)
print x
print n

Output:
9
[1, 6]

  • list.remove(item)
n = [1, 6, 9]
n.remove(9)
print n

Output:
[1, 6]

  • del(list[index])
n = [1, 6, 9]
del(n[2])
print n

Output:
[1, 6]

  • range(start, stop, step)
print range(8)
print range(1, 8)
print range(1, 8, 2)

Output:
[0, 1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7]
[1, 3, 5, 7]

  • 合并lists:list1 + list2
list1 = [1, 2, 3]
list2 = [4, 5, 6]
list3 = list1 + list2
print list3

Outpt:
[1, 2, 3, 4, 5, 6]

  • list里面嵌套lists
list_A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for i in list_A:
    print i

Output:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]

  • 練習(xí):設(shè)計(jì)一個(gè)function,把類(lèi)似[[1, 2, 3], [1, 3, 5, 7, 9]]這樣的list變成[1, 2, 3, 1, 3, 5, 7, 9]
    【方法一】
def flatten(my_list):
    results = []
    for i in my_list:
        for item in i:
            results.append(item)
    return results 
print flatten([[1, 2, 3], [1, 3, 5, 7, 9]])

【方法二】

def flatten(my_list):
    results = []
    for i in my_list:
        for number in range(len(i)):
            results.append(i[number])
    return results
print flatten([[1, 2, 3], [1, 3, 5, 7, 9]])

Output:
[1, 2, 3, 1, 3, 5, 7, 9]

  • .join(list)
    把list中的items連起來(lái)
my_list = ['ZHANG.Y', 'ZHANG.Y', 'ZHANG.Y', 'ZHANG.Y', 'ZHANG.Y']
print " <3 ".join(my_list)
Output:
ZHANG.Y <3 ZHANG.Y <3 ZHANG.Y <3 ZHANG.Y <3 ZHANG.Y

Functions

  • More than one variables:
def addition(a, b):
    return a + b
print addition(1, 2)

Output:
3

  • A list as an argument
def print_first_item(items):
    print items[0]
n = [1, 6, 9]
print_first_item(n)

Output:
1

  • Print items one by one
# 簡(jiǎn)單方法:
def print_items(items):
    for x in items:
        print x
n = [1, 6, 9] 
print_items(n)
def print_items(items):
    for x in range(len(items)):
        print items[x]  
n = [1, 6, 9] 
print_items(n)

Output:
1
6
9

  • Modifying each element
def triple_list(my_list):
    for x in range(len(my_list)):
        my_list[x] = my_list[x] * 3
    return my_list
n = [1, 6, 9] 
# 比較
triple_list(n)
print n
print triple_list(n)

Output:
[3, 18, 27]
[9, 54, 81]

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

相關(guān)閱讀更多精彩內(nèi)容

  • 一、實(shí)驗(yàn)?zāi)康?學(xué)習(xí)使用 weka 中的常用分類(lèi)器,完成數(shù)據(jù)分類(lèi)任務(wù)。 二、實(shí)驗(yàn)內(nèi)容 了解 weka 中 explo...
    yigoh閱讀 8,858評(píng)論 5 4
  • Python 是一種相當(dāng)高級(jí)的語(yǔ)言,通過(guò) Python 解釋器把符合語(yǔ)法的程序代碼轉(zhuǎn)換成 CPU 能夠執(zhí)行的機(jī)器碼...
    Python程序媛閱讀 2,039評(píng)論 0 3
  • //Clojure入門(mén)教程: Clojure – Functional Programming for the J...
    葡萄喃喃囈語(yǔ)閱讀 4,050評(píng)論 0 7
  • 我是從西安到上海去的,但卻不是西安人,而是四川人。2000年3月,開(kāi)始了滬漂。 去上海的原因是帶我的導(dǎo)師落葉歸根,...
    拓拔濯閱讀 383評(píng)論 0 0
  • 今天學(xué)習(xí)第四單元課文,我先讓孩子們學(xué)習(xí)了單元導(dǎo)讀!明白本單元學(xué)習(xí)的重點(diǎn)和任務(wù)!畫(huà)出關(guān)鍵句子!開(kāi)始學(xué)習(xí)第13課釣魚(yú)的...
    櫻桃心晴閱讀 111評(píng)論 0 0

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