python開發_python中的list操作


對python中list的操作,大家可以參考:

Python list 操作

以下是我個人的筆記:

============================================

Add by Hongten 2013-08-14

============================================

 1 #python list
 2 '''
 3     創建list有很多方法:
 4 
 5     1.使用一對方括號創建一個空的list:[]
 6     2.使用一對方括號,用','隔開里面的元素:[a, b, c], [a]
 7     3.Using a list comprehension:[x for x in iterable]
 8     4.Using the type constructor:list() or list(iterable)
 9     
10 '''
11 
12 def create_empty_list():
13     '''Using a pair of square brackets to denote the empty list: [].'''
14     return []
15 
16 def create_common_list():
17     '''Using square brackets, separating items with commas: [a], [a, b, c].'''
18     return ['a', 'b', 'c', 1, 3, 5]
19 
20 
21 def create_common_list2():
22     '''Using a list comprehension: [x for x in iterable].'''
23     return [x for x in range(1, 10)]
24 
25 def str_to_list(s):
26     '''Using a string to convert list'''
27     if s != None:
28         return list(s)
29     else:
30         return []
31 
32 def main():
33     test_listA = create_empty_list()
34     print(test_listA)
35     print('#' * 50)
36     test_listB = create_common_list()
37     print(test_listB)
38     print('#' * 50)
39     test_listC = create_common_list2()
40     print(test_listC)
41     print('#' * 50)
42     test_str = 'i want to talk about this problem!'
43     test_listD = str_to_list(test_str)
44     print(test_listD)
45 
46 
47 if __name__ == '__main__':
48     main()

 

運行效果:

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:

 

  1 Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
  2 Type "copyright", "credits" or "license()" for more information.
  3 >>> counter = 100
  4 >>> miles = 1000.0
  5 >>> name = "hongten"
  6 >>> numberA,numberB,nameC = 1,2,"Hongten"
  7 >>> list = [counter,miles,name,numberA,numberB,nameC]
  8 >>> print(list)
  9 [100, 1000.0, 'hongten', 1, 2, 'Hongten']
 10 >>> #這是注釋部分,注釋用"#"開始
 11 >>> for element in list:
 12     print(element)
 13 
 14 100
 15 1000.0
 16 hongten
 17 1
 18 2
 19 Hongten
 20 >>> #上面是遍歷列表list
 21 >>> print(list[0]) #獲取列表list里面的第一個元素值
 22 100
 23 >>> print(list[-1]) #獲取列表list里面的最后一個元素值
 24 Hongten
 25 >>> print(len(list)) #用len(list)獲取list列表的長度
 26 6
 27 >>> num_inc_list = range(10) #產生一個數值遞增的列表
 28 >>> print(num_inc_list)
 29 range(0, 10)
 30 >>> for inc_list in num_inc_list:
 31     print(inc_list)
 32 
 33 0
 34 1
 35 2
 36 3
 37 4
 38 5
 39 6
 40 7
 41 8
 42 9
 43 >>> #從這里我們可以看到range(10)是產生了一個從0開始到9的一個數值遞增列表
 44 >>> initial_value = 10
 45 >>> list_length = 5
 46 >>> myList = [initial_value for i in range(10)]
 47 >>> print(myList)
 48 [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
 49 >>> list_length = 2
 50 >>> myList = myList * list_length
 51 >>> print(myList)
 52 [10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
 53 >>> print(len(myList))
 54 20
 55 >>> #上面是用一個固定值initial_value去初始化一個列表myList
 56 >>> #同時用myList = myList * list_length去復制myList
 57 >>> #下面再看看復制的效果
 58 >>> copyList = [1,2,3,"hongten"]
 59 >>> copyList = copyList * list_length
 60 >>> print(len(copyList))
 61 8
 62 >>> for cl in copyList:
 63     print(cl)
 64 
 65     
 66 1
 67 2
 68 3
 69 hongten
 70 1
 71 2
 72 3
 73 hongten
 74 >>> #下面我們來仔細研究一下python里面的list
 75 >>> #在一個list中可以包含不同類型的元素,這個和ActionScript 3.0(AS3.0)中的數組類似
 76 >>> test_list = ["hello",1,2,"world",4,5,"hongten"]
 77 >>> print(len(test_list))
 78 7
 79 >>> print(test_list[0]) # 打印test_list
 80 hello
 81 >>> #打印test_list中的第一元素
 82 >>> print(test_list[-1]) #打印test_list中最后一個元素
 83 hongten
 84 >>> print(test_list[-len]) #打印第一個元素
 85 Traceback (most recent call last):
 86   File "<pyshell#44>", line 1, in <module>
 87     print(test_list[-len]) #打印第一個元素
 88 TypeError: bad operand type for unary -: 'builtin_function_or_method'
 89 >>> print(test_list[-len(test_list)]) #打印第一個元素
 90 hello
 91 >>> print(test_list[len(test_list) - 1]) #打印最后一個元素
 92 hongten
 93 >>> test_list.append(6) #向列表中追加一個元素
 94 >>> print(test_list[-1])
 95 6
 96 >>> test_list.insert(1,0)
 97 >>> print(test_list)
 98 ['hello', 0, 1, 2, 'world', 4, 5, 'hongten', 6]
 99 >>> #上面的操作是向列表test_list的小標為1的地方插入元素0
100 >>> test_list.insert(1,0)
101 >>> print(test_list)
102 ['hello', 0, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
103 >>> test_list.insert(2,1)
104 >>> print(test_list)
105 ['hello', 0, 1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
106 >>> print(test_list.pop(0)) #返回最后一個元素,並從test_list中刪除之
107 hello
108 >>> print(test_list)
109 [0, 1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
110 >>> print(test_list.pop(2)) #上面的注釋有錯誤,pop(index)的操作是返回數組下標為index的元素,並從列表中刪除之
111 0
112 >>> print(test_list)
113 [0, 1, 1, 2, 'world', 4, 5, 'hongten', 6]
114 >>> test_list.remove(1)
115 >>> print(test_list)
116 [0, 1, 2, 'world', 4, 5, 'hongten', 6]
117 >>> #remove(1)表示的是刪除第一次出現的元素1
118 >>> test_list.insert(0,1)
119 >>> print(test_list)
120 [1, 0, 1, 2, 'world', 4, 5, 'hongten', 6]
121 >>> test_list.remove(1)
122 >>> print(test_list)
123 [0, 1, 2, 'world', 4, 5, 'hongten', 6]
124 >>> test_list.insert(2,"hongten")
125 >>> print(test_list)
126 [0, 1, 'hongten', 2, 'world', 4, 5, 'hongten', 6]
127 >>> test_list.count("hongten")
128 2
129 >>> #count(var)是統計var元素在列表中出現的個數
130 >>> test_list.count("foo")
131 0
132 >>> test_list_extend = ["a","b","c"]
133 >>> test_list.extend(test_list_extend)
134 >>> print(test_list)
135 [0, 1, 'hongten', 2, 'world', 4, 5, 'hongten', 6, 'a', 'b', 'c']
136 >>> #使用extend(list)作用是追加一個list到源list上面
137 >>> print(test_list.sort())
138 Traceback (most recent call last):
139   File "<pyshell#76>", line 1, in <module>
140     print(test_list.sort())
141 TypeError: unorderable types: str() < int()
142 >>> test_list_extend.append("h")
143 >>> test_lsit_extend.append("e")
144 Traceback (most recent call last):
145   File "<pyshell#78>", line 1, in <module>
146     test_lsit_extend.append("e")
147 NameError: name 'test_lsit_extend' is not defined
148 >>> list_a = ["e","z","o","r"]
149 >>> list_a.extend(test_list_extend)
150 >>> print(list_a)
151 ['e', 'z', 'o', 'r', 'a', 'b', 'c', 'h']
152 >>> print(list_a.sort()) #對list_a列表進行排序
153 None
154 >>> #不知道為什么以上排序都有報錯......
155 >>> list_b = [1,3,5,2,6,4]
156 >>> print(list_b.sort())
157 None
158 >>> print(sort(list_b))
159 Traceback (most recent call last):
160   File "<pyshell#86>", line 1, in <module>
161     print(sort(list_b))
162 NameError: name 'sort' is not defined
163 >>> #不去管排序問題了,先看看刪除操作吧!!!!!
164 >>> print(list_b)
165 [1, 2, 3, 4, 5, 6]
166 >>> print(del list_b[1])
167 SyntaxError: invalid syntax
168 >>> del list_b[1]
169 >>> print(list_b)
170 [1, 3, 4, 5, 6]
171 >>> del list_b[0,2]
172 Traceback (most recent call last):
173   File "<pyshell#92>", line 1, in <module>
174     del list_b[0,2]
175 TypeError: list indices must be integers, not tuple
176 >>> del list_b[0:2]
177 >>> print(list_b)
178 [4, 5, 6]
179 
180 >>> #del list[index]刪除下標為index的元素,del list[start:end]刪除從start下標開始到end下標結束的元素
181 >>> del list_b[10]
182 Traceback (most recent call last):
183   File "<pyshell#96>", line 1, in <module>
184     del list_b[10]
185 IndexError: list assignment index out of range
186 >>> #如果我們刪除的下標超出了列表的長度范圍,就會報錯啦!!!!!
187 >>> ##########################################################################
188 >>> list_c = range(5);
189 >>> for c in list_c:
190     print(c)
191 
192     
193 0
194 1
195 2
196 3
197 4
198 >>> list_d = list_c
199 >>> for d in list_d:
200     print(d)
201 
202     
203 0
204 1
205 2
206 3
207 4
208 >>> #上面是列表的復制
209 >>> list_d[2] = 23
210 Traceback (most recent call last):
211   File "<pyshell#108>", line 1, in <module>
212     list_d[2] = 23
213 TypeError: 'range' object does not support item assignment
214 >>> list_e = [1,2,3,4,5]
215 >>> list_f = list_e
216 >>> list_f[2] = 234
217 >>> print(list_e)
218 [1, 2, 234, 4, 5]
219 >>> #從這里我們可以知道,list_f復制了list_e,list_f是對list_e的一個引用,
220 >>> #他們共同指向一個對象:[1,2,3,4,5],當我們視圖修改list_f[2]的值的時候,
221 >>> #list_f所指向的對象的行為發生了變化,即元素值發生了變化,但是他們的引用是沒有
222 >>> #發生變化的。所以list_e[2] = 234也是在情理之中。
223 >>> #######################################################################
224 >>> list_i = list_e[:]
225 >>> print(list_i)
226 [1, 2, 234, 4, 5]
227 >>> print(list_e)
228 [1, 2, 234, 4, 5]
229 >>> list_i[2] = 3
230 >>> print(list_e)
231 [1, 2, 234, 4, 5]
232 >>> print(list_i)
233 [1, 2, 3, 4, 5]
234 >>> #上面是進行了列表的克隆操作,即拷貝了另一個列表,這樣的操作,會創造出新的一個列表對象
235 >>> #使得list_i和list_e指向不同的對象,就有着不同的引用,所以當list_i[2] = 3的時候,
236 >>> #list_e[2]還是等於234,即不變
237 >>> 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM