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


Posted in Python onNovember 12, 2015

本文实例讲述了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程序设计有所帮助。

Python 相关文章推荐
python爬虫入门教程--HTML文本的解析库BeautifulSoup(四)
May 25 Python
python实现反转部分单向链表
Sep 27 Python
在python中将字符串转为json对象并取值的方法
Dec 31 Python
Python中使用__new__实现单例模式并解析
Jun 25 Python
详解centos7+django+python3+mysql+阿里云部署项目全流程
Nov 15 Python
PyTorch和Keras计算模型参数的例子
Jan 02 Python
使用python 的matplotlib 画轨道实例
Jan 19 Python
Django values()和value_list()的使用
Mar 31 Python
python爬虫学习笔记之pyquery模块基本用法详解
Apr 09 Python
Python try except else使用详解
Jan 12 Python
python opencv实现图像配准与比较
Feb 09 Python
用Python简陋模拟n阶魔方
Apr 17 Python
python开发之IDEL(Python GUI)的使用方法图文详解
Nov 12 #Python
Python中pygame的mouse鼠标事件用法实例
Nov 11 #Python
Python基于pygame实现的font游戏字体(附源码)
Nov 11 #Python
python中pygame针对游戏窗口的显示方法实例分析(附源码)
Nov 11 #Python
python基于pygame实现响应游戏中事件的方法(附源码)
Nov 11 #Python
Python基于pygame实现的弹力球效果(附源码)
Nov 11 #Python
Python中pygame安装方法图文详解
Nov 11 #Python
You might like
php实现json编码的方法
2015/07/30 PHP
PHP PDO操作MySQL基础教程
2017/06/05 PHP
select组合框option的捕捉实例代码
2008/09/30 Javascript
bgsound 背景音乐 的一些常用方法及特殊用法小结
2010/05/11 Javascript
JQuery+JS实现仿百度搜索结果中关键字变色效果
2011/08/02 Javascript
js控制web打印(局部打印)方法整理
2013/05/29 Javascript
node.js中的fs.writeFile方法使用说明
2014/12/14 Javascript
AngularJS中如何使用$parse或$eval在运行时对Scope变量赋值
2016/01/25 Javascript
JS 对象(Object)和字符串(String)互转方法
2016/05/20 Javascript
AngularJS国际化详解及示例代码
2016/08/18 Javascript
浅谈mint-ui loadmore组件注意的问题
2017/11/08 Javascript
React为 Vue 引入容器组件和展示组件的教程详解
2018/05/03 Javascript
微信小程序scroll-view实现滚动穿透和阻止滚动的方法
2018/08/20 Javascript
浅谈layer弹出层按钮颜色修改方法
2019/09/11 Javascript
jQuery实现简单全选框
2020/09/13 jQuery
python实现linux下使用xcopy的方法
2015/06/28 Python
利用python写个下载teahour音频的小脚本
2017/05/08 Python
windows10下python3.5 pip3安装图文教程
2018/04/02 Python
python批量修改图片大小的方法
2018/07/24 Python
Python中的异常处理try/except/finally/raise用法分析
2019/02/28 Python
Python 转换RGB颜色值的示例代码
2019/10/13 Python
keras 获取某层的输入/输出 tensor 尺寸操作
2020/06/10 Python
keras的三种模型实现与区别说明
2020/07/03 Python
香港最新科技与优质家居产品购物网站:J SELECT
2018/08/21 全球购物
什么是lambda函数
2013/09/17 面试题
Java中的基本数据类型所占存储空间大小固定的吗
2012/02/15 面试题
实习老师个人总结的自我评价
2013/09/28 职场文书
2014的自我评价
2014/01/13 职场文书
2014教育局对照检查材料思想汇报
2014/09/23 职场文书
节水倡议书
2015/01/19 职场文书
扬州个园导游词
2015/02/06 职场文书
教师节老师寄语
2015/05/28 职场文书
无违反计划生育证明格式
2015/06/24 职场文书
2015年小学财务工作总结
2015/07/20 职场文书
百年校庆感言
2015/08/01 职场文书
《传颂之物 虚伪的假面》BD发售宣传CM公开
2022/04/04 日漫