[Head First Python]5. summary
1- "原地"排序-转换后替换
>>> list = [2,1,3]
>>> list.sort()
>>> list
[1, 2, 3]
降序 reverse = True
>>> list.sort(reverse = True)
>>> list
[3, 2, 1, 1]
2- "复制"排序-转换然后返回
>>> data = []
>>> list = [3,2,4,1]
>>> data = sorted(list)
>>> data
[1, 2, 3, 4]
>>> list
[3, 2, 4, 1]
>>>
降序参数 reverse = True
>>> data = sorted(list, reverse = True)
>>> data
[5, 3, 2, 1]
3- "方法串链"-从左向右读,对数据应用一组方法
try:
with open(filename) as f:
data = f.readline()
return ( data.strip().split(','))
4- "函数串链"-从右向左读,对数据应用一组函数
print( sorted( set ([sanitize(s) for s in julie]) )[0:3] )
5- "列表推导" - 在一行上指定一个转换(不是使用迭代)
>>> new_l = []
>>> for each_item in old_l:
... new_l.append(len(each_item))
可用下面方法替换
>>> new_l = []
>>> new_l = [ len(s) for s in old_l ]
6- "分片" 从一个列表访问多个列表项 [1:2] 不包含2,只显示第1个项目, 从0开始
>>> list = [1,2,3,4]
>>> list[1:2]
[2]
>>> list[1:3]
[2, 3]
>>>
7- "集合"- 一组无序的数据项,其中不包含重复项,使用set工厂
>>> list = [1,1,2,3]
>>> set( list )
{1, 2, 3}
print( sorted( set ([sanitize(s) for s in james]) )[0:3] )
[Head First Python]5. summary的更多相关文章
- [Head First Python]6. summary
1- 字典-内置数据结构,数据值与键值关联 键-字典中查找部分 值-字典中数据部分 使用dict()工厂函数或者只用{}可以创建一个空字典 >>> list = {} >> ...
- [Head First Python]4. summary
1- strip()方法可以从字符串去除不想要的空白符 (role, line_spoken) = each_line.split(":", 1) line_spoken = li ...
- Python Syntax Summary
# _*_ coding: utf-8 _*_ """########################################################## ...
- Python初体验
今天开始所有的工作脚本全都从perl转变到python,开发速度明显降低了不少,相信以后随着熟练度提升会好起来.贴一下今天一个工作代码,由于之前去一家小公司测序时,序列长度竟然都没有达到要求,为了之后 ...
- C#调用Python脚本打印pdf文件
介绍:通过pdf地址先将文件下载到本地,然后调用打印机打印,最后将下载的文件删除. 环境:windows系统.(windows64位) windows系统中安装python3.6.2环境 资料: O ...
- 05基于python玩转人工智能最火框架之TensorFlow基础知识
从helloworld开始 mkdir mooc # 新建一个mooc文件夹 cd mooc mkdir 1.helloworld # 新建一个helloworld文件夹 cd 1.helloworl ...
- Python实例--C#执行Python脚本,传参
# -*- coding: utf-8 -*- # 第一行的目的,是为了让代码里面,可以有中文注释信息. (否则要运行报错) # 这个 Python 脚本, 用于被 C# 来调用. # 简单测试 He ...
- Cheatsheet: 2013 09.22 ~ 09.30
Other Python basics summary Another article about big O notation Mobile Getting Started with PhoneGa ...
- TensorFlow应用实战 | TensorFlow基础知识
挺长的~超出估计值了~预计阅读时间20分钟. 从helloworld开始 mkdir 1.helloworld cd 1.helloworldvim helloworld.py 代码: # -*- c ...
随机推荐
- Lua绑定C++类
原文:http://blog.csdn.net/chenee543216/article/details/12074771 以下是代码: Animal.h文件 #pragma once #ifndef ...
- js基础小总结之string&array&object
一.数据类型之间的转换 string--->number :parseInt(string)/parseFloat(string); 注:在Date中,因为返回值date为单位为ms的字符串,将 ...
- UIAppearance使用详解-备
iOS5及其以后提供了一个比较强大的工具UIAppearance,我们通过UIAppearance设置一些UI的全局效果,这样就可以很方便的实现UI的自定义效果又能最简单的实现统一界面风格,它提供如下 ...
- csdn的下载链接token
qt之QComboBox定制 http://www.cnblogs.com/swarmbees/p/5710714.html http://download.csdn.net/detail/qq_30 ...
- 微软官方的Unity支持组件
https://unity.codeplex.com/ http://www.nuget.org/packages/Unity.Interception/ http://www.nuget.org/p ...
- jquery使用总结
jquery使用总结-常用DOM操作 (1)查询或设置元素属性操作 html() //获取匹配元素集合中的第1个元素 html(htmlString) //为匹配集合中的所有元素设置内容 tex ...
- Linux下如何选择文件系统:EXT4、Btrfs 和 XFS
老实说,人们最不曾思考的问题之一是他们的个人电脑中使用了什么文件系统.Windows 和 Mac OS X 用户更没有理由去考虑,因为对于他们的操作系统,只有一种选择,那就是 NTFS 和 HFS+. ...
- poj 3662 Telephone Lines(好题!!!二分搜索+dijkstra)
Description Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone compa ...
- hdu 3711 Binary Number(暴力 模拟)
Problem Description For non-negative integers x and y, f(x, y) , )=,f(, )=, f(, )=. Now given sets o ...
- dispatch_group_async
- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. dispat ...