python basic
#遍历一个序列,很多传统语言过来的,习惯用下标遍历,Python中序列是可迭代的,直接for即可!
colors=['red','green','blue','yellow']
for color in colors:
print color
#遍历倒序,用range的负数来遍历,不如直接反转遍历
for color in reversed((colors):
print (color)
#zip真的不错,尤其是处理2个序列非常简洁,遍历2个collection
names=['loe','lili','sam']
colors=['red','green','blue','yellow']
for name,color in zip(names,colors):
print (name,'---->',color)
#遍历排序的序列,sorted这个函数非常重要
colors=['red','green','blue','yellow']
for color in sorted(colors,reverse=True):
print (color)
#自定义排序,排序是一个时时刻刻都遇到的文章,有列表排序,字典排序,排序里面的key用法很重要,甚至自定义一些函数去处理
colors=['red','green','blue','yellow']
print (sorted(colors,key=len))
#遍历文件遇到指定字符退出,iter是一个内置函数用来生产迭代器,partial的不断的读入文件中32字节,注意iter引入第二个参数,表示当读入的内容是''的时候,会触发生成器stop
blocks=[]
if block in iter(partial(read,32),''):
blocks.append(blocks)
#字典的循环
d={'apple':'red','banana':'yellow','peach':'pink'}
for k in d:
print (k)
for k in d.keys():
if k.startswith('a'):
del d[k]
#字典的统计,巧妙的利用字典的get用法,取不到value的时候用缺省值0
colors=['red','green','blue','yellow','red','green']
d={}
for color in colors:
d[color]=d.get(color,0)+1
#展开序列,要学会利用tuple展开,会更快更好,而且很多源码都是这样处理的
p=['Leo','Xin','30','Coder']
fname,lname,age,email=p
#交换变量,让我想起来的冒泡排序,交换变量,Python可以直接在一行中交换
def fibonacci(n):
x,y=0,1
for i in range(n):
print x
x, y = y, x+y
#更新序列
names=['raymond','rachel','matthew','roger','betty','melisa','judith','charlie']
import collections
names=collections.deque(['raymond','rachel','matthew','roger','betty','melisa','judith','charlie'])
del names[0]
names.popleft()
names.appdenleft('mark')
python basic的更多相关文章
- Python basic (from learn python the hard the way)
1. How to run the python file? python ...py 2. UTF-8 is a character encoding, just like ASCII. 3. ro ...
- [Python] Basic operations in Pycharm
From: http://learnpythonthehardway.org/book Comment with line comment: Ctrl + slash Run: Shift + F10 ...
- Python Basic 01.Basic
01.variable ''' 변수(variable) - 자료(data)를 임시(휘발성) 저장하는 역할 - 실제 자료가 아닌 자료의 주소를 저장한다.(참조변수) ''' # 1. 변수 ...
- python Basic usage
__author__ = 'student' l=[] l=list('yaoxiaohua') print l print l[0:2] l=list('abc') print l*3 l.appe ...
- python basic programs
- python 数据分析 Numpy(Numerical Python Basic)
a = np.random.random((2,4)) a Out[5]: array([[0.20974732, 0.73822026, 0.82760722, 0.050551 ], [0.773 ...
- Python实践所遇问题记录
1.在cmd中直接输入'python'提示:'python'不是内部或外部命令,也不是可运行的程序或批处理文件. 原因:没有为Python设置环境变量. 解法:控制面板->系统->高级系统 ...
- Object Oriented Programming python
Object Oriented Programming python new concepts of the object oriented programming : class encapsula ...
- 《Python基础教程(第二版)》学习笔记 -> 第三章 使用字符串
本章讲话介绍如何使用字符串格式化其他的值,并简单了解一下利用字符串的分割.联接.搜索等方法能做些什么. 基本字符串操作 所有标准的序列操作(索引.分片.乘法.判断成员资格.求长度.取最大最小值)对字符 ...
随机推荐
- Shell脚本编程(二):shell变量
定义变量 定义变量时,变量名不加美元符号($,PHP语言中变量需要),如: your_name="runoob.com" 注意,变量名和等号之间不能有空格,这可能和你熟悉的所有编程 ...
- emacs自动折行设置
- emacs自动折行 - 临时设置下 M-x `toggle-truncate-lines` - init.el 中添加 `(toggle-truncate-lines 1)`
- PHP可变参数
0x00 缘起 在laravel的源码里经常可以看到下面的函数形式 $func(...$args) 0x01 可变参数旧写法 这表示$func支持可变参数,在php5.6之前则是在函数体内调用 fun ...
- 【iCore1S 双核心板_ARM】例程十四:FATFS实验——读写文件
实验现象: 核心代码: int main(void) { /* USER CODE BEGIN 1 */ int i; int j; FIL file; FATFS fatfs; //Âß¼Çý¶¯ ...
- WPF 4.5 is here : check out the new features !
So what's New in WPF Version 4.5 Developer Preview ? Here is a list of the new features and their re ...
- FatTree拓扑结构
FatTree拓扑结构是由MIT的Fares等人在改进传统树形结构性能的基础上提出的,属于switch-only型拓扑. 整个拓扑网络分为三个层次:自上而下分别为边缘层(edge).汇聚层(aggre ...
- RDP 数据库简介
在扩增子数据分析中,有时会发现多个OTU 注释到了同一个species , 为什么会出现这种情况呢? 首先既然在OTU水平能分开,说明序列的相似度小于97%, 同一个物种的同一个基因的片段相似度会 ...
- laravel5.4 表单提交
1.路由配置: Route::post('/posts', '\App\Http\Controllers\PostController@store'); 2.防止csrf攻击 @section('co ...
- input 手机号
关于input手机号的验证 一.手机号的判断方法: function isPhoneTel(n){ var reg = /^1[3|4|5|8]\d{9}$/; if(!!(reg.test(n))) ...
- 16解释器模式Interpreter
一.什么是解释器模式 Interpreter模式也叫解释器模式,是行为模式之一,它 是一种特殊的设计模式,它建立一个解释器,对于特定 的计算机程序设计语言,用来解释预先定义的文法.简 单地说,Inte ...