python的知识点注意事项
生活随笔
收集整理的這篇文章主要介紹了
python的知识点注意事项
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
元組中只包含一個元素時,需要在元素后面添加逗號
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)
總結
以上是生活随笔為你收集整理的python的知识点注意事项的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vim 使用记录
- 下一篇: leetcode 438. Find A