python开发_python中for循环操作
2013-07-19 21:00
441 查看
如果你对python中的for循环不是很清楚,请看看这篇文章:”for循环控制语句——菜鸟的Python笔记“
下面是我做的一些学习记录供大家参考:
运行效果:
下面是我做的一些学习记录供大家参考:
#基本的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 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 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 sports:足球,乒乓球,游泳 gender:M name:hongten age:20 <zip object at 0x01FA1AA8> 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'] 构造字典后 weight:55 hight:170 gender:M name:Hongten age:20 >>>
相关文章推荐
- python开发之for循环操作实例详解
- python开发之for循环操作实例详解
- python for循环列表remove操作
- Python推特开发库tweepy基本操作:Twitter for Python
- python简单的for(while) else循环
- Python取日期范围循环操作的两种方法
- Python for 循环语句使用
- python3 for 循环中的 else 语句
- [转]python 如何在一个for循环中遍历两个列表
- Python基础教程:for 循环语句 与 range()函数
- Python--for 循环语句
- python开发_python操作mysql数据库
- python for 循环
- 对python中的for循环和range内置函数详解
- python for循环
- MySQLdb for Python使用指南/Python的数据库操作
- win7环境下使用sublime text搭建python开发环境并使用SublimeREPL实现交互操作
- 关于For循环中进行异步操作索引不正确的处理方式
- Python 循环语句(for,while)
- python中矩阵(matrix或array)运算比for循环速度更快