發表文章

目前顯示的是有「Python」標籤的文章

python virtualenv basic command: 一些基本的python virtuel environment的指令。

圖片
install virtualenv in ubuntu 18.04 $ wget https://bootstrap.pypa.io/get-pip.py $ sudo python3 get-pip.py $ sudo pip install virtualenv virtualenvwrapper $ sudo rm -rf ~/get-pip.py ~/.cache/pip  python   virtualenv basic command:  workon : list all your virtual environments. mkvirtualenv : create a new environment e.g. > mkvirtualenv mlpy37env -p python3.7 rmvirtualenv : remove a exists environment. deactivate : akes you to your system environment. You can activate any of your virtual environments again at any time。

【Python】Python - 聯合Sets。Python Join Sets.

圖片
 【Python】Python - 聯合Sets。Python Join Sets. 聯合2個Sets 在python 中有很多種方法可以聯合2個或多個sets。 你可以使用 union()  方法去回傳新的set包含2個set內所有的items,或使用 update()  方法來插入所有來自1個set的items到另一個: 範例 使用  union()  方法回傳一個新的set,且其items來自2個sets: set1 = { "a" ,  "b"  ,  "c" } set2 = { 1 ,  2 ,  3 } set3 = set1.union(set2) print (set3) 上面回傳: {'b', 1, 2, 'a', 3, 'c'} 範例 使用  update()  方法去新增set1的items,而資料來自set2: set1 = { "a" ,  "b"  ,  "c" } set2 = { 1 ,  2 ,  3 } set1.update(set2) print (set1) 上面輸出: {'c', 2, 'a', 1, 3, 'b'} Note:  下面2個  union()  與  update()  h都會忽略重複值。 二者都有的items才保存 使用  intersection_update()  方法將會僅保存二個sets都存在的items。 範例 如果存在set x與set y的items才保存: x = { "apple" ,  "banana" ,  "cherry" } y = { "google" ,  "microsoft" ,  "apple" } x.intersection_update(y) print (x) 上面輸出: {'apple'} The  intersection()  方法將會回傳新的set,且這個set裡面的items僅二者都有的才會有。  範例 ...

【Python】Python - 迴圈Set。Python Loop Set.

圖片
 【Python】Python - 迴圈Set。Python Loop Set. 使用迴圈存取 Items 你可以使用 for  迴圈去對 set裡面的item存取: 範例 下面的範例為對set做迴圈,並列印值: Loop through the set, and print the values: thisset = { "apple" ,  "banana" ,  "cherry" } for  x  in  thisset:    print (x) 上面輸出:  cherry banana apple

【Python】Python - 刪除Set items。Python Remove Set Items.

圖片
【Python】Python - 刪除Set items。Python Remove Set Items. 要移除set裡面的items, 使用remove()方法,或discard()方法。 範例 使用remove()方法移除"banana": thisset = { "apple" ,  "banana" ,  "cherry" } thisset.remove( "banana" ) print (thisset) 上面輸出: {'apple', 'cherry'} Note:  如果要刪除的item不存在set中,則remove()會出現錯誤。 範例 使用discard()方法移除"banana": thisset = { "apple" ,  "banana" ,  "cherry" } thisset.discard( "banana" ) print (thisset) 上面輸出: {'apple', 'cherry'} Note: 如果要刪除的item不存在set中,則 discard()   不會 出現錯誤。所以使用上要注意,這個不會出錯。 你也可使用 pop()  方法去移除item,但這個會僅移除最後一個item。記住sets是沒有順序的,所以無法知道哪一個item被刪除了。但 pop()  會回傳被刪除的item 的值。 範例 使用 pop()  方法移除set中的最後一個item: thisset = { "apple" ,  "banana" ,  "cherry" } x =   thisset.pop() print (x) print (thisset) 上面輸出:  banana {'cherry', 'apple'} Note:  Sets 是沒有順序的,所以當使用 pop()  方法是無法知道移除了哪個item。 範例 使用  clear()  方法清空整個set: thiss...

【Python】Python - 新增Set items。Python Add Set Items.

圖片
 【Python】Python - 新增Set items。Python Add Set Items. 新增 Items 一旦建立了set,則無法修改這個set的items,但可以新增。 可以使用 add()  方法去新增items到set裡面。 範例 使用 add()  方法去新增items到set裡面 : thisset = { "apple" ,  "banana" ,  "cherry" } thisset.add( "orange" ) print (thisset) 範例: {'orange', 'apple', 'cherry', 'banana'} 從其他sets新增 Sets的items 從其他set的items新增到另一個set可以使用 update()  方法。 範例 增加元素 從  tropical  與  thisset  到  newset : thisset = { "apple" ,  "banana" ,  "cherry" } tropical = { "pineapple" ,  "mango" ,  "papaya" } thisset.update(tropical) print (thisset) 上面輸出: {'apple', 'mango', 'cherry', 'pineapple', 'banana', 'papaya'} 新增任何的迭代 在 update()  方法裡面的物件,不一定需要是set,可以是任何的可迭代物件,如tuples, lists, dictionaries...等。 範例 從list的元素增加代set裡面: thisset = { "apple" ,  "banana" ,  "cherry" } mylist = [ "kiwi" ,  "orang...

【Python】Python - 存取Set items。Python Access Set Items.

圖片
 【Python】Python - 存取Set items。Python Access Set Items. 存取 Items 你無法使用index或key去存取set裡面的items。 但可以使用 for  迴圈整個讀取,或使用特定的值去用 in  keyword。 範例 使用迴圈透過set 並列值: thisset = { "apple" ,  "banana" ,  "cherry" } for  x  in  thisset:    print (x) 上面輸出,順序不一定:  apple cherry banana 範例 判斷是否 "banana" 有存在set裡面: thisset = { "apple" ,  "banana" ,  "cherry" } print ( "banana"   in  thisset) 上面輸出: True 修改 Items 一旦set被建立後,你無法改變裡面的items,但你可以使用新增add(),來增加新的items。

【Python】Python - Sets。Python Sets.

圖片
 【Python】Python - Sets。Python Sets. Set Sets 也是Python  中4種 collections資料型態的一種,常作為容器使用,可以儲存多個items到1個變數裡面。其他3種為 List , Tuple ,與Dictionaey,這些容器都有不同的使用方式與情境,對資料的品質也不同。 Set內儲存的資料沒有順序(unordered),也沒有索引值(unindexed)。 Set宣告時使用大括弧{}。 範例 建立一個 Set: thisset = { "apple" ,  "banana" ,  "cherry" } print (thisset) 上面輸出: {'cherry', 'apple', 'banana'} Note:  因為Sets沒有順序,所以存入sets內的資料在取出時無法確定哪個會先被取出。 Set Items Set items 是無順序的,無法改變的,且不會儲存重複的值。 沒有順序的 沒有順序的表示set裡面的items沒有定義的順序。 Set的items的順序每次使用的時候都不一樣,且沒有索引的key值。 不可改變的 Sets是無法改變的,表示當set被建立後我們無法改變裡面的items。 一但set被建立後,你無法改變裡面的items,但你可以新增新的items。 不允許重複值 Sets不可以有2個items是相同的值。 範例 重複值將被忽略: thisset = { "apple" ,  "banana" ,  "cherry" ,  "apple" } print (thisset) 上面輸出:  {'banana', 'cherry', 'apple'} 取得Set的長度 為了知道set內有多少個items可以使用len() 方法。 範例 取得set內items的個數: thisset = { "apple" ,  "banana" ,  "cherry" } print ( len (thisset...

【Python】Python - Tuple的函數。Python Tuple methods.

圖片
【Python】Python - Tuple的函數。Python Tuple methods. Tuple 方法 Python 有2個內建方法讓我們可以用來操作與使用tuples。 方法 描述 count() 回傳某個值出現在tuple的次數。 index() 搜尋tuple內的某個值的索引值。 範例 回傳5出現在tuple內的次數。 Return the number of times the value 5 appears in the tuple: thistuple = ( 1 ,  3 ,  7 ,  8 ,  7 ,  5 ,  4 ,  6 ,  8 ,  5 ) x = thistuple.count( 5 ) print (x) 上面輸出: 2 範例 搜尋並回傳第一個值為8的索引值。 thistuple = ( 1 ,  3 ,  7 ,  8 ,  7 ,  5 ,  4 ,  6 ,  8 ,  5 ) x = thistuple.index( 8 ) print (x) 上面輸出: 3