python列表的方法

百般思念 提交于 2020-02-27 05:35:21
aStr="火锅底料,白菜,土豆"
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]


print(aStr[2])
print(alist[2:4])

print(alist[4:])

#索引区范围,包前不报后

print("--------")
alist1=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
alist2=[1,2,3,4,5]
alist=[alist1,alist2]
print(alist)
#列表可以嵌套

print("----append----")

alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
alist.append('肥牛')
print(alist)

print("----pop----")
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
print(alist)
alist.pop()
print(alist)




print("----del----")
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
print(alist)
del alist[-1]
print(alist)
print("----运算----")
alist1=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
alist2=[1,2,3,4,5]
alist=alist1+alist2
print(alist)
#alist=alist1*5
print("alist:",alist)
print("alist1:",alist1)
print("alist2:",alist2)


print("----extend----")
alist1=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
alist2=[1,2,3,4,5]
alist1.extend(alist2)
print("alist1:",alist1)
print("alist2:",alist2)
#extend会改变列表中的数据,而加法不会改变原有列表中的数据


print("----index----")
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
print(alist)
print(alist.index("粑粑"))
#index会报错

print("----insert----")
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
alist.insert(1,"香油")
print(alist)


print("----remove----")
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
print(alist)
alist.remove('臭豆腐')
print(alist)
print("----remove----")
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
print(alist)
alist.clear()
print(alist)

print("--------")
#len 判断列表元素的总数
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
print(len(alist))
aStr="abcdefg"
print(len(aStr))
print("--------")
#max min
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
print(max(alist))
print(min(alist))

aStr=['ax','b','c','d','e','f','ga']
anStr=[0,1,2,3,4,5,6]
print("aStr max:",max(aStr))
print("aStr min:",min(aStr))
#判断字符首字母的编码
print("anStr max:",max(anStr))
print("anStr min:",min(anStr))


print("--------")

#判断数据出现次数
anStr=[0,1,2,3,4,5,6,6,4,6,6,5]
print(anStr.count(6))

aStr=['ax','b','c','d','e','f','ga']
print(anStr.count("a"))
#字符串的判断必须完全相同才会返回数字


#
print("--------")
alist=["火锅底料","白菜","土豆","萝卜","臭豆腐","粑粑"]
#while遍历
i=0
while (i<6):
    print(alist[i])
    i+=1
print("--------")

#for 遍历
for i in alist:
    print(i)

print("--------")
anStr=[0,1,2,3,4,5,6,6,4,6,6,5]
print(anStr.sort())
print(anStr)
#sort方法更改的是源列表
anStr=[0,1,2,3,4,5,6,6,4,6,6,5]
anStr1=anStr.copy()
#anStr1并不是一个新的列表,仅仅是调用了anStr
#anStr里的数据变化,anStr1数据也会跟着变化
print(anStr.sort(reverse= True))
print(anStr)
print(anStr1)

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!