python中最常见数据结构的遍历:字符串、列表、元祖、字典

1、字符串遍历

strs="hello world!"

for str in strs:

 print(str,end=" ")

2、列表遍历


list = [1, 2, 3, 4]
for num in list:
 print(num,end=' ')

3、元组遍历

turple = (1, 2, 3, 4)
for num in turple:
 print(num,end=" ")
4、字典遍历

<1> 遍历字典的key

dict= {"film":"spiderman","type":"fantasy"}
for key in dict.keys():
 print(key,end=" ")
<2> 遍历字典的value
dict= {"film":"spiderman","type":"fantasy"}
for value in dict.values():
 print(value,end=" ")
<3> 遍历字典的项(元素)

dict= {"film":"spiderman","type":"fantasy"}
for item in dict.items():
 print(item,end=" ")
<4> 遍历字典的key-value(键值对)

dict= {"film":"spiderman","type":"fantasy"}
for key,value in dict.items():
 print("键:%s,值:&s"%(key,value))

你可能感兴趣的:(python)