python基礎知識

一.隨機數的生成(都需要 import random )
1.用於生成一個指定範圍內的隨機浮點數。

print random.uniform(10,20)
print random.uniform(20,10)

2.生成一個指定範圍內的整數。(下限必須小於上限制)

 print random.randint(12,20)
 print random.randint(20,20)

3.隨機選取0到100之間的偶數

print random.randrange(0,99)

4.隨機浮點數

 print random.random()
 print random.uniform(1,10)

5.隨機字符串

 print random.choice('abcdefg&#%^*f')

6.多個字符串中選取特定的字符串

print random.sample('abcdefghij',3) 
['a', 'd', 'b']

7.洗牌

 items = [1, 2, 3, 4, 5, 6]
 random.shuffle(items)
 items
[3, 2, 5, 6, 4, 1]

Numpy是python的一個科學計算庫,提供了矩陣運算的功能,其一般與scipy, matplotlib一起使用。

8.數組的合併

 import numpy as np
 a = np.ones((2,2))
 b = np.eye(2)
 print np.vstack((a,b));
 [[ 1.  1.]
 [ 1.  1.]
 [ 1.  0.]
 [ 0.  1.]]
print np.hstack((a,b))
[[ 1.  1.  1.  0.]
 [ 1.  1.  0.  1.]]

.待續。。。。。。。

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