黑松山资源网 Design By www.paidiu.com
本文实例讲述了python开发之for循环操作。分享给大家供大家参考,具体如下:
下面是我做的一些学习记录供大家参考:
#基本的for循环语句 test_list = [2,"Jone",3,6,7,'hongten','hanyuan','good',"Tom"] #打印列表的长度 print(len(test_list)) #遍历列表 for i in test_list: print(i) test_str = "hello,i'm hongten" print('打印字符串:' + test_str) #遍历一个字符串 print('遍历一个字符串') for i in test_str: print(i) test_tuple = [("a",1),("b",2),("c",3),("d",4)] print(test_tuple) #遍历一个元组 print('遍历一个元组') for (i,j) in test_tuple: print(i,j) test_dict = {'name':'hongten','age':'20','gender':'M','sports':'足球,乒乓球,游泳'} #字典迭代器 for key in test_dict: print(key + ':' + test_dict[key]) L1 = [1,3,5,7] L2 = [2,4,6,8] #使用zip将两个列表合并 print(zip(L1,L2)) for (i,j) in zip(L1,L2): print(i,j) print('#######################################################') L3 = L2[:] L3.remove(8) print('L1,L3列表为:') print(L1) print(L3) for (i,j) in zip(L1,L3): print(i,j) #可以看出来当长度不一的时候,多余的被忽略 test_keys = ['name','age','gender','weight','hight'] test_values = ['Hongten','20','M','55','170'] #使用zip来构造一个字典 print('字典中的keys:') print(test_keys) print('字典中的key对应的value:') print(test_values) print('构造字典后') test_dic = dict(zip(test_keys,test_values)) for key in test_dic: print( key + ':' + test_dic[key])
运行效果:
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 Type "copyright", "credits" or "license()" for more information. > ================================ RESTART ================================ > 9 2 Jone 3 6 7 hongten hanyuan good Tom 打印字符串:hello,i'm hongten 遍历一个字符串 h e l l o , i ' m h o n g t e n [('a', 1), ('b', 2), ('c', 3), ('d', 4)] 遍历一个元组 ('a', 1) ('b', 2) ('c', 3) ('d', 4) gender:M age:20 name:hongten sports:足球,乒乓球,游泳 [(1, 2), (3, 4), (5, 6), (7, 8)] (1, 2) (3, 4) (5, 6) (7, 8) ####################################################### L1,L3列表为: [1, 3, 5, 7] [2, 4, 6] (1, 2) (3, 4) (5, 6) 字典中的keys: ['name', 'age', 'gender', 'weight', 'hight'] 字典中的key对应的value: ['Hongten', '20', 'M', '55', '170'] 构造字典后 gender:M age:20 name:Hongten weight:55 hight:170 >
希望本文所述对大家Python程序设计有所帮助。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 徐璨宾《井底的蚯蚓》[320K/MP3][78.77MB]
- 碧蓝航线国庆金秋版本重磅上线 新玩法3D宿舍系统闪亮登场
- 自在西游自在服即将在10月1日开启 入驻即享豪礼满满
- 炉石传说最强术士卡组是哪个 术士最强天梯卡组推荐一览
- 炉石传说最强萨尔卡组是哪个 萨尔最强天梯卡组推荐一览
- 炉石传说最强潜行者卡组是哪个 潜行者最强天梯卡组推荐一览
- 银霞.1983-《爱迷惑我·我住小楼中》台湾复刻版[WAV+CUE]
- [雨果唱片]粤曲名家-《再折长亭柳》[WAV+CUE]
- 刘德华1998-你是我的女人[香港第二版][WAV]
- 压迫感谁最强?外媒评选恐怖游戏最佳女反派
- 还没正式发售:《寂静岭2:重制版》Steam玩家峰值近9000人
- 别搞政确了!超95%国外玩家反对强制包容性
- 《暗喻幻想:ReFantazio》试玩:ATLUS的自我挑战?
- 《憧憬成为魔法少女》第二季 反转魔法少女题材再掀热潮
- 海信AI电视E7N正式发布,引领AI画质新标杆