python使用技巧(二十八):减少字典计算速度的技巧


# 字典便利追加
ll={
    
    }
for i in range(10):
    ll[str(i)]=i

print(ll)
# 字典两列表追加
x=[1,3,-7,8]
y=["A","B","C","D"]
Y={
    
    }
for i in range(len(x)):
    Y[y[i]]=x[i]
print(Y.keys())  
print(Y.values())


#遍历键和值
for key,value in Y.items():
    print(key+": "+str(value))

输出结果:

{
    
    '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9}
dict_keys(['A', 'B', 'C', 'D'])
dict_values([1, 3, -7, 8])
A: 1
B: 3
C: -7
D: 8

猜你喜欢

转载自blog.csdn.net/weixin_41194129/article/details/125427081