标签:简化 self append 一个 返回 *args 逆置 item 列表
列表,元组
查
索引(下标) ,都是从0开始
切片
.count 查某个元素的出现次数
.index 根据内容找其对应的位置
"haidilao ge" in a
增加
a.append() 增加
a.insert(index, "内容")
a.extend 扩展
修改
a[index] = "新的值"
a[start:end] = [a,b,c]
删除
remove("内容")
pop(index)
del a, del a[index]
a.clear() 清空
排序
sort ()
reverse()
身份判断
>>> type(a) is list
True
>>>
#Hostname:陈先生
#_Author:"Jayce Chan"
#Date:2019/1/1
a = [‘aa‘,‘bb‘,‘cc‘,‘dd‘,‘ee‘]
#增删改查
#增 切片: 经常取指定索引范围的操作,用循环十分繁琐,因此,Python提供了切片(Slice)操作符,能大大简化这种操作
print(a[1:])#取到最后
print(a[1:3])#从第二个元素取到第三个元素
print(a[1:-1])#取到倒数第二个
print(a[1:-1:1])#从左到右一个一个取
print(a[::1])#从左到右一个一个取,取到最后
print(a[1::2])#从左到右隔一个取一个,取到最后
print("从第四个元素往左一个一个取:")
print(a[3::-1])#从第四个元素往左一个一个取
print(a[3::-2])#从第四个元素往左隔一个取一个
print(a[-1::-2])#从最后一个元素往左隔一个取一个
b = a[3::-1]
print(b)
#添加 append insert
# def append(self, *args, **kwargs): # real signature unknown
# """ Append object to the end of the list. """
a.append("a1")#插在最后
print(a)
# insert(self, *args, **kwargs): # real signature unknown
# """ Insert object before index. """
a.insert(2,‘b1‘)#插入指定位置
print(a)
#修改
a[1] = ‘b‘
print(a)
a[1:3] = [‘a‘,"b"]
print(a)
#删除 remove pop del
#remove("要删除的列表里的内容")
# def remove(self, *args, **kwargs): # real signature unknown
#
# signature n.签名; 署名; 识别标志,鲜明特征; [医] 药的用法说明
# """
# Remove first occurrence of value.
#
# Raises ValueError if the value is not present.
# """
a.remove("dd") #该语句等价于remove(a[4])
print(a)
#pop() 删除某个位置的元素,并返回删除的元素
# pop(self, *args, **kwargs): # real signature unknown
# """
# Remove and return item at index (default last).
# Raises IndexError if list is empty or index is out of range.
# """
c = a.pop(1)
print(a)
print(c)
#del 用于list列表操作,删除一个或者连续几个元素a[1:3],也可删除一个对象
del a[0]
print(a)
#del a 删除a
#count()统计某个元素在列表中出现的次数
# count(self, *args, **kwargs): # real signature unknown
# """ Return number of occurrences of value. """
t = [‘to‘, ‘be‘, ‘or‘, ‘not‘, ‘to‘, ‘be‘].count(‘to‘)
print(t)
#extend()
# extend(self, *args, **kwargs): # real signature unknown
# """ Extend list by appending elements from the iterable(可迭代的; 可重复的; 迭代的;). """
d = [1,2,3]
e = [5,4,5,6]
d.extend(e)
print(d)
print(e)
#index()查找某元素第一次出现的位置
# index(self, *args, **kwargs): # real signature unknown
# """
# Return first index of value.
# Raises ValueError if the value is not present.
# """
first_5_index = d.index(5)
print("first_5_index:",d.index(5))
little_list = d[first_5_index+1:]
second_5_index = little_list.index(5)
print("second_5_index:",second_5_index)
second_5_index_inBigList = first_5_index + second_5_index +1
print("second_5_index_inBigList:",second_5_index_inBigList)
print("second_5:",d[second_5_index_inBigList])
#reverse() 逆置一个列表
# reverse(self, *args, **kwargs): # real signature unknown
# """ Reverse *IN PLACE*. """
print(a)
a.reverse()
print(a)
#sort() 在原位置对列表进行排序(依照ASCII码排序)
# sort(self, *args, **kwargs): # real signature unknown
# """ Stable sort *IN PLACE*. """
x = [4, 6, 2, 1, 7, 9]
x.sort()
#x.sort(reverse=True)#排序后逆置
print(x)
nn = type(x) is list
print(nn)
标签:简化 self append 一个 返回 *args 逆置 item 列表
原文地址:https://www.cnblogs.com/lengyao888/p/10211670.html