0%

Tuple is like list, but we can;t modify it, they are immutable. Tuple immutable 直,就是他的直不無法修改的,有些語法是用const類似這概念。

Create Tuple 建立tuple

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Empty tuple
my_tuple = ()
print(my_tuple) #()

# Tuple having integers
my_tuple = (1, 2, 3)
print(my_tuple) #(1,2,3)

# tuple with mixed datatypes
my_tuple = (1, "Hello", 3.4)
print(my_tuple) #(1, "Hello", 3.4)

# multiply value
x,y,z, *other = (1,2,3,4,5)
print(f"x: {x}, y:{y}, z:{z}, other:{other}")
#x: 1, y:2, z:3, other:[4, 5]

my_tuple = ("mouse", [8, 4, 6], (1, 2, 3))
print(my_tuple) #('mouse', [8, 4, 6], (1, 2, 3))

created without using parentheses

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
my_tuple = 3, 4.6, "dog"
print(my_tuple)

# tuple unpacking is also possible
a, b, c = my_tuple

print(a) # 3
print(b) # 4.6
print(c) # dog

my_tuple = ("hello")
print(type(my_tuple)) # <class 'str'>

# Creating a tuple having one element
my_tuple = ("hello",)
print(type(my_tuple)) # <class 'tuple'>

# Parentheses is optional
my_tuple = "hello",
print(type(my_tuple)) # <class 'tuple'>

Access Tuple Elements

1
2
3
4
5
6
7
8
9
10
11
12
# Accessing tuple elements using indexing
my_tuple = ('p','e','r','m','i','t')

print(my_tuple[0]) # 'p'
print(my_tuple[5]) # 't'

# nested tuple
n_tuple = ("mouse", [8, 4, 6], (1, 2, 3))

# nested index
print(n_tuple[0][3]) #'s'
print(n_tuple[1][1]) # 4

# Negative indexing for accessing tuple elements

1
2
3
4
5
6
7
8
my_tuple = ('p', 'e', 'r', 'm', 'i', 't')

# Output: 't'
print(my_tuple[-1])
# elements beginning to 2nd
# Output: ('p', 'r')
print(my_tuple[:-7])

Slicing

Read more »

python 有很多不同 function,因此我想在這篇寫所有 funtion 我會用到。我指的 function 不是傳 function,而是一些 libary function

Math

Datetime 時間

time

程式跑的時間

如果計算程式袍多就可以用這方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import time

# starting time
start = time.time()
# your code
for i in range(1,60):
print(i)

#endtime
end = time.time()
runtime=end - start
print(" Runtime of the program is {runtime:%.8f}")
print(f"Runtime of the program is {runtime:.5f}")
#or
print(f"Runtime of the program is {(end - start):.5f}")

datetime

今天日期

  • date.today 取今天日期
Read more »

Pandas 筆記

今天我想分享我學的 pandas 筆記,我覺得對於分大數據很有幫助,他有點像 mysql。請先下載 module 或 package:pip install pandas`

Part1 基本語法

連線 CSV 檔案

可以去 github 找相關 csv 檔案,然後去 raw 檔案做連線

1
2
3
4
5
import pandas as pd
url = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/tips.csv"
tips = pd.read_csv(url)

print(tips)

pandas 會回傳 DataFrame 格式。<class 'pandas.core.frame.DataFrame'>
下面是印出所有資料表內容。

1
2
3
4
5
6
7
8
9
10
11
12
     total_bill   tip     sex smoker   day    time  size
0 16.99 1.01 Female No Sun Dinner 2
1 10.34 1.66 Male No Sun Dinner 3
2 21.01 3.50 Male No Sun Dinner 3
3 23.68 3.31 Male No Sun Dinner 2
4 24.59 3.61 Female No Sun Dinner 4
.. ... ... ... ... ... ... ...
239 29.03 5.92 Male No Sat Dinner 3
240 27.18 2.00 Female Yes Sat Dinner 2
241 22.67 2.00 Male Yes Sat Dinner 2
242 17.82 1.75 Male No Sat Dinner 2
243 18.78 3.00 Female No Thur Dinner 2

Head and Tail

  • head(N): 是查前面 N 筆資料
Read more »

Create empty Excel File

1
2
3
4
import openpyxl
fn = 'new_excel.xlsx'
wb = openpyxl.Workbook()
wb.save(fn)

Create Sheet工作表的

Create Empty Sheet 新增工作

1
2
3
4
5
6
import openpyxl
fn = 'new_excel.xlsx'
wb = openpyxl.Workbook()
wb.create_sheet("Sheet1", 0)
wb.create_sheet("Sheet2", 1)
wb.save(fn)

Read Excel Sheet 讀取excel檔案每個工作表的名稱

1
2
3
4
5
6
import openpyxl
fn = 'new_excel.xlsx'
wb = openpyxl.load_workbook(fn)
print(wb.sheetnames)
print(wb.active)
print(wb.active.title)

output:

1
2
3
['Sheet1', 'Sheet2', 'Sheet']
<Worksheet "Sheet1">
Sheet1

edit Name of worksheet 修改工作表名稱

Read more »

list 列印

這裡會包含以下內內容:

  • 基本讀 list 搭配 for
  • *list (快速讀 list 不需要用到 for loop)
  • N 筆後換
  • 轉換 list 到字串(搭配 join)
  • enumerate 方法
  • Method:
    • split
    • slice
    • copy
    • zip
    • 轉換 list 理的 tuple 到 list

常用的方式印 list 內容

list : list = ['A','B','C','D','E','F']

我們可以用以下方法:

- for loop

1
2
3
4
list= [1,2,3]
for i in list:
print(i, end=" ") #
print()

output:

1 2 3

- *list: 不需要 for loop ,可以 unpack list

Read more »

今天分享如何搜尋字串裡面的 substring 或如果有檔案(log)想找相關字來做分析。

split() 方式

我的 string 是:

st="[20221013.162853.788442][info]:[>>> DL- ingress traffic: 0.010799(Mbps), egress traffic: 0.087016(Mbps), ReTx: 0.000220(Mbps)]"

我想抓出時間TPUT的直,如:
[20221013.162853.788442]0.010799(Mbps) 同時要把 [](Mbps)移除,可以用下面方式:

  • Step1: 宣告我的 string
1
st="[20221013.162853.788442][info]:[>>> DL- ingress traffic: 0.010799(Mbps), egress traffic: 0.087016(Mbps), ReTx: 0.000220(Mbps)]"
  • Step2 抓取時間直
1
2
dateStr = st.split('[', 1)[1].split(']')[0]
print(dateStr) #20221013.162853.788442

output:

20221013.162853.788442

Read more »

Open Read Write File

open syntax: 

open( file, mode, encoding="utf-8")

mode 字串 說明
r Read only 讀取模式 ( 預設 )
r+ Read and write
w Write only 寫入模式,檔案若存在,會清空內容再寫入; 若檔案不存在,則建立新檔開啟寫入
w+ write+read
a Append only 附加模式,若檔案存在,則寫入內容會附加至檔 案尾端
a+ Append and Read (‘a+’)

Read

讀取方法 描述
read( ) 一次讀取檔案所有的內容,回傳為字串
readline( ) 只讀取一行內容
readlines( ) 將所有檔案內容,每行讀入回傳為串列
1
2
3
fileObj = open("路徑檔名" , "r", encoding="utf-8") 
content = fileObj.read( )
fileObj.close( )

Write

1
2
3
fileObj = open("路徑檔名" , "w", encoding="utf-8")
fileObj.write(輸出資料)
fileObj.close( )

Example

Ex1. read file and print

Read more »

Interview Code Test

I am sharing some Code Test during my Interview, and also some practice Code example.
今天分享我一些Live Coding 面試問題,還有一些刷題的問題。

Amazon Live Code

Q1. read file into datastucture and search like logfile

If we have a log.txt, as below, you need to and search specfic string, like cat.

  • log.txt
    1
    2
    3
    1 cat
    2 lion
    3 apple

Answer 1: list

1
2
3
4
5
6
7
8
with open("log.txt", "r") as textfile:
lines = textfile.readlines()
for i in lines:
sli = i.split(' ')
#print(sli)
if "cat" in sli:
print(sli[0], sli[-1])

Answer2: using dictionary

1
2
3
4
5
6
7
8
9
10
11
12
d={}
with open("log.txt", "r") as textfile:
#lines = textfile.readlines()
#skip \n
lines = textfile.read().splitlines()
#print(lines)
for i in lines:
#sli = i.split(' ')
(key,val)=i.split(" ")
d[int(key)]=val
# print(key,"",val)
print(d)
Read more »

Advance JS

構造函數constructor Function

1
2
3
4
5
6
7
8
9
10
11
12
13
function createCard(initName){
this.name=initName

}

const a1 = new createCard("Ma")
const a2 = new createCard("Ma2")
const a3 = new createCard("Ma3")
const a4 = new createCard("Ma4")
const a5 = new createCard("Ma5")
console.log(a1)
console.log(a2)
console.log(a3)

output

createCard { name: ‘Ma’ }
createCard { name: ‘Ma2’ }
createCard { name: ‘Ma3’ }

array function =>

In ES6 there is a new function called array function, which you can write shorter function.

Before ES6

function hello(){}

after ES6

let hello = function (){}
shorthand
let hello = () =>{}

function without passing paramter

Before ES6 (normal function)

ex: function hello(){}

Read more »

今天我想分享如何用linux shell script 語法。
我們需要加這個在script裡面,然後檔案改成.sh

1
#! /bin/bash
  • How to execute script

    chmod 777 myscript.sh
    ./myscript.sh

1. variable

define variable

1
2
3
text1=Hello
name=CC
echo $text1 $name

output:

Hello CC

read and echo variable

read variable

read a

read vairable without skip new line:

read -p “Your Options: “ option

Read more »