数据类型的循环与取值--小结
一、列表
names=["Ezreal","Caitlyn","Vayne","Zed"] 1.不按照索引取值 for name in names: print(name,end=" ") # Ezreal Caitlyn Vayne Zed 2.按照索引取值 2.1 for循环 for name in range(len(names)): print(name,names[name]) 2.2while 循环 count=0 while count<len(names): print(count,names[count]) count+=1
二、元组
names=("Ezreal","Caitlyn","Vayne","Zed") 1.不按照索引取值 for name in names: print(name) 2.按照索引取值 for name in range(len(names)): print(name,names[name])
三、字典
lol={"name":"Ezreal","position":"ADC","opponent":"Vayne"} 1.只取key 1.1 通常情况 for item in lol: print(item) #name position opponent 1.2 专门玩法 for k in lol.keys(): print(i) #name position opponent 2. 只取value for v in lol.values(): print(v) #Ezreal ADC Vayne 3. 取出key和value 3.1 通常情况 for item in lol: print(item,lol[item]) 3.2 专门玩法 for k,v in lol.items(): print(k,v)
四、字符串
字符串的循环会将字符串中的所有元素依次取出,生成新的字符串
str="abcde" for i in str: print(i,type(i)) #a <class 'str'> #b <class 'str'> #c <class 'str'> #d <class 'str'> #e <class 'str'>