内置函数补充:
reversed()
保留原列表,返回一个反向的迭代器

l = [1,2,3,4,5]
l.reverse()
print(l)
l = [1,2,3,4,5]
l2 = reversed(l)
print(l2)
l = (1,2,23,213,5612,342,43)
sli = slice(1,5,2)
print(l[sli])
print(l[1:5:2]) print(format('test', '<20'))
print(format('test', '>40'))
print(format('test', '^40'))

bytes 转换成bytes类型

我拿到的是gbk编码的,我想转成utf-8编码
print(bytes('你好',encoding='GBK')) # unicode转换成GBK的bytes
print(bytes('你好',encoding='utf-8')) # unicode转换成utf-8的bytes

网络编程 只能传二进制
照片和视频也是以二进制存储
html网页爬取到的也是编码

b_array = bytearray('你好',encoding='utf-8')
print(b_array)
print(b_array[0])
'\xe4\xbd\xa0\xe5\xa5\xbd'
s1 = 'alexa'
s2 = 'alexb'

l = 'ahfjskjlyhtgeoahwkvnadlnv'
l2 = l[:10]

切片 —— 字节类型 不占内存
字节 —— 字符串 占内存

print(ord('好'))
print(ord(''))
print(chr(97))
print(ascii('好'))
print(ascii(''))
name = 'egg'
print('你好%r'%name)
print(repr(''))
print(repr(1))
print(all(['a','',123]))
print(all(['a',123]))
print(all([0,123]))
print(any(['',True,0,[]]))
l = [1,2,3,4,5]
l2 = ['a','b','c','d']
l3 = ('*','**',[1,2])
d = {'k1':1,'k2':2}
for i in zip(l,l2,l3,d):
print(i)
def is_odd(x):
return x % 2 == 1
def is_str(s):
return s and str(s).strip()
ret = filter(is_odd, [1, 6, 7, 12, 17])
ret = filter(is_str, [1, 'hello','',' ',None,[], 6, 7, 'world', 12, 17])
print(ret)
for i in ret:
print(i)
[i for i in [1, 4, 6, 7, 9, 12, 17] if i % 2 == 1]
from math import sqrt
def func(num):
res = sqrt(num)
return res % 1 == 0
ret = filter(func,range(1,101))
for i in ret:
print(i)
ret = map(abs,[1,-4,6,-8])
print(ret)
for i in ret:
print(i)
filter 执行了filter之后的结果集合 <= 执行之前的个数
filter只管筛选,不会改变原来的值
map 执行前后元素个数不变,值可能发生改变
l = [1,-4,6,5,-10]
# l.sort(key = abs) # 在原列表的基础上进行排序
# print(l)
print(sorted(l,key=abs,reverse=True)) # 生成了一个新列表 不改变原列表 占内存
print(l)
l = [' ',[1,2],'hello world']
new_l = sorted(l,key=len)
print(new_l)

匿名函数

def add(x,y):
return x+y
add = lambda x,y:x+y
print(add(1,2))
dic={'k1':10,'k2':100,'k3':30}
def func(key):
return dic[key]
print(max(dic,key=func)) #根据返回值判断最大值,返回值最大的那个参数是结果
print(max(dic,key=lambda key:dic[key]))
max([1,2,3,4,5,-6,-7],key=abs)
ret = map(abs,[-1,2,-3,4])
for i in ret:
print(i)
def func(x):
return x**2
ret = map(func,[-1,2,-3,4])
for i in ret:
print(i)
ret = map(lambda x:x**2,[-1,2,-3,4])
def func(x):
return x>10 res = filter(func,[5,8,11,9,15])
for i in res:
print(i)

min max filter map sorted —— lambda

d = lambda p:p*2
t = lambda p:p*3
x = 2
x = d(x) #x = 4
x = t(x) #x = 12
x = d(x) #x = 24
print(x)

map方法的应用

ret = zip((('a'),('b')),(('c'),('d')))
ret = map(lambda t:{t[0]:t[1]},ret)
print(list(ret))

现有两元组(('a'),('b')),(('c'),('d')),
请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}]

#匿名函数 == 内置函数

# 方法一
t1=(('a'),('b'))
t2=(('c'),('d'))
# print(list(zip(t1,t2)))
print(list(map(lambda t:{t[0],t[1]},zip(t1,t2))))

# 方法二
print(list([{i,j} for i,j in zip(t1,t2)]))

#方法三
func = lambda t1,t2:[{i,j} for i,j in zip(t1,t2)]
ret = func(t1,t2)
print(ret)

Python之路----内置函数补充与匿名函数的更多相关文章

  1. python之路——内置函数和匿名函数

    阅读目录 楔子 内置函数 匿名函数 本章小结 楔子 在讲新知识之前,我们先来复习复习函数的基础知识. 问:函数怎么调用? 函数名() 如果你们这么说...那你们就对了!好了记住这个事儿别给忘记了,咱们 ...

  2. 百万年薪python之路 -- 内置函数二 -- 最常用的内置函数

    1.内置函数 1.1 匿名函数 匿名函数,顾名思义就是没有名字的函数(其实是有名字的,就叫lambda),那么什么函数没有名字呢?这个就是我们以后面试或者工作中经常用匿名函数 lambda,也叫一句话 ...

  3. 《Python》 内置函数补充、匿名函数、递归初识

    一.内置函数补充: 1.数据结构相关(24): 列表和元祖(2):list.tuple list:将一个可迭代对象转化成列表(如果是字典,默认将key作为列表的元素). tuple:将一个可迭代对象转 ...

  4. Python开发基础-Day11内置函数补充、匿名函数、递归函数

    内置函数补充 python divmod()函数:把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b) 语法: divmod(a, b) #a.b为数字,a为除数,b ...

  5. Python之路----------内置函数

    1.abs(x)绝对值 #coding=utf-8 a = 1 b = -2 print(abs(a)) print(abs(b)) 2.all(iterable)可迭代对象里面所有内容为真返回真,空 ...

  6. python之路--内置函数, 匿名函数

    一 . 内置函数 什么是内置函数? 就是python给你提供的. 拿来直接⽤的函数, 比如print., input等等. 字符串类型代码的执⾏ eval() 执⾏字符串类型的代码. 并返回最终结果( ...

  7. python之路 内置函数,装饰器

    一.内置函数 #绝对值 abs() #所有值都为真才为真 all() #只要有一个值为真就为真 any() #10进制转成二进制 bin() #10进制转成八进制 oct() #10进制转成十六进制 ...

  8. 百万年薪python之路 -- 内置函数

    内置对象(68个)第一部分 内置函数一共68个 一些可以重要性不高的内置函数,只需了解即可 all() any() bytes() callable() chr() ord() complex() d ...

  9. python基础之内置函数补充、匿名函数、递归函数

    内置函数补充 python divmod()函数:把除数和余数运算结果结合起来,返回一个包含商和余数的元组(a // b, a % b) 语法: 1 divmod(a, b) #a.b为数字,a为除数 ...

随机推荐

  1. Mongodb 副本集 数据同步简单测试

    副本集的搭建,请见  CENTOS6.5 虚拟机MONGODB创建副本集 接下来将简单说明下副本集之间的数据同步. 1.首先,进入primary节点 MOGO_PATH/bin/mongo  -por ...

  2. php里面向指定的页面提交数据

    在jquery里用 load post 等等,无法得到我想要的结果!于是突然-----这几天想的东西都白想了,现在只好这样了 现在想在php里面向指定的页面提交数据,应该有,还可以有返回值 于是找了这 ...

  3. Java-查询已创建了多少个对象

    //信1603 //查询创建了多少个对象//2017.10.19public class Lei {//记录对象个数 ;//生成一个对象就自加加 public Lei() { x++; }public ...

  4. codeforces 14A - Letter & codeforces 859B - Lazy Security Guard - [周赛水题]

    就像title说的,是昨天(2017/9/17)周赛的两道水题…… 题目链接:http://codeforces.com/problemset/problem/14/A time limit per ...

  5. ZJU-1003 Crashing Balloon dfs,

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=3 题意(难以描述):A,B两个人从1~100选数乘起来比谁的大(不能选重复的或者 ...

  6. php iconv() : Detected an illegal character in input string

    php iconv() : Detected an illegal character in input string_php技巧_脚本之家 https://www.jb51.net/article/ ...

  7. 单机闭环 使用Nginx+Lua开发高性能Web应用

    [西域骆驼D1532101213]西域骆驼(VANCAMEL)D1532101213 休闲套脚鞋 卡其43[行情 报价 价格 评测]-京东 http://item.jd.com/1856564.htm ...

  8. 一个JS Class的“增删改查”

    function AA (){ var r={}; this.get = function(key){ return r[key]; } this.put = function(key,x){ r[k ...

  9. 单例模式:Qt本身就提供了专门的宏 Q_GLOBAL_STATIC 通过这个宏不但定义简单,还可以获得线程安全性

    标题起的是有点大 主要是工作和学习中,遇到些朋友,怎么说呢,代码不够Qt化 可能是由于他们一开始接触的是 Java MFC 吧 接触 Qt 7个年头了 希望我的系列文章能抛砖引玉吧 单例模式 很多人洋 ...

  10. Is It A Tree?----poj1308

    http://poj.org/problem?id=1308 #include<stdio.h> #include<string.h> #include<iostream ...