数据类型的循环与取值
Posted Smart1san
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据类型的循环与取值相关的知识,希望对你有一定的参考价值。
一、列表
names=["Ezreal","Caitlyn","Vayne","Zed"]
1.不按照索引取值
for name in names:
print(name,end=" ") # Ezreal Caitlyn Vayne Zed
2.按照索引取值
a.for循环
for name in range(len(names)):
print(name,names[name])
# 0 Ezreal # 1 Caitlyn # 2 Vayne # 3 Zed
b.while 循环
count=0
while count<len(names):
print(count,names[count])
count+=1
# 0 Ezreal # 1 Caitlyn # 2 Vayne # 3 Zed
二、元组
names=("Ezreal","Caitlyn","Vayne","Zed")
1.不按照索引取值
for name in names:
print(name)
# Ezreal Caitlyn Vayne Zed
2.按照索引取值
for name in range(len(names)):
print(name,names[name])
# 0 Ezreal # 1 Caitlyn # 2 Vayne # 3 Zed
三、字典
lol={"name":"Ezreal","position":"ADC","opponent":"Vayne"}
1.不按照索引取值
a.for i in lol:
print(i)
#取出key,并不会取出value #name #position #opponent
b.for i in lol:
print(i,lol[i])
# 取出key 和value #name Ezreal #position ADC #opponent Vayne
四、字符串
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‘>
以上是关于数据类型的循环与取值的主要内容,如果未能解决你的问题,请参考以下文章