## （Python基础教程之八）Python中的list操作

Python中，列表为：

• 有序
• 索引（索引从0开始）
• 易变的
• 异构的（列表中的项目不必是同一类型）
• 写为方括号之间的逗号分隔值列表
listOfSubjects = ['physics', 'chemistry', "mathematics"]
listOfIds = [0, 1, 2, 3, 4]
miscList = [0, 'one', 2, 'three']


## 1. Access list items

list [m : n]表示子列表从索引m（包括）开始，到索引n（不包括）结束。

• 如果m未提供，则假定其值为零。
• 如果n未提供，则选择范围直到列表的最后。
ids = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
print( ids[0] )			# 0
print( ids[1:5] )		# [1, 2, 3, 4]
print( ids[ : 3] )		# [0, 1, 2]
print( ids[7 : ] )		# [7, 8, 9]\
print( ids[-8:-5] )		# [2, 3, 4]


## 2. Modily list

charList =  ["a", "b", "c"]
charList [2] = "d"
print (charList)	# ['a', 'b', 'd']


## 3. Iterate a list

charList =  ["a", "b", "c"]

for x in charList:
print(x)

# a
# b
# c


## 4. Check if a item exists in the list

charList =  ["a", "b", "c"]
if "a" in charList:
print("a is present")	# a is present

if "d" in charList:
print("d is present")
else:
print("d is NOT present")	# d is NOT present


## 5. Finding length of the list

charList =  ["a", "b", "c"]
x = len (charList)
print (x)	# 3


• 要将项目添加到列表的末尾，请使用append(item)方法。
• 要在特定索引位置添加项目，请使用insert(index, item)方法。如果index大于索引长度，则将项目添加到列表的末尾。
charList =  ["a", "b", "c"]
charList.append("d")
charList.append("e")
print (charList)		# ['a', 'b', 'c', 'd', 'e']
charList.insert(5, "f")

print (charList)		# ['a', 'b', 'c', 'd', 'e', 'f']

charList.insert(10, "h")	# No error

print (charList)	# ['a', 'b', 'c', 'd', 'e', 'f', 'h']


## 7. Removing items

#### 7.1. remove()

charList =  ["a", "b", "c"]
charList.remove("c")
print (charList)		# ['a', 'b']


#### 7.2. pop()

charList =  ["a", "b", "c", "d"]
charList.pop()			# removes 'd' - last item
print (charList)		# ['a', 'b', 'c']
charList.pop(1)			# removes 'b'
print (charList)		# ['a', 'c']


#### 7.3. clear()

charList =  ["a", "b", "c", "d"]
charList.clear()
print (charList)		# []


#### 7.4. del keyword

charList =  ["a", "b", "c", "d"]
del charList[0]

print (charList)		# ['b', 'c', 'd']

del charList

print (charList)		# NameError: name 'charList' is not defined


## 8. Join two lists

charList = ["a", "b", "c"]
numList	= [1, 2, 3]

list1 = charList + numList

print (list1)	# ['a', 'b', 'c', 1, 2, 3]

charList.extend(numList)

print (charList)	# ['a', 'b', 'c', 1, 2, 3]


## 9. Python list methods

#### 9.1. append()

charList =  ["a", "b", "c"]

charList.append("d")

print (charList)	# ["a", "b", "c", "d"]


#### 9.2. clear()

charList =  ["a", "b", "c"]

charList.clear()

print (charList)	# []


#### 9.3. copy()

charList =  ["a", "b", "c"]
newList = charList.copy()
print (newList)	# ["a", "b", "c"]


#### 9.4. count()

charList =  ["a", "b", "c"]
x = charList.count('a')
print (x)	# 1


#### 9.5. extend()

charList = ["a", "b", "c"]
numList	= [1, 2, 3]
charList.extend(numList)

print (charList)	# ['a', 'b', 'c', 1, 2, 3]


#### 9.6. index()

charList =  ["a", "b", "c"]
x = charList.index('a')
print (x)	# 0


#### 9.7. insert()

charList =  ["a", "b", "c"]
charList.insert(3, 'd')
print (charList)	# ['a', 'b', 'c', 'd']


#### 9.8. pop()

charList =  ["a", "b", "c", "d"]
charList.pop()			# removes 'd' - last item
print (charList)		# ['a', 'b', 'c']
charList.pop(1)			# removes 'b'
print (charList)		# ['a', 'c']


#### 9.9. remove()

charList =  ["a", "b", "c", "d"]
charList.remove('d')
print (charList)		# ['a', 'b', 'c']


#### 9.10. reverse()

charList =  ["a", "b", "c", "d"]
charList.reverse()
print (charList)		# ['d', 'c', 'b', 'a']


#### 9.11. sort()

charList =  ["a", "c", "b", "d"]
charList.sort()
print (charList)		# ["a", "b", "c", "d"]


posted on 2020-05-18 07:50  分布式编程  阅读(348)  评论(0编辑  收藏