python的知识点注意事项

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sty945/article/details/80074347

元组中只包含一个元素时,需要在元素后面添加逗号

a = (1)
b = (2,)
print(a)
print(b)

1
(2,)

output:

列表的排序(从到到小)

selected_titles = sorted(selected_titles, key = lambda t: -eval(t[1]))

字典的排序(从大到小)

方法1

from operator import itemgetter
sorted_support = sorted(support.items(), key=itemgetter(1), reverse=True)

方法2

s_dict = sorted(a_dict.items(), key = lambda e: e[1], reverse=False)

列表字典的排序

score_10 = sorted(score_10, key=lambda t: -t['book_freq'])

取多维数组的某一列

a[:,3] #假如是四维数组,表示取数组第4列,下标从0开始
print("x array is:")
print(a)
print("\nx array [a:3] is")
print(a[:3])
print("\nx array [a:,3] is")
print(a[:,3])
output:
x array is:
[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]]

x array [a:3] is
[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]]

x array [a:,3] is
[0.2 0.2 0.2 0.2 0.2]

python中的divmod() 函数

>>>divmod(7, 2)
(3, 1)
>>> divmod(8, 2)

有序的字典

from collections import OrderedDict

# 建立词典
d = OrderedDict()

# 弹出第一个存储
d.popitem(last = False)

猜你喜欢

转载自blog.csdn.net/sty945/article/details/80074347