python_note
  • Introduction
  • My Python
    • Anaconda
    • argparse
    • datetime
    • json
    • logging
    • numpy
    • open
    • openCC
    • pandas & csv
    • Socket & SocketServer
    • re
    • yaml
    • smtp
    • 物件操作
    • unittest
    • 線程
    • prettytable
    • IO
    • pycurl
    • sys
    • pickle
    • auto-python-to-exe
    • cython
    • nuitka
  • Crawler
    • Urllib & Requests
      • User-agent
      • Percent-Encoding
      • mail code
    • Selenium
    • TCP & UDP
    • 控制字符(control character)
  • Web Development
    • Flask
      • RESTful api
      • Template
      • blueprint
    • Django
      • 環境佈署(windows)
    • 檢查Port
    • Apache
    • 使用者行為
    • jQuery
    • 壓力測試
    • DataTable
    • Bootstrap
    • CSS
    • JavaScript
    • Chart.js
  • Deep Learning
    • Keras 設定
    • RNN
    • LSTM
  • Test
    • T-Test
  • 資料結構
    • Hash
    • 時間複雜度
  • NLP
    • N-gram
    • CKIP
    • 中文轉數字
    • CRF
    • Mutual Information
    • 模糊比對
  • Linebot
    • Heroku
    • 圖文選單
    • channel
  • Linux
    • 常用指令
    • shell script
    • sshfs
    • ssh
    • nodejs & npm
    • debug
  • GCP
    • app engine
    • ssh(gcp)
    • gsutil
    • brabrabra
    • Load Balancer
    • k8s
  • Database
    • mysql
    • elasticsearch
      • Query
      • Backup and Restore
      • elasticdump
      • es2csv
      • ELK
    • mongodb
      • install
      • authentication
      • pymongo
    • sql server
  • go
    • Swarm
  • Docker
    • Kitematic
    • Dockerfile
    • Swarm
  • Git
  • 其他
    • USB軟體保護
    • Windows效能監視器
  • Blockchain
Powered by GitBook
On this page
  • List
  • Dict
  • is vs ==
  • Class
  • Copy
  • sort

Was this helpful?

  1. My Python

物件操作

List

#Insert 
aList = [123, 'xyz', 'zara', 'abc']
aList.insert( 3, 2009)
print "Final List : ", aList

#Reverse
aList.reverse()

#去除重複
newList = list(set(aList))

# 刪除
li = [1,2,3,4,5,6]
# 删除對應索引的元素
del li[2]
# li = [1,2,4,5,6]

# 删除最后一个元素
li.pop()
# li = [1,2,4,5]

# 刪除list中指定位址的內容並指定到新的變數中
a = li.pop(0)
# li = [2,4,5]

# 删除指定值的元素
li.remove(4)
# li = [2,5]

排序

x = [1,5,2,3,4]
x.reverse()  # 大到小  
x.sort()  # 小到大
a = sorted(x)  # 保留原始,並產生小到大

Dict

遍歷

列出所字典內容,.keys()、.values()、dict.keys()、.items()

# python
x = {1:1, 0:0, 2:2}

# 列出所有key
print(x.keys())

# 列出所有key的list
print(list(x.keys))

# 同時提取key與value
for key, value in x.items():
    print(key, value)

刪除

指定key並刪除

# python3
x = {1:1, 0:0, 2:2}
x.pop(1)
print(x)
# {0: 0, 2: 2}

y = {1:1, 0:0, 2:2}
del y[1]
print(y)
# {0: 0, 2: 2}

# 兩種方式皆可用

# 更新key  (0 ==> 1)
y[1] = y.pop(0)
# {2: 2, 1: 0}

排序

result = sorted(dict1.items(), key=lambda d: d[1])  # 回傳list,裡面是turple

判斷

判斷型態

if isinstance(number, int):   
    print('ok')

判斷數字

raw = "345"
raw.isnumeric()  # true/false

is vs ==

is是相同物件
==是相同值

Class

Copy

# python3
import copy

a = {'a': 1}

b = copy.deepcopy(a)  # 複製新的物件

sort

string insert方式

 s[:4] + '-' + s[4:]
PrevioussmtpNextunittest

Last updated 5 years ago

Was this helpful?

參考連結:

判斷a list內容是否都出現在b list中:

參考連結:

參考資料:

判斷數字

https://www.tutorialspoint.com/python/list_insert.htm
https://www.douban.com/note/277146395/
http://thispointer.com/python-check-if-a-list-contains-all-the-elements-of-another-list/
http://www.iplaypy.com/jinjie/jj114.html
https://blog.csdn.net/mmc2015/article/details/50777349
https://segmentfault.com/a/1190000004959880
http://www.runoob.com/python/att-string-isdigit.html
http://www.runoob.com/python3/python3-string-isnumeric.html
https://www.itread01.com/article/1533266784.html
https://blog.mimvp.com/article/25955.html
http://www.iplaypy.com/jinjie/is.html
http://blog.blackwhite.tw/2013/05/python-is.html
https://marco79423.net/articles/淺談-python-的屬性/
https://medium.com/@weilihmen/關於python的類別-class-基本篇-5468812c58f2
http://www.runoob.com/w3cnote/python-understanding-dict-copy-shallow-or-deep.html
http://blog.51cto.com/wangwei007/1100742
https://stackoverflow.com/questions/5254445/add-string-in-a-certain-position-in-python