首页 > 编程语言 > 详细

Python容器类型的遍历汇总

时间:2021-06-22 22:29:34      阅读:24      评论:0      收藏:0      [点我收藏+]

遍历列表

l = ["a","b","1",2]
print(l)
for i in l:
	print(i)
	print(type(i))

结果

[‘a‘, ‘b‘, ‘1‘, 2]
a
<class ‘str‘>
b
<class ‘str‘>
1
<class ‘str‘>
2
<class ‘int‘>

遍历字符串

s = "abcde"
print(s)
for si in s:
	print(si)
	print(type(si))

结果

abcde
a
<class ‘str‘>
b
<class ‘str‘>
c
<class ‘str‘>
d
<class ‘str‘>
e
<class ‘str‘>

遍历字典

dic = {"name":"lsj","age":18}
print(dic,type(dic))
for d in dic:
	print(d,dic[d])
	print(type(d))

结果

{‘name‘: ‘lsj‘, ‘age‘: 18} <class ‘dict‘>
name lsj
<class ‘str‘>
age 18
<class ‘str‘>

遍历集合

‘‘‘
Python大型免费公开课,适合初学者入门
加QQ群:579817333 获取学习资料及必备软件。
‘‘‘
se = {"a","1","ab",1,2,4}
print(set(se),type(se))
for se1 in se:
	print(se1)
	print(type(se1))

结果

{‘a‘, ‘ab‘, 2, 1, 4, ‘1‘} <class ‘set‘>
a
<class ‘str‘>
ab
<class ‘str‘>
2
<class ‘int‘>
1
<class ‘int‘>
4
<class ‘int‘>
1
<class ‘str‘>

遍历元组

t = ("a","1","ab",1,2,4,)
print(t,type(t))
for t1 in t:
	print(t1)
	print(type(t1))

结果

(‘a‘, ‘1‘, ‘ab‘, 1, 2, 4) <class ‘tuple‘>
a
<class ‘str‘>
1
<class ‘str‘>
ab
<class ‘str‘>
1
<class ‘int‘>
2
<class ‘int‘>
4
<class ‘int‘>

Python容器类型的遍历汇总

原文:https://www.cnblogs.com/djdjdj123/p/14920047.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!