python基礎之數據類型語句

讀教程筆記http://cs231n.github.io/python-numpy-tutorial/

關於python基礎知識:

1.string:

#~len(str):數組長度;

#~sprint style string formats:

hello="hello"
world='world'
str2="%s %s %d" %(hello,world,12)
print str2  #輸出hello world 12
#~string可調用函數
s="hello"
print s.capitalize() #首字母大寫
print s.upper()#將全部小寫轉化爲大寫
print s.rjust(9)  #右對齊字符串,左邊使用空格填充
print s.center(9) #給定字符串總數,string居中,其餘用空格填充
print s.replace('l',"(ell)") #使用右邊參數替代前面參數
輸出結果爲:
Hello
HELLO
    hello
  hello  
he(ell)(ell)o
2.containers ;

#list

x=[3,2,"foo"] #create list
lastx=x.pop() #pop函數返回並刪除list的最後一個元素
print lastx
print x
x.append("bar")  #添加元素
print x
輸出結果爲:

foo
[3, 2]
[3, 2, 'bar']

#添加元素可以使用append和extend函數,但是兩者用法不同:

列表是以類的形式實現的。“創建”列表實際上是將一個類實例化。因此,列表有多種方法可以操作。
1.列表可包含任何數據類型的元素,單個列表中的元素無須全爲同一類型。
2.append() 方法向列表的尾部添加一個新的元素。只接受一個參數。
3.extend()方法只接受一個列表作爲參數,並將該參數的每個元素都添加到原有的列表中。

**********************************************************************************************************************

append()用法示例:

>>> mylist = [1,2,0,'abc']

>>> mylist

[1, 2, 0, 'abc']

>>> mylist.append(4)

>>> mylist

[1, 2, 0, 'abc', 4]

>>> mylist.append('haha')

>>> mylist

[1, 2, 0, 'abc', 4, 'haha']

>>>

**********************************************************************************************************************

extend()用法示例:

>>> mylist

[1, 2, 0, 'abc', 4, 'haha']

>>> mylist.extend(['lulu'])

>>> mylist

[1, 2, 0, 'abc', 4, 'haha', 'lulu']

>>> mylist.extend(['123123','lalalala'])

>>> mylist

[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala']

>>> mylist.extend([111111,222])

>>> mylist

[1, 2, 0, 'abc', 4, 'haha', 'lulu', '123123', 'lalalala', 111111, 222]


animals=['cat','dog','monky']  #帶索引序號的list遍歷函數enumerate
for idx,animal in enumerate(animals):
    print '索引%d %s' %(idx+1,animal)
輸出結果爲:

索引1 cat
索引2 dog
索引3 monky

nums = [0, 1, 2, 3, 4]
even_squares = [x ** 2 for x in nums if x % 2 == 0]
print even_squares  #輸出[0, 4, 16]
#dictionary
<pre name="code" class="python">d = {'cat': 'cute', 'dog': 'furry'}
print d.get('monkey', 'N/A')  # Get an element with a default; prints "N/A"
print d.get('fish', 'N/A')    # Get an element with a default; prints "wet"
</pre>d={'person':2,'cat':4,'spider':8}for animal in d:    print '%s has %d legs' %(animal,d[animal])<p></p><pre>
</pre><pre name="code" class="python"><pre name="code" class="python">輸出結果爲:
person has 2 legs
spider has 8 legs
cat has 4 legs
#如果想要遍歷字典裏面的索引和索引對應的值,使用iteritems函數
for animal,legs in d.iteritems():
    print '%s has %d legs' %(animal,legs)

#將list生成dictionary
nums=[0,1,2,3,4]
even_num={x: x**x for x in nums if x%2==0}
#set

<pre name="code" class="python">animals = {'cat', 'dog'}
print 'cat' in animals   # Check if an element is in a set; prints "True"

print len(animals)       # Number of elements in a set; prints "3"
animals.add('cat')       # Adding an element that is already in the set does nothing
print len(animals)       # Prints "3"
animals.remove('cat')    # Remove an element from a set

from math import sqrt
nums = {int(sqrt(x)) for x in range(30)}
print nums  # Prints "set([0, 1, 2, 3, 4, 5])"

d = {(x, x + 1): x for x in range(10)}  # Create a dictionary with tuple keys
t = (5, 6)       # Create a tuple
print type(t)    # Prints "<type 'tuple'>"
print d[t]       # Prints "5"
print d[(1, 2)]  # Prints "1"


Python常用函數:

1.關於隨機函數

times = np.arange(0, duration_s, time_step_s)    # start, end, step
與matlab的參數順序不同,這個函數允許輸入爲float類型的。


vt = np.random.rand(np.shape(times)[0])
這個函數是可以生成某種大小的0到1之間的隨機數組(矩陣),參數可以是數組的大小。

np.random.random((2, 3))
array([[ 0.58941311,  0.00996924,  0.30159729],
       [ 0.64386549,  0.94079774,  0.92460379]])

生成0到1之間的大小爲2*3的矩陣


np.random.randint(5, size=(2, 4))
array([[4, 0, 2, 1],
       [3, 2, 2, 0]])
Generate a 2 x 4 array of ints between 0 and 4

我也是傻了,浮點數的話可以先生成隨機數然後再乘以最大值即可。

比如要生成0-200之間的隨機數,先生成0-1之間的隨機數然後再乘以200即可。


2. 關於符號函數

spikes[i, :] = np.signbit(vt - (spikes_pers * time_step_s))
這個函數是將數組中的負數置爲false, 其他置爲true。

而sign函數卻是負數爲-1, 0 爲0 , 正數爲1.

此外,上面一行代碼中括號中的最後一項乘項是兩個實數項相乘,vt是向量,雖然numpy的array可以實現“向量-實數=向量”,但是python的list不行,list必須採取上面的signbit函數。當操作對象爲numpy的array時,上面一行代碼與下面的一行代碼是等價的。

spikes[i, :] = (spikes_pers * time_step_s) > vt



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