1. 程式人生 > >Python基礎day-4[dict,set,bool]

Python基礎day-4[dict,set,bool]

pro 錯誤 name 特性 字典 無法 密碼 精確 新增

布爾類型:

  True和False

  所有的數據類型自帶布爾值,只有0,None,空的布爾值為False

字典dict:

  特性:dict中使用key和對應的value來存儲數據,dict不像 list那樣,數據越多查詢越慢.dict中key必須是不可變類型,或者說必須是可hash類型.value的值是可以改變的,可以是任意類型的數據.字典的取值是無序的.無法通過索引取值.

  定義dict:

  d = {key:value,key:value,key:value}

  d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}

  新增:

  d = {}

  d[‘x‘] = 1

  取值: 通過key取值

  print(d[‘name‘])

  print(d[1])

  循環:

  for i in d:
  print(i) #利用for循環取直接取字典內容,只能取出字典的key
  print(d[i]) #利用取出的key,打出對應的value

  嵌套使用: list和字典可以互相套,list中可以有字典,字典的value也可以是列表,註意:list是不可hash類型,也就說不能作為key

  user_info=[ #定義一個列表,列表中使用字典存儲用戶名和密碼
  {‘username‘:‘egon‘,‘password‘:‘123‘},
  {‘username‘:‘alex‘,‘password‘:‘alex3714‘},
  {‘username‘:‘yuanhao‘,‘password‘:‘sb123‘},
  ]
  for item in user_info: #for循環提取list中的dict
  print(item[‘username‘],item[‘password‘]) #打印dict中的 username和password

  成員運算:

  d = {‘x‘:1,‘u‘:2}

  print(‘x‘ in d)

  print(1 in d.values())

  dict的常用操作:

    d.clear():清空字典的所有內容

    d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
    d.clear()
    print(d)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{}

Process finished with exit code 0

    d.get():帶返回值的取值方式,當取出不存的值時會返回None 不會報錯

    d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
    print(d.get(‘abc‘))

D:\Python\Python36-32\python.exe E:/Python/tmp.py
None

Process finished with exit code 0

    d.items():以元組的方式顯示鍵值對,for循環時可以給兩個變量,同時取出key和value

    d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
    for k,v in d.items():
    print(k,v)
    print(‘==================‘)
    for i in d.items():
    print(i)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
name abc
age 18
1 id
==================
(name, abc)
(age, 18)
(1, id)

Process finished with exit code 0

    d.keys and d.values:指定提取全部的key或者value

    d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
    print(d.keys())
    print(d.values())

D:\Python\Python36-32\python.exe E:/Python/tmp.py
dict_keys([name, age, 1])
dict_values([abc, 18, id])

Process finished with exit code 0

    d.pop():pop是帶返回值的,精確刪除某個key

    d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
    print(d.pop(‘age‘)) #pop()必須給予一個key,否則報錯
    print(d)
    print(d.pop(‘abc‘,‘沒有abc那個key‘)) #由於pop帶返回值,所以可以指定返回信息,來去除錯誤信息.不指定返回值,當刪除一個不存在的key時會報錯.

D:\Python\Python36-32\python.exe E:/Python/tmp.py
18
{name: abc, 1: id}
沒有abc那個key

Process finished with exit code 0

    d.popitem():隨機刪除一組key和對應的value

    d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
    d.popitem()
    print(d)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{name: abc, age: 18}

Process finished with exit code 0

    d.setdefault():向dict中添加key和對應的value,如果key已存在則不添加(也不會報錯.)

    d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
    d.setdefault(‘aaa‘,111)
    print(d)
    d.setdefault(‘name‘,‘abc‘)
    print(d)
    d.setdefault(‘name‘,‘abc123‘)
    print(d)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{name: abc, age: 18, 1: id, aaa: 111}
{name: abc, age: 18, 1: id, aaa: 111}
{name: abc, age: 18, 1: id, aaa: 111}

Process finished with exit code 0

    幾種新建字典的方式:

d1={}
d2=dict()
print(d1,d2)
d3=dict(x=1,y=2,z=3)
print(d3)
d4=dict({x:1,y:2,z:3})
print(d4)
d5=dict([(‘x‘,1),(‘y‘,2),(‘z‘,3)])
print(d5)
D:\Python\Python36-32\python.exe E:/Python/tmp.py
{} {}
{x: 1, y: 2, z: 3}
{x: 1, y: 2, z: 3}
{‘x‘: 1, ‘y‘: 2, ‘z‘: 3} Process finished with exit code 0

    d.fromkeys(): 創建的dict的格式化,前面所有的key匹配同一個value

    d={}.fromkeys([‘name‘,‘age‘],None)
    print(d)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{name: None, age: None}

Process finished with exit code 0

    d.update():更新列表,沒有的key就添加,key如果存在就更新value

    d = {‘name‘:‘abc‘,‘age‘:18,1:‘id‘}
    d1 = {‘name‘:‘ac‘,1:‘i‘}

    d1.update(d)
    print(d1)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{name: abc, 1: id, age: 18}

Process finished with exit code 0

集合set:

  特性:集合內的元素必須是唯一的,集合內的元素必須是可hash的,也就是不可變類型,集合是無序的

  定義集合:

  s = {‘abc‘,1,‘aaa‘,(1,2),‘name‘}

 集合的作用:

  1.去重 #因為集合的元素是唯一的所以可用於去除重復的元素.

  2.關系運算:

    python_s={‘egon‘,‘alex‘,‘abc‘,‘wang‘}
    linux_s={‘alex‘,‘abc‘,‘aaa‘,‘bbb‘}
    # 取共同部分:交集
    print(python_s & linux_s)
    #取老男孩所有報名學習的學生:並集
    print(python_s | linux_s)
    #取只報名了python課程的學生:差集
    print(python_s - linux_s)
    # 取只報名了linux課程的學生:差集
    print(linux_s - python_s)
    # 取沒有同時報名python和linux課程的學:對稱差集
    print(linux_s ^ python_s)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{abc, alex}
{wang, bbb, abc, aaa, egon, alex}
{egon, wang}
{aaa, bbb}
{aaa, egon, wang, bbb}

Process finished with exit code 0

  集合的操作:

    關系運算
    python_s={‘egon‘,‘alex‘,‘abc‘,‘wang‘}
    linux_s={‘alex‘,‘abc‘,‘aaa‘,‘bbb‘}

    print(python_s.intersection(linux_s))  # 交集:python_s & linux_s
    print(python_s.union(linux_s))    #並集:|
    print(python_s.difference(linux_s))   #python_s-linux_s
    print(python_s.symmetric_difference(linux_s))  # 對稱差集,python_s ^ linux_s

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{abc, alex}
{wang, egon, abc, alex, bbb, aaa}
{wang, egon}
{bbb, wang, egon, aaa}

Process finished with exit code 0

    s.difference_update():差集更新

    python_s={‘egon‘,‘alex‘,‘abc‘,‘wang‘}
    linux_s={‘alex‘,‘abc‘,‘aaa‘,‘bbb‘}

    python_s.difference_update(linux_s)
    print(python_s)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{wang, egon}

Process finished with exit code 0

    s.update():更新,如果沒有添加

    s1={‘a‘,1}
    s2={‘a‘,‘b‘,2}

    s1.update(s2)
    print(s1)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{1, 2, b, a}

Process finished with exit code 0

    s.add():添加一個元素

    s={‘a‘,1}
    s.add(‘abc‘)
    print(s)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{1, a, abc}

Process finished with exit code 0

    s.discard():刪除一個指定的元素,刪除不存在元素時不會報錯

    s={‘a‘,1}
    s.discard(‘a‘)
    print(s)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{1}

Process finished with exit code 0

    s.remove():刪除一個指定元素,刪除不存在的元素時會報錯

    s={‘a‘,1}
    s.remove(‘a‘)
    print(s)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
{1}

Process finished with exit code 0

    s.pop():隨機刪除一個元素

    s={‘a‘,2,3,4,5,6,7,8,9}
    print(s.pop())
    print(s)

D:\Python\Python36-32\python.exe E:/Python/tmp.py
a
{2, 3, 4, 5, 6, 7, 8, 9}

Process finished with exit code 0

    s.issubset():判斷是否是子集

    s.issuperset():判斷是否是父集

    s1={1,2,}
    s2={1,2,3}

    print(s1.issubset(s2))
    print(s2.issuperset(s1))

D:\Python\Python36-32\python.exe E:/Python/tmp.py
True
True

Process finished with exit code 0

    s.isdisjoint():沒有交集返回True

    s1={‘a‘,‘b‘}
    s2={1,2,3}

    print(s1.isdisjoint(s2))

D:\Python\Python36-32\python.exe E:/Python/tmp.py
True

Process finished with exit code 0

Python基礎day-4[dict,set,bool]