列表是咱們最之後最經常使用的數據類型之一,經過列表能夠對數據實現最方便的存儲、修改等操做html
定義列表python
students = ["ronghui","jyj","a","b"]linux
students[0]git
'ronghui'shell
切片:取多個元素windows
students = ["ronghui","jyj","a","b"]api
students[1:4]#包括1可是不包括4app
['jyj','a','b']iphone
students[1:-1]#下標1到倒數第二個ide
['jyj','a']
students[0:2] = students[:2]#開頭到第一個,不包括下標2
['ronghui','jyj']
students[0::2] = students[::2]#每隔一個元素取一個
['ronghui','a']
追加
students = ["ronghui","jyj","a","b"]
students.append("jiangyijing")
students = ["ronghui","jyj","a","b","jiangyijing"]
插入
students = ["ronghui","jyj","a","b"]
students.insert(2,"new")#在第二個位置插入new
students = ["ronghui","jyj","new","a","b"]
修改
students = ["ronghui","jyj","a","b"]
students[0]="new"
students = ["new","jyj","a","b"]
刪除
students = ["ronghui","jyj","a","b"]
students.remove("ronghui")
del students[0]
students.pop(obj)#默認刪除最後一個
students.pop(0)
拷貝
students = ['a','b','c',['d','e']]
students2 = students.copy()
print(students) #['a', 'b', 'c', ['d', 'e']]
print(students2) #['a', 'b', 'c', ['d', 'e']]
students[0] = 'A'
print(students) ['A', 'b', 'c', ['d', 'e']]
print(students2) ['a', 'b', 'c', ['d', 'e']]
students[3][0] = 'D'
print(students) ['A', 'b', 'c', ['D', 'e']]
print(students2) ['a', 'b', 'c', ['D', 'e']]
僅僅拷貝第一層 由於第一層['d','e']指向內存中的一個地址
['a', 'b', 'c', ['d', 'e']]
['a', 'b', 'c', ['d', 'e']]
['A', 'b', 'c', ['d', 'e']]
['a', 'b', 'c', ['d', 'e']]
['A', 'b', 'c', ['D', 'e']]
['a', 'b', 'c', ['D', 'e']]
排序&翻轉
sort&reverse
students = ["ronghui","jyj","a","b"]
students.sort()#排序
students.reverse()#倒序
獲取下標
students = ["ronghui","jyj","a","b"]
students.index("ronghui")
元組其實跟列表差很少,也是存一組數,只不是它一旦建立,便不能再修改,因此又叫只讀列表
語法
students = ('a','b','c')
它只有2個方法,一個是count,一個是index,完畢。
請閉眼寫出如下程序。
程序:購物車程序
需求:
shopping_car = []
shopping_list = [['iphone',5000],['book',100],['bike',800]]
for line in shopping_list:
print(line)
flag = True
while flag:
salary = input("your salary:")
if salary.isdigit():
salary = int(salary)
flag = False
print("輸入你要購買商品的編號:")
while True:
choice = input("your number:")
if choice.isdigit():
choice = int(choice)
product = shopping_list[choice]
price = shopping_list[choice][1]
print(price)
if salary >= price:
shopping_car.append(product)
salary -= price
print("your balance %s"%(salary))
elif choice == 'q':
print(shopping_car)
break
else:
print("invalid input ")
else:
print("not number,exit")
name = 'jiangyijing'
name2 = name.capitalize()
name2 = name.swapcase()
name2 = name.center(50,'*')
name2 = len(name)
name2 = name.count('j')
name2 = name.encode()
name2 = name.endswith('g')
name2 = name.expandtabs(10)
name2 = name.find('j')
name2 = name.index('j')
name2 = name.isdigit()
name2 = name.isalpha()
name2 = name.isalnum()
name2 = name.isidentifier()
name2 = name.isupper()
name2 = name.ljust(40,'*')
name2 = name.rjust(40,'*')
name2 = name.strip()
name2 = name.lstrip()
name2 = name.rstrip()
print(name)
print(name2)
students = {
'stu1':'ronghui',
'stu2':'jyj',
'stu3':'a'
}
字典的特性:
students['stu4'] = 'huihui'
students = {
'stu1':'ronghui',
'stu2':'jyj',
'stu3':'a',
'stu4':'huihui'
}
修改
students['stu4'] = 'HHH'
students = {
'stu1':'ronghui',
'stu2':'jyj',
'stu3':'a',
'stu4':'HHH'
}
刪除
students.pop()
del students['stu1']
students.popitem()
查找
"stu1" in students
True
多級菜單
menu = {
"shanghai":{
"baoshan":{
"sitang":{"ronghui":"hardworking"},
"huma":{"zhouwenzhe":"shabi"},
"tonghe":{"zhouwei":"dashabi"}
},
"hongkou":{
"hkxincun":{},
"hkzuqiuchang":{}
},
"pudong":{
"nanhui":{},
"huinan":{}
}
},
"beijing":{
"haidian":{},
"changping":{},
"chaoyang":{}
},
}
其餘
#values
students.value
#keys
循環dict
for key in students:
print(key,students[key])
程序: 三級菜單
要求:
集合是一個無序的,不重複的數據組合,它的主要做用以下:
經常使用操做
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」的一個淺複製
5. 文件操做
f
=
open
(
'lyrics'
)
#打開文件
first_line
=
f.readline()
print
(
'first line:'
,first_line)
#讀一行
print
(
'我是分隔線'
.center(
50
,
'-'
))
data
=
f.read()
# 讀取剩下的全部內容,文件大時不要用
print
(data)
#打印文件
f.close()
#關閉文件
打開文件的模式有:
"+" 表示能夠同時讀寫某個文件
"U"表示在讀取時,能夠將 \r \n \r\n自動轉換成 \n (與 r 或 r+ 模式同使用)
"b"表示處理二進制文件(如:FTP發送上傳ISO鏡像文件,linux可忽略,windows處理二進制文件時需標註)
with語句
爲了不打開文件後忘記關閉,能夠經過管理上下文,即:with
open
(
'log'
,
'r'
) as f:
with
open
(
'log1'
) as obj1,
open
(
'log2'
) as obj2:
pass
程序練習
程序1: 實現簡單的shell sed替換功能
程序2:修改haproxy配置文件
詳細文章:
http://www.cnblogs.com/yuanchenqi/articles/5956943.html
http://www.diveintopython3.net/strings.html
需知:
1.在python2默認編碼是ASCII, python3裏默認是unicode
2.unicode 分爲 utf-32(佔4個字節),utf-16(佔兩個字節),utf-8(佔1-4個字節), so utf-16就是如今最經常使用的unicode版本, 不過在文件裏存的仍是utf-8,由於utf8省空間
3.在py3中encode,在轉碼的同時還會把string 變成bytes類型,decode在解碼的同時還會把bytes變回string