串列 list
可以存放任何資料型別
可變資料型別 vs 不可變資料型別
在原地址記憶體空間里,能修改內容的就是可變資料型別,不能修改內容的就不是可變資料型別
串列元素的增加操作 lst=[10,20,30]
append()(在串列末尾添加一個元素) lst.append(100)
extend() (在串列的末尾至少添加一個元素) lst2=['hello','world'] lst.exted(lst2)
insert() (在串列的任意位置添加一個元素) lst.insert(1,90)
切片 lst3=[Tru,False,'hello'] lst[1:]=lst3
切片例題:
lst3 = [print,None,1,'abd',True]
lst3
[<built-in function print>, None, 1, 'abd', True]
lst3 = [print,None,1,'abd',True]
lst3
[<built-in function print>, None, 1, 'abd', True]
lst3[::-2]
[True, 1, <built-in function print>]
lst3[::-1]
[True, 'abd', 1, None, <built-in function print>]
lst3[:-2]
[<built-in function print>, None, 1]
串列元素的洗掉操作 lst=[10,20,30,40,50,60,30]
remove()(一次洗掉一個元素,重復元素洗掉第一個) lst.remove(30)
pop() (洗掉一個指定索引位置上的元素,不指定索引,洗掉串列最后一個元素) lst.pop(1) lst.pop()
切片 (一次至少洗掉一個元素) new_lst=lst[1:3](產生一個新的串列) lst[1:3]=[] (不產生新的串列物件,而是洗掉原串列中的內容)
clear() (清空串列) lst.clear()
del (洗掉串列) del lst
串列的排序操作 sort 進行排序 reverse 進行反轉
lst=[20,40,10,98,54]
print('排序前的串列',lst)
lst.sort() #升序排序
print('排序后的串列',lst)
lst.sort(reverse=True) #表示降序排序
lst.sort(reverse=False) #表示升序排序
new_lst=sorted(lst)
查找統計類:
copy復制拷貝 count出現多少次數 index出現的下標位置
例題:
有一條船,船上有40個人,船超載20個人,
每數到第九個就讓他下船,最后輸出哪些人下船了,
num = list(range(1,41))
while len(num) > 20:
a = num[8]
num = num[9:] + num[:8]
print(f'{a}', end='#')
lst1 = [1,2,4,5,6]
for i in lst1:
print(i)
成員關系運算子 (in) (not in)
lst1 = [1,2,4,5,6]
if 67 in lst1:
print('ok')
else:
print('no')
enumerate() 可以回傳數值和下標
# 從鍵盤接受用戶輸入,輸入一個整數10,
# 回傳兩個數相加等于10這個數的下標 2,6
lst = [2,5,6,7,3,1,4]
i=0
sum = int(input('請輸入一個整數:'))
for i in range(len(lst)):
for j in range(i+1,len(lst)):
if lst[i] + lst[j] == sum:
print(f'{lst[i]}與{lst[j]}的和為{sum},下標分別為{i},{j}')
或者:
lst = [2,5,6,7,3,1,4]
num=int(input('請輸入一個整數'))
for i,j in enumerate(lst):
num1 = num - j
if num1 in lst[i+1:] and lst.index(num1) > i :
print(f"兩數之和等于{num}的下標為{i},{lst.index(num1)}")
元組(tuple)
有序的專案集合
可以存放任何資料型別
不可變資料型別
內建函式 工廠函式(型別轉換)
#############元組的定義##############
只有一個元素,需要在元素后面加逗號
元組里面包含串列,串列是可以改變的,因為串列是可變資料型別,串列里的元素更改不會改變串列本身
不可變資料型別
例題:用戶檢測小工具
?歡迎語:”歡迎使用用戶檢測系統“
?定義一個用戶串列,包含用戶名密碼:[('root', '123456'), ('admin', 'admin'),]
?鍵盤輸入用戶名、密碼
?判斷并驗證用戶密碼,并給予相應提醒
lst=[('root','123456'),('admin','admin')]
print('歡迎使用用戶檢測系統')
u_name=input('請輸入用戶名')
u_passwd=input('請輸入密碼')
tup=(u_name,u_passwd)
if lst.count(tup) != 0:
print('登陸成功')
else:
print('用戶名或密碼錯誤')
Python 字典(Dictionary) items() 函式以串列回傳可遍歷的(鍵, 值) 元組陣列
d = {'one': 1, 'two': 2, 'three': 3}
>>> d.items()
dict_items([('one', 1), ('two', 2), ('three', 3)])
>>> type(d.items())
<class 'dict_items'>
字典( dict )
什么是字典( dict )
?字典是Python中一個鍵值映射的資料結構
?字典是無序的?字典是可變物件
?key必須是唯一的, so 天生去重
字典的定義
?元組通過花括號中用鍵 / 值對用冒號分割,而各個對用逗號分割
key必須是可hash物件
hash 是一類演算法的統稱,將任意長度的輸入變成固定長度的輸出
hash 是一項單向加密技術,可以通過原文得到hash值,但是不能通過hash去推匯出原文
常常用在驗證檔案的完成性,有沒有被篡改,傳輸程序中有沒有傳完整
字典的原理
字典的本質就是一個空閑的hash表
以空間換時間
key必須是唯一的,而且必須是可hash物件
value可以使任何值
dict.get(key,default) 獲取key的值,如果沒有就回傳default的值
如果沒有default,不存在key的話就回傳None
新增和修改
dict[key]=value
統計字串中的每個字符出現的次數,不要重復輸出
str1='hfisidjofhosif'
d1={}
for i in str1:
d1[i]=d1.get(i,0) + 1
print(d1)
洗掉
pop()
popitem()
d1 = {'a':1}
d2 = {'b':2}
dict(d1,**d2) 生成一個新字典
{'a': 1, 'b': 2}
d1.update(d2) updata會覆寫
d1
{'a': 1, 'b': 2}
print('歡迎使用用戶檢測系統')
d1={'root':'123456','admin':'admin'}
while 1:
u_name=input('請輸入用戶名')
u_passwd=input('請輸入密碼')
if u_name == 'q' or u_passwd == 'q':
break
if u_name in d1 and u_passwd == d1[u_name]:
print('驗證成功')
else:
print('驗證失敗')
print('慢走不送')
集合:
無序,不重復
可變的
看做一個只有key的字典
元素唯一,天生去重
可hash物件
.add()新增
.update()添加可迭代物件
例:
x = set('runoob')
>>> y = set('google')
>>> x, y
(set(['b', 'r', 'u', 'o', 'n']), set(['e', 'o', 'g', 'l'])) # 重復的被洗掉
>>> x & y # 交集
set(['o'])
>>> x | y # 并集
set(['b', 'e', 'g', 'l', 'o', 'n', 'r', 'u'])
>>> x - y # 差集
set(['r', 'b', 'u', 'n'])
>>>
例如:
s1
{1, 2, 3}
s1.update({'c':1,'b':3})
s1
{1, 2, 3, 'c', 'b'}
s1.add('asf')
s1
{'asf', 1, 2, 3, 'c', 'b'}
s1.update({'c':1,'b':3}.items()) #轉換成元組
s1
{'asf', 1, 2, 3, 'c', ('b', 3), ('c', 1), 'b'}
remove()洗掉 如果洗掉的元素沒有,會報錯
discard()洗掉 不會報錯
例如:
s1.remove(5)
Traceback (most recent call last):
File "<input>", line 1, in <module>
KeyError: 5
s1.discard(5)
s1={1,2,3,4,5,6}
s2={1,2,3,4,4,5,6,7,3}
s1&s2 #取交集
{1, 2, 3, 4, 5, 6}
s1|s2 #取或,都存在的,并集
{1, 2, 3, 4, 5, 6, 7}
s1 -s2 #在s1里取s2沒有的
set()
s2 -s1 #在s2里取s1沒有的
{7}
s1 ^ s2 #并集減去交集,對稱差集
{7}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/375091.html
標籤:其他
上一篇:hadoop環境搭建(一)
