遍历 dict
for k,v in dict.items():
where timestampdiff(minute, time1,time2)<30
def execute(self, sql):
self.db.ping(reconnect=True) #一直连着数据库有时候会端,所以用ping,如果断了,自动重连
self.cursor.execute(sql)
# 提交到数据库执行
self.db.commit()
results = self.cursor.fetchall()
return results
差集 set1-set2
遍历集合 for i in set1:
dict合并
dic = {**dic1,**dic2}
如果键重复,就使用dic2的值
取出字典里面值的list
list(dict.values())
取出字典里面键的list
dict.keys()
选出只有2个以内相同questionid的问题
select * from similarquestion where questionid in (select questionid from similarquestion group by questionid having count(questionid)<2)
https://zh-google-styleguide.readthedocs.io/en/latest/google-python-styleguide/python_style_rules/
Python风格规范
git compare工具推荐用beyond compare,这个可以替换git自带的对比工具,不会替换的找我。比自带的好用很多。
Python风格规范
git compare工具推荐用beyond compare,这个可以替换git自带的对比工具,不会替换的找我。比自带的好用很多。
顶级空行,要空2行,类里面的函数,空1行,
判断为空,使用if a,就可以判断非空
命名不要用list, 直接使用复数就行
try里面不要用循环,效率很低
深复制、浅复制
尽可能不用==,用is
如果函数里面,要使用全局变量,要加global
使用id 可以看内存id
60/235=0.255
====================
notepad 编辑、列块编辑、
批量替换末尾有0的
.*0 .*
注意勾选正则匹配
.*d0 .*
是匹配0前面还是有一个数字的行
然后删除空行,点击【编辑】--【行操作】--【移除空行】
=====================
内存分配
c = 2.0
d = 2.0
print(id(c), id(d), id(2.0))
print('c == d:',c==d) #True
print('c is d:',c is d) #True
对于小的数字,或者字符串,python为了节省空间,会开辟同一个内存空间,然后让两个变量都指向这个内存空间。
但是对于大的数字,比如1999998,
c is d, 就会使False。
python的赋值语句,如x=3,在内存的操作是,开辟一个内存,然后让x指向它
然后如果再写一条赋值语句,x=4,那么这是x指向的id就是4所在的ID。
但是有一个特殊,就是list等
list = [1,2,3] #id = xxxxxx30000
list[0] = 2 #这时候,list的id还是xxxxxx30000
但是list[0]的id已经变了,指向了2
也就是说