标签:
主要是列表
1 #!/usr/bin/env python 2 # -*- coding:utf-8 -*- 3 #字符串的格式化输出 4 name = "han" 5 print("i am %s"%name) 6 7 #列表 8 namee = ["a","b","c",345,name,"love"] 9 print(namee) 10 print (namee[1]) 11 12 #切片 13 print(namee[:3]) 14 print(namee[-3:]) 15 print(namee[-3:][-1][0]) 16 17 #修改一个字符串 18 namee[5] = "hani"#把love改成hani 19 print(namee) 20 21 #插入一个字符,在第一个位置 22 namee.insert(1,"woshi") 23 print(namee) 24 #在结尾追加一个字符串 25 namee.append("wahaha") 26 print(namee) 27 28 #移除一个字符串 29 namee.remove("a") 30 print(namee) 31 #------------------------------------------------------------ 32 nam = ["alex","jack","peter","aaron","fiona","kai"] 33 nam.insert(-1,"abc") 34 print(nam) 35 #步长的概念 36 print("----------------",nam[::2]) 37 38 #del删除内存中的数据 39 del nam[4:6] #连续删除两个字符串 40 print(nam) 41 42 nam2 = nam[2:6] 43 print(nam2) 44 45 num = ["alex","jack","peter","aaron","fiona","kai",3,34,45,56,76,87,78,45,3,3,3,3,3,3,44,5,55,65,565767,87686,98,76] 46 #print(78 in num) 47 if 3 in num: 48 print("3 in num")#如果存在3打印3in num 49 50 num_of_ele = num.count(3)#统计存在多少个3 51 pos_of_ele = num.index(3)#查看3的坐标 52 53 num[pos_of_ele] = 67 54 print(num) 55 print("[%s] 3 are in num,pos[%s]"%(num_of_ele,pos_of_ele)) 56 print("===============================") 57 for i in range(num.count(3)):#循环3出现的次数 58 ele_index = num.index(3) #取出3的坐标 59 num[ele_index]="3a" #替换所有的3为3a 60 print(num)
标签:
原文地址:http://www.cnblogs.com/hanxiaobei/p/5658976.html