【Python筆記】:常用備查

1.[10 32 16 16]的數組只取3x3的小塊

#a.shape = [10 32 16 16]
print (a[:,:,0:3,0:3][0][0])

輸出:

[[ 0.13789405  0.07708804  3.4677859 ]
 [ 0.53664948  0.42124621  0.14421479]
 [-0.05646547 -0.45452007  0.05854976]]

2.Python中Numpy庫中np.sum(array,axis=0,1,2…)怎麼理解?

3.numpy數組旋轉180度

import numpy as np
ac = np.arange(0,9,1).reshape(3,3)
print (ac)
dc = np.rot90(ac,1)
print ("將矩陣逆時針旋轉90度")
print (dc)
ec = np.rot90(dc,1)
print ("再將矩陣逆時針旋轉90度")
print (ec)
fc = np.rot90(ac,2)
print ("將矩陣逆時針旋轉180度")
print (fc)

輸出:

[[0 1 2]
 [3 4 5]
 [6 7 8]]
將矩陣逆時針旋轉90[[2 5 8]
 [1 4 7]
 [0 3 6]]
再將矩陣逆時針旋轉90[[8 7 6]
 [5 4 3]
 [2 1 0]]
將矩陣逆時針旋轉180[[8 7 6]
 [5 4 3]
 [2 1 0]]

4.Python行內中文

# -*- coding: UTF-8 -*-

5.np.argmax()

>>> a = np.arange(6).reshape(2,3)
>>> a
array([[0, 1, 2],
       [3, 4, 5]])
>>> np.argmax(a)
5
>>> np.argmax(a, axis=0)
array([1, 1, 1])
>>> np.argmax(a, axis=1)
array([2, 2])

>>> b = np.arange(6)
>>> b[1] = 5
>>> b
array([0, 5, 2, 3, 4, 5])
>>> np.argmax(b) # Only the first occurrence is returned.
1

6.構造數組

print (np.ones(9))
print (np.zeros(9))
np.arange(9).reshape(3,3)

7.pickle 增量存儲:

增量存儲不同shape的數組,然後原樣讀取

import pickle
data1 = np.arange(9).reshape(3,3)
data2 = np.arange(8).reshape(2,4)

output = open('./data.pkl', 'wb')

# Pickle dictionary using protocol 0.
pickle.dump(data1, output)
pickle.dump(data2, output)

output.close()
import pickle

pkl_file = open('./data.pkl', 'rb')

for i in range(0,2):                #注意:pickle存儲可以增量存儲,但是讀取時不是一下子讀出來的,而是按順序一個一個讀取        
    data_pkl = pickle.load(pkl_file)
    print(data_pkl)
    print ("\n")

pkl_file.close()
[[0 1 2]
 [3 4 5]
 [6 7 8]]


[[0 1 2 3]
 [4 5 6 7]]

Python pickle模塊(1) – > 多次dump問題

8.數組數list的相互轉換

np.array(a)
c.tolist()
#a爲python的list類型
a=([3.234,34,3.777,6.33])

# list 用len(a)查看長度
print(len(a))

#將a轉化爲numpy的array:  
print (np.array(a))

# c 是array
c = np.array([  3.234,  34.   ,   3.777,   6.33 ])


#將a轉化爲python的list
print (len(c.tolist()))

9.python中list和tuple的用法及區別

list是一種有序的集合,可以隨時添加和刪除其中的元素
list裏面的元素類型可以不同(上面的舉例就可以看出來啦),不僅如此,它的元素還可以是另一個list

tuple是一種有序列表,它和list非常相似,但是(但是前面的話也不都是廢話)
tuple一旦初始化就不能修改,而且沒有append() insert()這些方法,可以獲取元素但不能賦值變成另外的元素

python中list和tuple的用法及區別

10.查看python版本

1.未進入python shell

python --version

2.進入python shell,有兩種方法

(1)

help()

(2)

import sys
sys.version

11.python 3.3.2報錯:No module named ‘urllib2’ 解決方法

python代碼:

import urllib2  
response = urllib2.urlopen('http://www.baidu.com/')  
html = response.read()  
print html  

報錯如下:

Traceback (most recent call last):
  File "<pyshell#0>", line 1, in <module>
    import urllib2
ImportError: No module named 'urllib2' 

解決方法:

在python3.3裏面,用urllib.request代替urllib2,另外python3之後,不能再用,print html
注意:print 的東西要用()括起來。
這樣的方式,因爲print這個時候已經是一個方法了。必須使用下面的方法
可以將代碼換成:

import urllib.request
resp=urllib.request.urlopen('http://www.baidu.com')
html=resp.read()
print(html)

轉自:https://blog.csdn.net/hacker_Lees/article/details/77866338

12.Python可迭代對象中的添加和刪除(add,append,pop,remove,insert)

對於List:

    classmates = ['Michael', 'Bob', 'Tracy']
    classmates.append('Adam')    //添加在末尾,沒有add()方法
    classmates.insert(1, 'Jack') //在指定位置添加
    classmates.pop(1)            //在知道位置刪除,參數是索引
    del classmate[1]             //刪除第二個元素
    classmates.remove('Bob')     //參數是元素,刪除第一個與Bob值匹配的元素,之後又相同元素不會刪除

對於dict:

    d = {'a': 'A', 'b': 'B'}
    del d['a']
    d.pop('a')    //參數是key,沒有remove()方法
    d['c']='C';   //插入直接賦值即可

對於set:

    s={1,2,3}       //set對象的創建也可以是s=set(iterable)
    s.add(8)        //添加8到末尾   沒有append()方法
    s.remove(8)     //參數是元素,不是索引    刪除8   
    s.pop()         //刪除最後一個元素

對於tuple:

由於tuple一旦初始化就不能修改,所以不能插入和刪除

原文:https://blog.csdn.net/handsome_gay/article/details/52740055

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章