#1、list定义
list=[1,2,3,4,5]
print(list[1])
#2、list负数索引
print(list[-1])
print(list[-3])
print(list[1:3])
print(list[1:-1])
list.append(6)
print(list)
list.insert(2,0)
print(list)
list.extend([3,0])
print(list)
#4、list搜索
list.index(4)
#list.index(8) #异常
print(8 in list)
#5、list删除元素
list.remove(2) #删除第一个
print(list)
#list.remove(8) #异常
list.pop()
#6、list运算符
list1 = list + [0,0,0]
print(list1)
list += [0,0,0]
print(list)
list2 = list*3
print(list)
#7、使用join链接list成为字符串,join 只能用于元素是字符串的 list
param = {"1":"a","2":"b","3":"c"}
print(["%s=%s" % (k, v) for k, v in param.items()])
print(";".join(["%s=%s" % (k, v) for k, v in param.items()]))
#8、list分割字符串
s = param
s = ";".join(s)
print(s)
s.split(";")
print(s)
s.split(";",1) #分割次数
print(s)
#9、list的映射解析
li = [1,2,3,4]
print([i*2 for i in li])
li1 = [i*2 for i in li]
print(li1)
#10、list过滤
li = ["one","a","two","b","b"]
print([i for i in li if len(i)>1])
print([i for i in li if i!="b"])
print([i for i in li if li.count(i)==1])
list操作
最新推荐文章于 2023-07-27 09:26:10 发布