python相关知识点整理二(自己复习用)

字典骚操作

l = [11,22,33,44,55,66,77,88,99,90]
result = {}

for item in l:
    if item < 66:  #阅读到11时,小于66 
        if "k1" not in result:   #判断键值在不在字典中,不在
             result['k1'] = [item, ]   #创建一个键值'k1',并且键值赋值为数组[],数组中存入第一个数11,加上逗号
        else: #循环到22,判断小于66,判断里面有'k1'
            result['k1'].append(item) #k1的结果是一个数组,可以用append添加
print(result)

上式也可以这样,先把判断结果放入a b两个数组中,再计入字典。

l = [11,22,33,44,55,66,77,88,99,90]
result = {}
a=[];b=[]

for item in l:
    if item < 66: 
        a.append(item)
    else:
        b.append(item)
result['key1'] = a
result['key2'] = b
def test(x,*args,**kwargs):
    print(x)
    print(args,args[-1])
    print(kwargs,kwargs.get('y'))

test(1,*[1,2,3],**{'y':1})
test(1,1,2,1,1,11,1,y=2,z=3)

#
1
(1, 2, 3) 3   #元组
{'y': 1} 1   #字典
#
1
(1, 2, 1, 1, 11, 1) 1  #元组
{'y': 2, 'z': 3} 2   #字典
msg='i am %s my hobby is %s' % ('lhf','alex')  #格式化输出
print(msg)

msg='i am %s my hobby is %s' % ('lhf',1)
msg='i am %s my hobby is %s' % ('lhf',[1,2])
print(msg)
name='lhf'
age=19
msg='i am %s my hobby is %s' % (name,age)
print(msg)
d = {'a':3, 'b':2, 'c':6}
print(min(zip(d.values(),d.keys())))
print(d.items())
print(min((v,k) for (k,v) in d.items()))
l = [1,1,2,3,4,5,6,7,8,9]
from collections import defaultdict
l1 = defaultdict(lambda : 0)
for i in l:
    l1[i] += 1
print(l1)
defaultdict(<function <lambda> at 0x000001C77F9F2EA0>, {1: 2, 2: 1, 3: 1, 4: 1, 5: 1, 6: 1, 7: 1, 8: 1, 9: 1})





阅读更多

没有更多推荐了,返回首页