PHTHON中循环语句在列表和字典中的应用

断断续续的自学Python有一段时间了,每一次都有进步,但是也越学越难,今天就写个简单的入门教材。
列表和字典在Python中应用非常灵活,今天通过几个事例,分享一下列表和字典中几个循环打印的问题
01
打开IDLE

02
同时打印索引和列表内容
a=[10,9,3,5,2,4]
for i,p in enumerate(a):
print(i,p)
03
同时打印字典的key和value
a={"name":"crt","sex":"male","old":25}
for i,p in a.items():
print(i,p)

04
同时打印两个列表的对应值
a=['name','sex','old']
b=['crt','male','28']
for i,j in zip(a,b):
print("{}:{}".format(i,j))

05
反向打印列表的值
a=['name','sex','old']
for i in reversed(a):
print(i)

06
把一个列表中满足条件的值添加到另一个列表中
a=[1,5,8,6,2,9]
b=[]
for i in a:
if i>5:
b.append(i)

07
对列表中的值进行排序打印
a=[1,5,8,6,2,9]
for i in sorted(a):
print(i)

相比其他语言,PYTHON 语言非常简洁,也非常容易入门,并且有强大的可调用的资源库,可用于人工智能,人脸识别,数据抓取等,有兴趣的同学大家可以研究一下!
赞 (0)