v = dict.fromkeys({'k1','k2'},[])
v['k1'].append(['356'])
print(v)
v['k1'] = '777'
print(v)
{'k2': [['356']], 'k1': [['356']]}
{'k2': [['356']], 'k1': '777'}
Process finished with exit code 0
A:map B: reduce C: filter D: mapreduce
dict1 = {}
dict2 = {3:5}
dict3 = {[1,2,3]:'aaa'} #数组是可变类型,不能做建
dict4 = {(1,2,3):'aaa'}
df.apply(lambda x:x.sum(),axis = 1) #TypeError: apply() missing 1 required positional argument: 'func'
df.applymap(abs()) #TypeError: abs() takes exactly one argument (0 given)
df.rolling(10).apply(lambda x:x.rank()) #TypeError: rolling() missing 1 required positional argument: 'window'
df.applymap(np.log) #TypeError: applymap() missing 1 required positional argument: 'func'
x=y=z=1
x=(y=z+1) #非法
x,y = y
x+=y
除了字典外,所有标准对象均可用于布尔测试 #错误,字典对象也可以用于布尔测试
空字符串的布尔值是False
空列表对象的布尔值是False
值为0的任何数字对象的布尔值是False
min = x if x< y else y
while True:pass
if (x>y) print(x) #缺少分号
max = x>y ? x:y #就是不对
def shouNumber(numbers): 传入的不是可迭代对象就会报错,字典也可以被迭代
for n in numbers:
print(n)
def changeInt(number):
number = number + 1
print("number:",number)
number1 = 2
changeInt(number1)
print("number1:",number1)
number: 3
number1: 2
Process finished with exit code 0
__new__ Create and return a new object
__iter__ 用来生成迭代器。
__dict__ 储存变量
__iterator__ 无此函数
class A:
def showinfo(self):
print(self.x)
a = A()
a.showinfo()
可以实例化,但是不能调用方法,编译器会有未定义提示
class Hello:
def __init__(self,name):
self.name = name
def showinfo(self):
print(self.name)
h = Hello('111')
h.showinfo()
g = (x*x for x in range(10))
l = [x*x for x in range(10)]
print(g==l)
print(g[0]==l[0])
print(type(g)==type(l))
print(sum(g)==sum(l))
print(g,l)
False
第二条执行失败 'generator' object is not subscriptable
False
True
at 0x0000022469AAF1B0> [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
def func(a,*args,b):pass
def func1(a,**kwargs):pass
def func3(a,b,*args,**kwargs):pass
def func2(a,**kwargs,*args,c):pass False
a = [1,2,3,4,5,6,7]
b = filter(lambda x:x > 5,a)
print(list(b))
[6, 7]
from functools import reduce
a = reduce(lambda x,y:x*y,range(1,4))
print(a)
6 #1*2 *3
Process finished with exit code 0
import copy
a = [1,2,3,4,['a','b']]
b = copy.copy(a)
c = copy.deepcopy(a)
print(id(a[1]) == id(b[1]))
print(id(a[-1]) == id(b[-1]))
print(id(a[-1]) == id(c[-1]))
True
True
False
Process finished with exit code 0
import mo
from mo import *
import mo as m
A1 = {'小明':93,'小红':88,'小刚':100}
A2 = {'小刚':100,'小明':93,'小红':88}
print(A1 == A2)
True
Process finished with exit code 0
a = [1,2,3]
b = [4,5]
a.append(b)
print(a)
[1, 2, 3, [4, 5]]
Traceback (most recent call last):
File "E:/DataAnalysis/project/其他/test.py", line 10, in
print(sum(a))
TypeError: unsupported operand type(s) for +: 'int' and 'list'
import re
a = "601988-中国银行"
print(re.findall("(\d+)-([\u4e00-\u9fa5]+)",a))
[('601988', '中国银行')]
Process finished with exit code 0
a = {"a":1,"b":2,"c":3}
print(max(a))
print(max(a,key = lambda x:a[x]))
c
c
Process finished with exit code 0
print(0 or False and 1)
from collections import deque
queue = deque(['Eric','John','Michael'])
queue.append('Terry')
queue.append('Graham')
print(queue)
queue.popleft()
print(queue)
deque(['Eric', 'John', 'Michael', 'Terry', 'Graham'])
deque(['John', 'Michael', 'Terry', 'Graham'])
Process finished with exit code 0