'''数据类型:
int
bool
...
数据结构:
dict (python独有的)
list
tuple (pytho独有的)
set
str
'''
# reverse() 反转
l = [1,2,3,4,5]
l.reverse()
print(l)
# reversed() 反转,但它的反转不改变原来的列表且返回一个反序的迭代器
l = [1,2,3,4,5]
l2 = reversed(l)
print(l2) # <list_reverseiterator object at 0x000001DF4CC30940>
# slice() 切片
l = [1,2,3,4,5,6,7]
lis = slice(1,5,2)
print(l[lis]) # 和 l[1,5,2] 效果一样
# 字符串
#
# str
#
# format()
#
# http://www.cnblogs.com/Eva-J/articles/7266245.html
# bytes 转换成bytes类型
print(bytes('你好',encoding='GBK')) # unicode转换成GBK的bytes
print(bytes('你好',encoding='GBK').decode('GBK'))
print(bytes('你好',encoding='UTF-8')) # unicode转换成utf-8的bytes
print(bytes('你好',encoding='UTF-8').decode('utf-8'))
# 我拿到的是GBK编码的,我想转成UTF-8编码
# 网络编程中 只能传二进制
# 照片和视频也是以二进制存储
# html网页爬取到的也是编码
# bytearray
# b_array = bytearray('你好',encoding='UTF-8')
# print(b_array)
# print(b_array[0])
# memoryview
# ord
# 字符按照unicode转数字
print(ord('a')) # 97
print(ord('你')) # 20320
# chr
# 数字按照unicode转字符
print(chr(97)) # a
# ascii
# 只要是ascii码中的内容,就打印出来,不是就转换成\u
print(ascii('好')) # '\u597d'
print(ascii('a')) # 'a'
# repr:用于%r格式化输出
name = 'KID'
print('你好,%s'%name) # 你好,KID
name = 'KID'
print('你好,%r'%name) # 你好,'KID',调用了repr
print(repr(1))
print(repr('1')) # 会带着符号全部输出
# dict
# set
# frozenset:不可变的
--------------------------------------------------------------------------------------
# len() enumerate() zip() filter() map() sorted()
# len :长度
# enumerate: 枚举
all : 列表里面存在有False的就输出false
print(all(['a','',123])) # False
print(all(['a',123])) # True
print(all([0,123])) # False
# any()
print(any([0,123])) # True
zip():返回一个迭代器
l = [1,2,3]
l2 = ['a','b','c']
print(zip(l,l2)) # <zip object at 0x000001F908DC68C8>
for i in zip(l,l2):
print(i)
(1, 'a')
(2, 'b')
(3, 'c')
个数不一样
l = [1,2,3]
l2 = ['a','b','c','d']
for i in zip(l,l2):
print(i)
(1, 'a')
(2, 'b')
(3, 'c')
个数不一样
l = [1,2,3,4]
l2 = ['a','b','c']
for i in zip(l,l2):
print(i)
(1, 'a')
(2, 'b')
(3, 'c')
类型不一样:列表与元组
l = [1,2,3]
l2 = ('*','+++',[1,2])
for i in zip(l,l2):
print(i)
(1, '*')
(2, '+++')
(3, [1, 2])
列表与字典
l = [1,2,3]
l2 = {'*':'+++','sdf':[l]}
for i in zip(l,l2):
print(i)
(1, '*')
(2, 'sdf') 只输出keys
filter()
http://www.cnblogs.com/Eva-J/articles/7266192.html
要从一个list [1, 4, 6, 7, 9, 12, 17]中删除偶数,保留奇数
def is_odd(x):
return x % 2 == 1
ret = filter(is_odd, [1, 4, 6, 7, 9, 12, 17]) # 里面第一个必须是函数的名字,不能加括号
print(ret) # 迭代器 <filter object at 0x000001B7039886A0>
返回迭代器,是为了节省内存
for i in ret:
print(i)
1
7
9
17
相当于做一个筛选
# # 与下面这种写法一样
# [i for i in [1, 4, 6, 7, 9, 12, 17] if i % 2 == 1]
#
# 只返回字符串类型
# def is_str(s):
# return type(s) == str
# ret = filter(is_str, [1, 4, 're', '7', 9, 12, 17])
# for i in ret:
# print(i)
# re
# 7
# 利用filter(),可以完成很多有用的功能,例如,删除 None 或者空字符串:
# def is_not_empty(s):
# return s and str(s).strip()
# ret = filter(is_not_empty, [1,'test', None, [], '', 'str', ' ', 'END'])
# print(ret) # <filter object at 0x000001D14BDC6B00>
# for i in ret:
# print(i)
# test
# str
# END
# 请利用filter()过滤出1~100中平方根是整数的数,即结果应该是:
# [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
# import math
# def func(num):
# return math.sqrt(num) % 1 == 0
# ret = filter(func,range(1,101))
# for i in ret:
# print(i)
#
# # map
# ret = map(abs,[1,2,4,-4,-8])
# print(ret) # <map object at 0x0000012F344169B0>
# for i in ret:
# print(i)
#
# # filter: 执行了filter之后的结果集合一定小于或等于之前的个数
# # filter只管筛选,不会改变原来的值
# # map:执行前后元素个数不变,
# # 值可能发生改变
#
# # sorted
#
# # http://www.cnblogs.com/Eva-J/articles/7265992.html
#
# # sort
# l = [1,-4,6,-8,10]
# l.sort() # [-8, -4, 1, 6, 10]
# print(l)
#
# l = [1,-4,6,-8,10]
# l.sort(key=abs) # [1, -4, 6, -8, 10]
# print(l)
#
# # sorted
#
# l = [1,-4,6,-8,10]
# print(sorted(l)) # [-8, -4, 1, 6, 10]
# print(l) # [1, -4, 6, -8, 10]
# # sort直接改变原列表
# # 生成一个新列表,占内存。
# # 说明l还在,在列表不大的时候可以用,且要保留原列表时。
#
#
# # 2.
# # 列表按照其中每一个值的绝对值排序
# li = [1,-2,3,-48,78,9]
# print(sorted(li,key = abs)) # [1, -2, 3, 9, -48, 78]
#
# 3.
# 列表按照每一个元素的len排序
li = [(1,-2),[3],[-48,78,9],'hello world']
ret = sorted(li,key = len)
print(ret) # [[3], (1, -2), [-48, 78, 9], 'hello world']
----------------------------------------------------------------------------------
# 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数
# 这段代码
def calc(n):
return n ** n
print(calc(10))
# 换成匿名函数
calc = lambda n: n ** n
print(calc(10))
# 函数名 = lambda 参数 :返回值
#参数可以有多个,用逗号隔开
#匿名函数不管逻辑多复杂,只能写一行,且逻辑执行结束后的内容就是返回值
#返回值和正常的函数一样可以是任意数据类型
# 请把以下函数变成匿名函数
def add(x,y):
return x+y
ret = lambda x,y:x+y
print(ret(5,6))
# 上面是匿名函数的函数用法。除此之外,匿名函数也不是浪得虚名,它真的可以匿名。在和其他功能函数合作的时候
l=[3,2,100,999,213,1111,31121,333]
print(max(l))
dic = {'k1':10,'k2':100,'k3':30}
# print(max(dic))
print(max(dic,key= lambda k:dic[k]))
#map
res = map(lambda x:x**2,[1,5,7,4,8])
for i in res:
print(i)
#filter
res = filter(lambda x:x>10,[5,8,11,9,15])
for i in res:
print(i)
# min max filter map sorted --> lambda
现有两元组(('a'),('b')),(('c'),('d')),请使用python中匿名函数生成列表[{'a':'c'},{'b':'d'}]
看到匿名函数,就要想到肯定会带着考内置函数,而和匿名函数相关的内置函数只有5个:min max filter map sorted
排除法想到map
# 没用匿名函数
tu1 =(('a'),('b'))
tu2 =(('c'),('d'))
res = zip(tu1,tu2)
def func(tup):
return {tup[0]:tup[1]}
ret = map(func,res)
# for i in ret:
# print(i)
list(ret)
# 用匿名函数
tu1 =(('a'),('b'))
tu2 =(('c'),('d'))
res = zip(tu1,tu2)
# def func(tup):
# return {tup[0]:tup[1]}
ret = map(lambda tup:{tup[0]:tup[1]},res)
# for i in ret:
# print(i)
print(list(ret))
# 简化后:
print(list(map(lambda tup:{tup[0]:tup[1]},zip((('a'),('b')),(('c'),('d'))))))