1. 程式人生 > >python學習筆記第二天

python學習筆記第二天

adl 基本 dig close 是什麽 recent per names 常用

  • 列表、元組操作
  • 字符串操作
  • 字典操作
  • 集合操作
  • 文件操作
  • 作業

(ps:昨天寫的竟然沒保存。。。。想哭哭,所以今天寫的內容的代碼演示部分為了節約時間就直接復制別人的了)


一、列表、元組操作

1.1、列表操作

列表初始化:

names = [‘Alex‘,"Tenglan",‘Eric‘]

列表的切片:(左閉右開,前小後大)

>>> names = ["Alex","Tenglan","Eric","Rain","Tom","Amy"]
>>> names[1:4]  #取下標1至下標4之間的數字,包括1,不包括4
[‘Tenglan‘, ‘Eric‘, ‘Rain‘]
>>> names[1:-1] #取下標1至-1的值,不包括-1
[‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘]
>>> names[0:3] 
[‘Alex‘, ‘Tenglan‘, ‘Eric‘]
>>> names[:3] #如果是從頭開始取,0可以忽略,跟上句效果一樣
[‘Alex‘, ‘Tenglan‘, ‘Eric‘]
>>> names[3:] #如果想取最後一個,必須不能寫-1,只能這麽寫
[‘Rain‘, ‘Tom‘, ‘Amy‘] 
>>> names[3:-1] #這樣-1就不會被包含了
[‘Rain‘, ‘Tom‘]
>>> names[0::2] #後面的2是代表,每隔一個元素,就取一個
[‘Alex‘, ‘Eric‘, ‘Tom‘] 
>>> names[::2] #和上句效果一樣
[‘Alex‘, ‘Eric‘, ‘Tom‘]

列表的增(追加+插入):

>>> names
[‘Alex‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘]
>>> names.append("我是新來的")
>>> names
[‘Alex‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, ‘我是新來的‘]

>>> names
[‘Alex‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, ‘我是新來的‘]
>>> names.insert(2,"強行從Eric前面插入")
>>> names
[‘Alex‘, ‘Tenglan‘, ‘強行從Eric前面插入‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, ‘我是新來的‘]

列表的刪(pop,del,remove):

>>> del names[2] 
>>> names
[‘Alex‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘從eric後面插入試試新姿勢‘, ‘Tom‘, ‘Amy‘, ‘我是新來的‘]
>>> del names[4]
>>> names
[‘Alex‘, ‘Tenglan‘, ‘Eric‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, ‘我是新來的‘]
>>> 
>>> names.remove("Eric") #刪除指定元素
>>> names
[‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, ‘我是新來的‘]
>>> names.pop() #刪除列表最後一個值 
‘我是新來的‘
>>> names
[‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘]

列表的改:

>>> names
[‘Alex‘, ‘Tenglan‘, ‘強行從Eric前面插入‘, ‘Eric‘, ‘Rain‘, ‘從eric後面插入試試新姿勢‘, ‘Tom‘, ‘Amy‘, ‘我是新來的‘]
>>> names[2] = "該換人了"
>>> names
[‘Alex‘, ‘Tenglan‘, ‘該換人了‘, ‘Eric‘, ‘Rain‘, ‘從eric後面插入試試新姿勢‘, ‘Tom‘, ‘Amy‘, ‘我是新來的‘]

列表的查:(for in..,index)

>>> names
[‘Tom‘, ‘Tenglan‘, ‘Amy‘, ‘Amy‘, ‘Alex‘, ‘3‘, ‘2‘, ‘1‘]
>>> names.index("Amy")
2 #只返回找到的第一個下標

列表的擴展:

>>> names
[‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘]
>>> b = [1,2,3]
>>> names.extend(b)
>>> names
[‘Alex‘, ‘Tenglan‘, ‘Rain‘, ‘Tom‘, ‘Amy‘, 1, 2, 3]

列表的統計:

>>> names
[‘Alex‘, ‘Tenglan‘, ‘Amy‘, ‘Tom‘, ‘Amy‘, 1, 2, 3]
>>> names.count("Amy")
2

列表的copy(淺復制+深復制):

暫無

1.2、元組操作

元組和列表差不多,但是不允許被修改,用()表示

names = ("alex","jack","eric")

元組不可修改,所以只有count和index兩種方法

二、字符串操作

註意:無論是什麽方法,都不會修改字符串本身的

字符串的常用操作:

name.capitalize()  首字母大寫
name.casefold()   大寫全部變小寫
name.center(50,"-")  輸出 ‘---------------------Alex Li----------------------‘
name.count(‘lex‘) 統計 lex出現次數
name.encode()  將字符串編碼成bytes格式
name.endswith("Li")  判斷字符串是否以 Li結尾
 "Alex\tLi".expandtabs(10) 輸出‘Alex      Li‘, 將\t轉換成多長的空格 
 name.find(‘A‘)  查找A,找到返回其索引, 找不到返回-1 

format :
    >>> msg = "my name is {}, and age is {}"
    >>> msg.format("alex",22)
    ‘my name is alex, and age is 22‘
    >>> msg = "my name is {1}, and age is {0}"
    >>> msg.format("alex",22)
    ‘my name is 22, and age is alex‘
    >>> msg = "my name is {name}, and age is {age}"
    >>> msg.format(age=22,name="ale")
    ‘my name is ale, and age is 22‘
format_map
    >>> msg.format_map({‘name‘:‘alex‘,‘age‘:22})
    ‘my name is alex, and age is 22‘


msg.index(‘a‘)  返回a所在字符串的索引
‘9aA‘.isalnum()   True

‘9‘.isdigit() 是否整數
name.isnumeric  
name.isprintable
name.isspace
name.istitle
name.isupper
 "|".join([‘alex‘,‘jack‘,‘rain‘])
‘alex|jack|rain‘

  

三、字典操作

字典的定義:

info = {
    ‘stu1101‘: "TengLan Wu",
    ‘stu1102‘: "LongZe Luola",
    ‘stu1103‘: "XiaoZe Maliya",
}

字典的特點:

  • 無序
  • key必須唯一

字典的增:

>>> info["stu1104"] = "蒼井空"
>>> info
{‘stu1102‘: ‘LongZe Luola‘, ‘stu1104‘: ‘蒼井空‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1101‘: ‘TengLan Wu‘}

字典的刪:pop,del

>>> info
{‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1101‘: ‘武藤蘭‘}
>>> info.pop("stu1101") #標準刪除姿勢
‘武藤蘭‘
>>> info
{‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘}
>>> del info[‘stu1103‘] #換個姿勢刪除
>>> info
{‘stu1102‘: ‘LongZe Luola‘}

字典的改:

>>> info[‘stu1101‘] = "武藤蘭"
>>> info
{‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘, ‘stu1101‘: ‘武藤蘭‘}

字典的查:

>>> info = {‘stu1102‘: ‘LongZe Luola‘, ‘stu1103‘: ‘XiaoZe Maliya‘}
>>> 
>>> "stu1102" in info #標準用法
True
>>> info.get("stu1102")  #獲取
‘LongZe Luola‘
>>> info["stu1102"] #同上,但是看下面
‘LongZe Luola‘
>>> info["stu1105"]  #如果一個key不存在,就報錯,get不會,不存在只返回None
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
KeyError: ‘stu1105‘

字典的循環:

#方法1
for key in info:
    print(key,info[key])

#方法2
for k,v in info.items(): #會先把dict轉成list,數據裏大時莫用
    print(k,v)

  

四、集合操作

集合set,無序的,不重復的

常見操作:

s = set([3,5,9,10])      #創建一個數值集合  
  
t = set("Hello")         #創建一個唯一字符的集合  


a = t | s          # t 和 s的並集  
  
b = t & s          # t 和 s的交集  
  
c = t – s          # 求差集(項在t中,但不在s中)  
  
d = t ^ s          # 對稱差集(項在t或s中,但不會同時出現在二者中)  
  
   
  
基本操作:  
  
t.add(‘x‘)            # 添加一項  
  
s.update([10,37,42])  # 在s中添加多項  
  
   
  
使用remove()可以刪除一項:  
  
t.remove(‘H‘)  
  
  
len(s)  
set 的長度  
  
x in s  
測試 x 是否是 s 的成員  
  
x not in s  
測試 x 是否不是 s 的成員  
  
s.issubset(t)  
s <= t  
測試是否 s 中的每一個元素都在 t 中  
  
s.issuperset(t)  
s >= t  
測試是否 t 中的每一個元素都在 s 中  
  
s.union(t)  
s | t  
返回一個新的 set 包含 s 和 t 中的每一個元素  
  
s.intersection(t)  
s & t  
返回一個新的 set 包含 s 和 t 中的公共元素  
  
s.difference(t)  
s - t  
返回一個新的 set 包含 s 中有但是 t 中沒有的元素  
  
s.symmetric_difference(t)  
s ^ t  
返回一個新的 set 包含 s 和 t 中不重復的元素  
  
s.copy()  
返回 set “s”的一個淺復制

  

五、文件操作

文件的基本操作流程:

  • 打開文件,得到文件句柄並賦值給一個變量
  • 通過對文件句柄進行操作
  • 關閉文件

基本操作:

f = open(‘lyrics‘) #打開文件
first_line = f.readline()
print(‘first line:‘,first_line) #讀一行
print(‘我是分隔線‘.center(50,‘-‘))
data = f.read()# 讀取剩下的所有內容,文件大時不要用
print(data) #打印文件
 
f.close() #關閉文件

 readline()是讀取一行,read()是讀取所有

文件的打開模式:

  • r,只讀模式
  • w,只寫模式(不可讀!!!!!註意這種模式下,如果本來已經有同名的文件,會新創建一個文件覆蓋原有文件,導致原有文件清空)
  • a,追加模式(不可讀!!和w相比,就是會直接在原有的同名文件裏面追加內容)
  • r+(可讀寫)
  • w+(可寫讀,但是先打開文件的時候仍然存在上述問題)
  • a+(a)

六、作業

  1. 三級菜單的實現
  2. 購物車的優化
  3. 實現簡單的shell sed替換功能
  4. 修改haproxy配置文件

python學習筆記第二天