py数组

py有四种集合类型
列表:list有序可更改的集合,允许有重复的成员
元组:tuple有序不可更改的集合,允许有重复的成员
集合:set无序无索引集合,没有重复成员
词典:dictionary无序,可变有索引的集合,没有重复成员

列表:永方括号编写
实例:

thislist=[''comthe","thecom","g0","v9st","xcc"]
print(thislist)        #创建列表
print(thislist[2])    #访问项目
#负的索引:末尾开始,-1表示最后一个项目,-2倒是第二,以此推
print(thislist[-2])    #返回v9
print(thislist[2:4])    #索引范围  返回第三项到第四项(第一项索引为0)  从2包括开始到**4**不包括结束
print(thislist[-3:-1])  #-3开始(包括-3)到-1结束(不包括)

更改项目值:使用索引

comthe=["xss","web","sql","server"]
comthe[3]="pyth"
print(comthe) 

遍历列表:使用for循环

comthe=["xss","web","sql","server"]
for x in comthe:
          print(x)

检测项目是否存在:检测是否存在指定值,使用in关键字

实例:检测是否存在“web”

thislist=[''comthe","thecom","g0","v9st","web"]
if "web" in thislist:
  print(yes.)

列表长度:使用len()

thislist=["server","ad","sql"]
print(len(thislist))

添加项目:append()

thislist=["server","ad","sql"]
thislist.append("hello")
print(thislist)

后续更新…

猜你喜欢

转载自blog.csdn.net/m0_45578555/article/details/106819020