python开发_python中for循环操作
如果你对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':'','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','','M','','']
#使用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', '', 'M', '', '']
构造字典后
weight:55
hight:170
gender:M
name:Hongten
age:20
>>>
python开发_python中for循环操作的更多相关文章
- python开发_python中字符串string操作
在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...
- python开发_python中str.format()
格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...
- python开发_python中的Boolean运算和真假值
python中的真假值: Truth Value Testing Any object can be tested for truth value, for use in an if or while ...
- python开发_python中的range()函数
python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...
- python开发_python中的module
在python中,我们可以把一些功能模块化,就有一点类似于java中,把一些功能相关或者相同的代码放到一起,这样我们需要用的时候,就可以直接调用了 这样做的好处: 1,只要写好了一个功能模块,就可以在 ...
- python开发_python中的函数定义
下面是我做的几个用列: #python中的函数定义,使用和传参 def_str = '''\ python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]): 执行语 ...
- python开发_python中的变量:全局变量和局部变量
如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...
- python开发_python中的list操作
对python中list的操作,大家可以参考: Python list 操作 以下是我个人的笔记: ============================================ Add b ...
- python开发_python关键字
python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...
随机推荐
- 构造字典:DictionaryBase类和SortedList类
DictionaryBase 类 msdn对DictionaryBase的文档解释 泛型KeyValuePair类 msdnd对泛型KeyValuePair类的文档解释 SortedList类 RUN ...
- Android Studio真机测试失败-----''No target device found" (转)
参考文章: https://blog.csdn.net/chang_sir/article/details/51755572 今天想用真机测试一个程序,却报出这样一个Error"No tar ...
- admin- 源码解析(流程)
首先我们需要了解一个知识点:---单例模式--- 单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当你希望在整个系统中 ...
- BZOJ4373 算术天才⑨与等差数列 【线段树】*
BZOJ4373 算术天才⑨与等差数列 Description 算术天才⑨非常喜欢和等差数列玩耍. 有一天,他给了你一个长度为n的序列,其中第i个数为a[i]. 他想考考你,每次他会给出询问l,r,k ...
- BZOJ4128 Matrix 【BSGS】
BZOJ4128 Matrix Description 给定矩阵A,B和模数p,求最小的x满足 A^x = B (mod p) Input 第一行两个整数n和p,表示矩阵的阶和模数,接下来一个n * ...
- 20179223《Linux内核原理与分析》第十一周学习笔记
缓冲区溢出漏洞实验 一.实验简介 缓冲区溢出是指程序试图向缓冲区写入超出预分配固定长度数据的情况.这一漏洞可以被恶意用户利用来改变程序的流控制,甚至执行代码的任意片段.这一漏洞的出现是由于数据缓冲器和 ...
- Xcode ARC需要什么版本的环境支持
Mac OS X v10.6 和 v10.7 (64位应用) 的Xcode 4.2以上版本支持ARC,iOS 4 和 iOS 5 下ARC都能工作,但Weak不支持Mac OS X v10.6 和 i ...
- 《DSP using MATLAB》示例Example 8.14
%% ------------------------------------------------------------------------ %% Output Info about thi ...
- apache与nginx对比优势及劣势
1.nginx相对于apache的优点:轻量级,同样起web 服务,比apache占用更少的内存及资源抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx 能 ...
- Errors running builder 'DeploymentBuilder' on project ' 解决方法
此问题一般发生在Myeclipse 保存文件并自动部署时候. Errors occurred during the build. Errors running builder 'DeploymentB ...