首页 > 编程 > Python > 正文

python开发之for循环操作实例详解

2020-01-04 17:56:51
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了python开发之for循环操作,以实例形式较为详细的分析了Python中for循环的具体使用技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了python开发之for循环操作。分享给大家供大家参考,具体如下:

下面是我做的一些学习记录供大家参考:

 

 
  1. #基本的for循环语句 
  2. test_list = [2,"Jone",3,6,7,'hongten','hanyuan','good',"Tom"] 
  3. #打印列表的长度 
  4. print(len(test_list)) 
  5. #遍历列表 
  6. for i in test_list: 
  7. print(i) 
  8. test_str = "hello,i'm hongten" 
  9. print('打印字符串:' + test_str) 
  10. #遍历一个字符串 
  11. print('遍历一个字符串') 
  12. for i in test_str: 
  13. print(i) 
  14. test_tuple = [("a",1),("b",2),("c",3),("d",4)] 
  15. print(test_tuple) 
  16. #遍历一个元组 
  17. print('遍历一个元组') 
  18. for (i,j) in test_tuple: 
  19. print(i,j) 
  20. test_dict = {'name':'hongten','age':'20','gender':'M','sports':'足球,乒乓球,游泳'} 
  21. #字典迭代器 
  22. for key in test_dict: 
  23. print(key + ':' + test_dict[key]) 
  24. L1 = [1,3,5,7] 
  25. L2 = [2,4,6,8] 
  26. #使用zip将两个列表合并 
  27. print(zip(L1,L2)) 
  28. for (i,j) in zip(L1,L2): 
  29. print(i,j) 
  30. print('#######################################################') 
  31. L3 = L2[:] 
  32. L3.remove(8) 
  33. print('L1,L3列表为:') 
  34. print(L1) 
  35. print(L3) 
  36. for (i,j) in zip(L1,L3): 
  37. print(i,j) 
  38. #可以看出来当长度不一的时候,多余的被忽略 
  39. test_keys = ['name','age','gender','weight','hight'] 
  40. test_values = ['Hongten','20','M','55','170'] 
  41. #使用zip来构造一个字典 
  42. print('字典中的keys:') 
  43. print(test_keys) 
  44. print('字典中的key对应的value:') 
  45. print(test_values) 
  46. print('构造字典后') 
  47. test_dic = dict(zip(test_keys,test_values)) 
  48. for key in test_dic: 
  49. print( key + ':' + test_dic[key]) 

运行效果:

 

 
  1. Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32 
  2. Type "copyright", "credits" or "license()" for more information. 
  3. >>> ================================ RESTART ================================ 
  4. >>>  
  5. 9 
  6. 2 
  7. Jone 
  8. 3 
  9. 6 
  10. 7 
  11. hongten 
  12. hanyuan 
  13. good 
  14. Tom 
  15. 打印字符串:hello,i'm hongten 
  16. 遍历一个字符串 
  17. h 
  18. e 
  19. l 
  20. l 
  21. o 
  22. , 
  23. i 
  24. ' 
  25. m 
  26.  
  27. h 
  28. o 
  29. n 
  30. g 
  31. t 
  32. e 
  33. n 
  34. [('a', 1), ('b', 2), ('c', 3), ('d', 4)] 
  35. 遍历一个元组 
  36. ('a', 1) 
  37. ('b', 2) 
  38. ('c', 3) 
  39. ('d', 4) 
  40. gender:M 
  41. age:20 
  42. name:hongten 
  43. sports:足球,乒乓球,游泳 
  44. [(1, 2), (3, 4), (5, 6), (7, 8)] 
  45. (1, 2) 
  46. (3, 4) 
  47. (5, 6) 
  48. (7, 8) 
  49. ####################################################### 
  50. L1,L3列表为: 
  51. [1, 3, 5, 7] 
  52. [2, 4, 6] 
  53. (1, 2) 
  54. (3, 4) 
  55. (5, 6) 
  56. 字典中的keys: 
  57. ['name', 'age', 'gender', 'weight', 'hight'] 
  58. 字典中的key对应的value: 
  59. ['Hongten', '20', 'M', '55', '170'] 
  60. 构造字典后 
  61. gender:M 
  62. age:20 
  63. name:Hongten 
  64. weight:55 
  65. hight:170 
  66. >>> 

希望本文所述对大家Python程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表