python :list

list

赋值

list_a = [1]
list_a[0] = 10
list_a = [1, 2, 3, 4]
list_a = list([1, 2, 3, 4])
list_a = list((1, 2, 3, 4))

输出

print (list_a)
print (list_a[0])

方法

list_a.append(1)
	append(self, object, /)			将对象附加到列表的末尾

list_a.clear()
	clear(self, /)					从列表中删除所有项目

copy_a = list_a.copy()
	copy(self, /)					返回 列表的浅拷贝

count_a = list_a.count(1)
	count(self, value, /)			返回 值出现的次数,不会统计元组和列表中的值

list_a.extend(list_b)
	extend(self, iterable, /)		通过附加来自可迭代对象的元素来扩展列表(将列表b附加到a的末尾)
		
index_b = list_a.index(1)
	index(self, value, start=0, stop=9223372036854775807, /)
									返回 值 的第一个索引;如果值不存在,则引发 ValueError

list_a.insert(2 , 33)
	insert(self, index, object, /)	在索引之前插入对象(在index之后插入object)

pop_b = list_a.pop(2)
	pop(self, index=-1(下标), /)		删除并返回索引处的项目(默认最后);如果列表为空或索引超出范围,则引发 IndexError
									(删除下标的元素,并返回)		
	
list_a.remove(5)
	remove(self, value, /)			删除第一次出现的值 ;如果值不存在,则引发 ValueError。

a.reverse()
	reverse(self, /)				反向*原地*(颠倒列表)


 |  sort(self, /, *, key=None, reverse=False)
 |      Sort the list in ascending order and return None.
 |      
 |      The sort is in-place (i.e. the list itself is modified) and stable (i.e. the
 |      order of two equal elements is maintained).
 |      
 |      If a key function is given, apply it once to each list item and sort them,
 |      ascending or descending, according to their function values.
 |      
 |      The reverse flag can be set to sort in descending order.

猜你喜欢

转载自blog.csdn.net/m0_55778885/article/details/119740064