python关于for循环的几个函数
1.enumerate:返回2个值,1是当前的for循环的第几轮,2是循环得到的数值
enumerate works by supplying a corresponding index to each element in the list that you pass it. Each time you go through the loop, index will be one greater, and item will be the next item in the sequence.
choices = ['pizza', 'pasta', 'salad', 'nachos'] print 'Your choices are:'
for index, item in enumerate(choices):
print index+1, item
运行结果如下:
Your choices are:
1 pizza
2 pasta
3 salad
4 nachos
None
2.zip:根据最小的列表,pairs它们
It's also common to need to iterate over two lists at once. This is where the built-in zip function comes in handy.
zip will create pairs of elements when passed two lists, and will stop at the end of the shorter list.zip can handle three or more lists as well!
例子:
list_a = [3, 9, 17, 15, 19]
list_b = [2, 4, 8, 10, 30, 40, 50, 60, 70, 80, 90] for a, b in zip(list_a, list_b):
# Add your code here!
if a >= b:
print a
else:
print b
第一组得到a=3 b=2 输出3,第二组得到a=9 b=4 输出9。。。以此类推。
3.For / else:for里面有break时,else的内容不执行。
In this case, the else statement is executed after the for, but only if the for ends normally—that is, not with a break. This code will break when it hits 'tomato', so the else block won't be executed.
fruits = ['banana', 'apple', 'orange', 'tomato', 'pear', 'grape'] print 'You have...'
for f in fruits:
if f == 'tomato':
print 'A tomato is not a fruit!' # (It actually is.)
break
print 'A', f
else:
print 'A fine selection of fruits!'
ps:小练习题:
1.验证一个数是整数:
def is_int(x):
if int(x) == x:
return True
else:
return False
2.将类似于1234的数值加起来:1+2+3+4 = 10
方法1:转换为字符,将数字一个个取出来
def digit_sum(n):
s = str(n)
sum = 0
for s1 in s:
sum += int(s1)
return sum
方法2:使用%和/得到一个个数字
def digit_sum(n):
sum = 0
while n > 0:
sum += (n % 10)
n = int(n / 10)
return sum
3.迭代计算阶乘
def factorial(x):
f = 1
while x > 1:
f = f * x
x -= 1
return f
4.判断是否是质数
def is_prime(x):
if x == 1:
return False
elif x == 2:
return True
else:
s = 2
while s != x:
if x % s == 0:
return False
s += 1
else:
return True
5.倒序输出一个字符串:
def reverse(text):
s = ""
for i in range(0,len(text)):
a = text[len(text) -1 - i]
s = s + a
return s
6.返回输入列表中不重复的内容:
def remove_duplicates(list):
s = []
for i in list:
if i not in s:
s.append(i)
return s
7.返回一个列表里的中值:
def median(list):
list_sorted = sorted(list)
length = len(list_sorted)
print list_sorted
if length % 2 == 0:
return float(list_sorted[length/2 - 1] + list_sorted[length/2])/2
else:
return list_sorted[length/2]
python关于for循环的几个函数的更多相关文章
- Python语言的循环语句、迭代器与生成器、函数学习
while循环语句 无限循环 我们可以通过设置条件表达式永远不为false来实现无限循环,实例如下: for语句 Python for循环可以遍历任何序列的项目,如一个列表或者一个字符串 Python ...
- python基础之循环结构以及列表
python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.python IDE的选择 IDE的全称叫做集成 ...
- Python进阶05 循环设计
作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 在"循环"一节,我们已经讨论了Python基本的循环语法.这一 ...
- 第五篇:python基础之循环结构以及列表
python基础之循环结构以及列表 python基础之编译器选择,循环结构,列表 本节内容 python IDE的选择 字符串的格式化输出 数据类型 循环结构 列表 简单购物车的编写 1.pyth ...
- Python编程核心内容之一——Function(函数)
Python版本:3.6.2 操作系统:Windows 作者:SmallWZQ 截至上篇随笔<Python数据结构之四--set(集合)>,Python基础知识也介绍好了.接下来准备干 ...
- Python编程从入门到实践笔记——函数
Python编程从入门到实践笔记——函数 #coding=gbk #Python编程从入门到实践笔记——函数 #8.1定义函数 def 函数名(形参): # [缩进]注释+函数体 #1.向函数传递信息 ...
- Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)...啊啊啊
函数式编程 函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数调用,就可以把复杂任务分解成简单的任务,这种分解可以称之为面向过程的程序设计.函数就是面向过程的程序设计 ...
- (转)Python进阶:函数式编程(高阶函数,map,reduce,filter,sorted,返回函数,匿名函数,偏函数)
原文:https://www.cnblogs.com/chenwolong/p/reduce.html 函数式编程 函数是Python内建支持的一种封装,我们通过把大段代码拆成函数,通过一层一层的函数 ...
- 【python】Python3 循环语句
[python]几种常见的循环 注意:如果涉及到程序中print语句中含有%d,%s,那么要在脚本最开始写语句:#coding=utf-8,才能够正常输出想要的数字或者字符串. Python3 循环语 ...
随机推荐
- MongoDB学习笔记(四) 用MongoDB的文档结构描述数据关系
MongoDB的集合(collection)可以看做关系型数据库的表,文档对象(document)可以看做关系型数据库的一条记录.但两者并不完全对等.表的结构是固定的,MongoDB集合并没有这个约束 ...
- Swift - 列表项尾部附件点击响应(感叹号,箭头等)
列表单元格尾部可以添加各种样式的附件,如感叹号,三角箭头等.而且点击内容区域与点击附件的这两个响应事件是不同的,这样可以方便我们实现不同的功能(比如点击内容则查看详情,点击感叹号则编辑) 1 2 3 ...
- [知识库分享系列] 二、Web(高性能Web站点建设)
知识库分享系列: [知识库分享系列] 二..NET(ASP.NET) [知识库分享系列] 一.开篇 分享介绍 此知识库之所以为 Web 系列,因为和 .NET 没有完全的关系,其中的技术和实践实用于各 ...
- 深度RAMOS,把操作系统全部安装在内存上
你看下深度RAMOS就知道了 RAMOS+音速启动+绿色软件+云端 很爽 http://www.shenduwin7.com/jiaocheng/52.html
- tbb 线程安全concurrent_queue的性能
tbb实现了线程安全的queue,这样程序员既可以不用和那些lock,mutex,criticalsection打交道,又大大提高性能,太给力了..比较的结果见代码中的注释.结果可以看出代码足足少一半 ...
- hdu2647解题报告
题意:有个工厂的老板给工人发奖金,每人基础都是888,工人们有自己的想法,如:a 工人想要比 b 工人的奖金高,老板想要使花的钱最少 那么就可以 给b 888,给a 889 ,但是如果在此基础上,b也 ...
- 此三层非彼三层——MVC&UBD
学习了三年编程了,到如今这个阶段,開始接触架构,開始认识架构,怎样设计一个程序的结构,学名称"架构模式"(architectural pattern).个人经历告诉我这在编程中是一 ...
- js中substring或split方法取得URL中的域名
1.split方式 <html> <head></head> <body onload="convertTemp()"> <s ...
- poj 3450 Corporate Identity
题目链接:http://poj.org/problem?id=3450 题目分类:后缀数组 题意:求n个串的最长公共字串(输出字串) //#include<bits/stdc++.h> # ...
- ORACLE—005:创建JOB(二)
假设须要创建带參数的job,怎样创建呢. 我们直接将參数声明.并赋值.然后传给job调用的存储过程就可以. 比如.存储过程名为Pro_Test_JOB,參数共同拥有一个.是VARCHAR2类型. 创建 ...