python開發(fā)之list操作實(shí)例分析
本文實(shí)例分析了python開發(fā)之list操作。分享給大家供大家參考,具體如下:
對python中l(wèi)ist的操作,大家可以參考《Python list操作用法總結(jié)》
以下是我個(gè)人的筆記:
#python list
'''
創(chuàng)建list有很多方法:
1.使用一對方括號創(chuàng)建一個(gè)空的list:[]
2.使用一對方括號,用','隔開里面的元素:[a, b, c], [a]
3.Using a list comprehension:[x for x in iterable]
4.Using the type constructor:list() or list(iterable)
'''
def create_empty_list():
'''Using a pair of square brackets to denote the empty list: [].'''
return []
def create_common_list():
'''Using square brackets, separating items with commas: [a], [a, b, c].'''
return ['a', 'b', 'c', 1, 3, 5]
def create_common_list2():
'''Using a list comprehension: [x for x in iterable].'''
return [x for x in range(1, 10)]
def str_to_list(s):
'''Using a string to convert list'''
if s != None:
return list(s)
else:
return []
def main():
test_listA = create_empty_list()
print(test_listA)
print('#' * 50)
test_listB = create_common_list()
print(test_listB)
print('#' * 50)
test_listC = create_common_list2()
print(test_listC)
print('#' * 50)
test_str = 'i want to talk about this problem!'
test_listD = str_to_list(test_str)
print(test_listD)
if __name__ == '__main__':
main()
運(yùn)行效果:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. >>> ================================ RESTART ================================ >>> [] ################################################## ['a', 'b', 'c', 1, 3, 5] ################################################## [1, 2, 3, 4, 5, 6, 7, 8, 9] ################################################## ['i', ' ', 'w', 'a', 'n', 't', ' ', 't', 'o', ' ', 't', 'a', 'l', 'k', ' ', 'a', 'b', 'o', 'u', 't', ' ', 't', 'h', 'i', 's', ' ', 'p', 'r', 'o', 'b', 'l', 'e', 'm', '!'] >>>
下面有更多的demo:
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> counter = 100
>>> miles = 1000.0
>>> name = "hongten"
>>> numberA,numberB,nameC = 1,2,"Hongten"
>>> list = [counter,miles,name,numberA,numberB,nameC]
>>> print(list)
[100, 1000.0, 'hongten', 1, 2, 'Hongten']
>>> #這是注釋部分,注釋用"#"開始
>>> for element in list:
print(element)
100
1000.0
hongten
1
2
Hongten
>>> #上面是遍歷列表list
>>> print(list[0]) #獲取列表list里面的第一個(gè)元素值
100
>>> print(list[-1]) #獲取列表list里面的最后一個(gè)元素值
Hongten
>>> print(len(list)) #用len(list)獲取list列表的長度
6
>>> num_inc_list = range(10) #產(chǎn)生一個(gè)數(shù)值遞增的列表
>>> print(num_inc_list)
range(0, 10)
>>> for inc_list in num_inc_list:
print(inc_list)
0
1
2
3
4
5
6
7
8
9
>>> #從這里我們可以看到range(10)是產(chǎn)生了一個(gè)從0開始到9的一個(gè)數(shù)值遞增列表
>>> initial_value = 10
>>> list_length = 5
>>> myList = [initial_value for i in range(10)]
>>> print(myList)
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>>> list_length = 2
>>> myList = myList * list_length
>>> print(myList)
[10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
>>> print(len(myList))
20
>>> #上面是用一個(gè)固定值initial_value去初始化一個(gè)列表myList
>>> #同時(shí)用myList = myList * list_length去復(fù)制myList
>>> #下面再看看復(fù)制的效果
>>> copyList = [1,2,3,"hongten"]
>>> copyList = copyList * list_length
>>> print(len(copyList))
8
>>> for cl in copyList:
print(cl)
1
2
3
hongten
1
2
3
hongten
>>> #下面我們來仔細(xì)研究一下python里面的list
>>> #在一個(gè)list中可以包含不同類型的元素,這個(gè)和ActionScript 3.0(AS3.0)中的數(shù)組類似
>>> test_list = ["hello",1,2,"world",4,5,"hongten"]
>>> print(len(test_list))
7
>>> print(test_list[0]) # 打印test_list
hello
>>> #打印test_list中的第一元素
>>> print(test_list[-1]) #打印test_list中最后一個(gè)元素
hongten
>>> print(test_list[-len]) #打印第一個(gè)元素
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
print(test_list[-len]) #打印第一個(gè)元素
TypeError: bad operand type for unary -: 'builtin_function_or_method'
>>> print(test_list[-len(test_list)]) #打印第一個(gè)元素
hello
>>> print(test_list[len(test_list) - 1]) #打印最后一個(gè)元素
hongten
>>> test_list.append(6) #向列表中追加一個(gè)元素
>>> print(test_list[-1])
6
>>> test_list.insert(1,0)
>>> print(test_list)
['hello', 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> #上面的操作是向列表test_list的小標(biāo)為1的地方插入元素0
>>> test_list.insert(1,0)
>>> print(test_list)
['hello', 0, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.insert(2,1)
>>> print(test_list)
['hello', 0, 1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> print(test_list.pop(0)) #返回最后一個(gè)元素,并從test_list中刪除之
hello
>>> print(test_list)
[0, 1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> print(test_list.pop(2)) #上面的注釋有錯誤,pop(index)的操作是返回?cái)?shù)組下標(biāo)為index的元素,并從列表中刪除之
0
>>> print(test_list)
[0, 1, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.remove(1)
>>> print(test_list)
[0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> #remove(1)表示的是刪除第一次出現(xiàn)的元素1
>>> test_list.insert(0,1)
>>> print(test_list)
[1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.remove(1)
>>> print(test_list)
[0, 1, 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.insert(2,"hongten")
>>> print(test_list)
[0, 1, 'hongten', 2, 'world', 4, 5, 'hongten', 6]
>>> test_list.count("hongten")
2
>>> #count(var)是統(tǒng)計(jì)var元素在列表中出現(xiàn)的個(gè)數(shù)
>>> test_list.count("foo")
0
>>> test_list_extend = ["a","b","c"]
>>> test_list.extend(test_list_extend)
>>> print(test_list)
[0, 1, 'hongten', 2, 'world', 4, 5, 'hongten', 6, 'a', 'b', 'c']
>>> #使用extend(list)作用是追加一個(gè)list到源list上面
>>> print(test_list.sort())
Traceback (most recent call last):
File "<pyshell#76>", line 1, in <module>
print(test_list.sort())
TypeError: unorderable types: str() < int()
>>> test_list_extend.append("h")
>>> test_lsit_extend.append("e")
Traceback (most recent call last):
File "<pyshell#78>", line 1, in <module>
test_lsit_extend.append("e")
NameError: name 'test_lsit_extend' is not defined
>>> list_a = ["e","z","o","r"]
>>> list_a.extend(test_list_extend)
>>> print(list_a)
['e', 'z', 'o', 'r', 'a', 'b', 'c', 'h']
>>> print(list_a.sort()) #對list_a列表進(jìn)行排序
None
>>> #不知道為什么以上排序都有報(bào)錯......
>>> list_b = [1,3,5,2,6,4]
>>> print(list_b.sort())
None
>>> print(sort(list_b))
Traceback (most recent call last):
File "<pyshell#86>", line 1, in <module>
print(sort(list_b))
NameError: name 'sort' is not defined
>>> #不去管排序問題了,先看看刪除操作吧?。。。。?
>>> print(list_b)
[1, 2, 3, 4, 5, 6]
>>> print(del list_b[1])
SyntaxError: invalid syntax
>>> del list_b[1]
>>> print(list_b)
[1, 3, 4, 5, 6]
>>> del list_b[0,2]
Traceback (most recent call last):
File "<pyshell#92>", line 1, in <module>
del list_b[0,2]
TypeError: list indices must be integers, not tuple
>>> del list_b[0:2]
>>> print(list_b)
[4, 5, 6]
>>> #del list[index]刪除下標(biāo)為index的元素,del list[start:end]刪除從start下標(biāo)開始到end下標(biāo)結(jié)束的元素
>>> del list_b[10]
Traceback (most recent call last):
File "<pyshell#96>", line 1, in <module>
del list_b[10]
IndexError: list assignment index out of range
>>> #如果我們刪除的下標(biāo)超出了列表的長度范圍,就會報(bào)錯啦!!!!!
>>> ##########################################################################
>>> list_c = range(5);
>>> for c in list_c:
print(c)
0
1
2
3
4
>>> list_d = list_c
>>> for d in list_d:
print(d)
0
1
2
3
4
>>> #上面是列表的復(fù)制
>>> list_d[2] = 23
Traceback (most recent call last):
File "<pyshell#108>", line 1, in <module>
list_d[2] = 23
TypeError: 'range' object does not support item assignment
>>> list_e = [1,2,3,4,5]
>>> list_f = list_e
>>> list_f[2] = 234
>>> print(list_e)
[1, 2, 234, 4, 5]
>>> #從這里我們可以知道,list_f復(fù)制了list_e,list_f是對list_e的一個(gè)引用,
>>> #他們共同指向一個(gè)對象:[1,2,3,4,5],當(dāng)我們視圖修改list_f[2]的值的時(shí)候,
>>> #list_f所指向的對象的行為發(fā)生了變化,即元素值發(fā)生了變化,但是他們的引用是沒有
>>> #發(fā)生變化的。所以list_e[2] = 234也是在情理之中。
>>> #######################################################################
>>> list_i = list_e[:]
>>> print(list_i)
[1, 2, 234, 4, 5]
>>> print(list_e)
[1, 2, 234, 4, 5]
>>> list_i[2] = 3
>>> print(list_e)
[1, 2, 234, 4, 5]
>>> print(list_i)
[1, 2, 3, 4, 5]
>>> #上面是進(jìn)行了列表的克隆操作,即拷貝了另一個(gè)列表,這樣的操作,會創(chuàng)造出新的一個(gè)列表對象
>>> #使得list_i和list_e指向不同的對象,就有著不同的引用,所以當(dāng)list_i[2] = 3的時(shí)候,
>>> #list_e[2]還是等于234,即不變
>>>
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
- Python list操作用法總結(jié)
- Python實(shí)現(xiàn)比較兩個(gè)列表(list)范圍
- python中l(wèi)ist常用操作實(shí)例詳解
- 在Python中操作列表之list.extend()方法的使用
- Python列表(list)常用操作方法小結(jié)
- Python中內(nèi)置數(shù)據(jù)類型list,tuple,dict,set的區(qū)別和用法
- 詳解Python中dict與set的使用
- 在Python中使用dict和set方法的教程
- Python中集合類型(set)學(xué)習(xí)小結(jié)
- Python set集合類型操作總結(jié)
- 跟老齊學(xué)Python之集合(set)
- Python判斷值是否在list或set中的性能對比分析
相關(guān)文章
odoo?為可編輯列表視圖字段搜索添加查詢過濾條件的詳細(xì)過程
Odoo 是基于 Python 寫的一系列開源商業(yè)應(yīng)用程序套裝,前身是 OpenERP,這篇文章主要介紹了odoo?為可編輯列表視圖字段搜索添加查詢過濾條件,需要的朋友可以參考下2023-02-02
python?包實(shí)現(xiàn)JSON?輕量數(shù)據(jù)操作
這篇文章主要介紹了python?包實(shí)現(xiàn)JSON?輕量數(shù)據(jù)操作,文章介紹內(nèi)容首先將對象轉(zhuǎn)為json字符串展開主題詳細(xì)內(nèi)容需要的小伙伴可以參考一下2022-04-04
python3+opencv生成不規(guī)則黑白mask實(shí)例
今天小編就為大家分享一篇python3+opencv生成不規(guī)則黑白mask實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-02-02
Python中出現(xiàn)IndentationError:unindent does not match any outer
今天在網(wǎng)上copy的一段代碼,代碼很簡單,每行看起來該縮進(jìn)的都縮進(jìn)了,運(yùn)行的時(shí)候出現(xiàn)了如下錯誤,IndentationError: unindent does not match any outer indentation level,如果看起來縮進(jìn)正常所有tab與空格混用就會出現(xiàn)這個(gè)問題2019-01-01
基于python實(shí)現(xiàn)一個(gè)簡單的瀏覽器引擎
瀏覽器引擎是用來處理、渲染和顯示網(wǎng)頁內(nèi)容的核心組件,其主要任務(wù)是將用戶輸入的URL所代表的網(wǎng)頁資源加載并呈現(xiàn)出來,通常包括HTML、CSS、JavaScript以及各種多媒體內(nèi)容,本文給大家介紹了如何基于python實(shí)現(xiàn)一個(gè)簡單的瀏覽器引擎,需要的朋友可以參考下2024-10-10
Python-numpy實(shí)現(xiàn)灰度圖像的分塊和合并方式
今天小編就為大家分享一篇Python-numpy實(shí)現(xiàn)灰度圖像的分塊和合并方式,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-01-01
基于Python實(shí)現(xiàn)簡易的植物識別小系統(tǒng)
這篇文章主要介紹了利用Python實(shí)現(xiàn)一個(gè)簡易的植物識別系統(tǒng),文中的示例代碼簡潔易懂,對我們學(xué)習(xí)Python有一定的幫助,需要的小伙伴可以參考一下2021-12-12
python代碼如何實(shí)現(xiàn)切換中英文輸入法
這篇文章主要介紹了python代碼如何實(shí)現(xiàn)切換中英文輸入法,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11

