>>> l = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> type(l) <type 'list'> >>> el = [] # Create an empty list >>> len(el) 0 >>> sl = [1] # Create a single item list >>> len(sl) 1 >>> sl = [1,] # Create a single item list, as with a tuple >>> len(sl) 1
>>> help(list) Help on class list in module __builtin__: class list(object) | list() -> new list | list(sequence) -> new list initialized from sequence's items | | Methods defined here: | | __add__(...) | x.__add__(y) <==> x+y | | __contains__(...) | x.__contains__(y) <==> y in x | ...
>>> l = list()
>>> type(l)
<type 'list'>
>>> len(l)
0
>>> l
[]
>>> l = list((0, 1, 2, 3, 4, 5, 6, 7,
8, 9)) # Create a list from a tuple
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> len(l)
10
>>> l = list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # Create a list from a list
>>> l
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> len(l)
10
>>> l = list(0, 1, 2, 3, 4, 5, 6, 7,
8, 9) # Error: Must pass in a sequence
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: list() takes at most 1 argument (10 given)
>>> l = list("0123456789") # Create a list from a string
>>> l
['0', '1', '2', '3', '4', '5', '6', '7', '8', '9']
>>> type(l)
<type 'list'>
>>> len(l)
10
>>> l = list([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> l[0] # Get the first item in the list 0 >>> type(l[0]) <type 'int'> >>> l[5] # Get the sixth item in the list 5 >>> l[1:5] # Get the second through fifth items [1, 2, 3, 4] >>> type(l[1:5]) <type 'list'> >>> l[0::2] # Get every second item [0, 2, 4, 6, 8] >>> l[0], l[1], l[2] (0, 1, 2)
>>> l = [] >>> l[0] = 0 # The list is empty Traceback (most recent call last): File "<stdin>", line 1, in ? IndexError: list assignment index out of range >>> l.append(0) >>> l [0] >>> l[0] = 1 >>> l [1]
>>> l=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> l[2] = 2 >>> type(l[2]) <type 'int'> >>> l[2] = "two" # Change the type of an element >>> ,ype(l[2]) <type 'str'> >>> l [0, 1, 'two', 3, 4, 5, 6, 7, 8, 9] >>> l[2] = l[2:5] * 2 >>> l [0, 1, ['two', 3, 4, 'two', 3, 4], 3, 4, 5, 6, 7, 8, 9] >>> del(l[2]) # Remove single element >>> l [0, 1, 3, 4, 5, 6, 7, 8, 9] >>> l[1:3] = [] # Remove a slice >>> l [0, 4, 5, 6, 7, 8, 9]
>>> al = [[0, 1, 2], [3, 4, 5], [6, 7, 8]] >>> al [[0, 1, 2], [3, 4, 5], [6, 7, 8]] >>> al[0][0] # First element in 2D array 0 >>> al[2][2] # Last element in 2D array 8 >>> al[1][2] 5 >>> al = [[[0, 1], [2, 3]], [[4, 5], [6, 7]]] >>> al [[[0, 1], [2, 3]], [[4, 5], [6, 7]]] >>> al[0][0][1] 1 >>> len(al) # Length of outer dimension 2 >>> len(al[0]) # Length of middle dimension 2 >>> len(al[0][0]) # Length of inner dimension 2
>>> l=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> id(l) # This is the object id for our current list 4525432 >>> l.reverse() # Reverse the list >>> l [9, 8, 7, 6, 5, 4, 3, 2, 1, 0] >>> id(l) # The id is the same, modified list in place. 4525432 >>> l.sort() # Sort the list in numerical order >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] >>> id(l) # Modified the existing list 4525432 >>> l.index(5) # Same as l[5] 5 >>> l.count(0) # How many times does '0' occur in the list 1 >>> l.pop() # Take off the last item (Stack) 9 >>> l [0, 1, 2, 3, 4, 5, 6, 7, 8] >>> l.pop(5) # Take out the fifth element 5 >>> l [0, 1, 2, 3, 4, 6, 7, 8] >>> l.pop(0) # Take the first item off the list (Queue) 0 >>> l [1, 2, 3, 4, 6, 7, 8]
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有