国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

字典

2019-11-08 02:23:17
字體:
來源:轉載
供稿:網友
brand=['李寧','耐克','阿迪達斯']slogan=['一切皆有可能','Just do it','impossible is nothing']PRint ('李寧的口號是:',slogan[brand.index('李寧')])#-->李寧的口號是: 一切皆有可能鍵值組合dict1={'李寧':'一切皆有可能','耐克':'Just do it','阿迪達斯':'impossible is nothing'}print('李寧的口號是:',dict1['李寧'])#-->李寧的口號是: 一切皆有可能#創造一個空的字典dict2={}#創造一個字典dict3={1:"one",2:"two",3:"three"}dict3[2]#'two'dict3=dict((('F',70),('i',105),('s',115),('h',104),('C',67)))>>> dict3#一個映射類型的參數mapping{'i': 105, 'F': 70, 'h': 104, 'C': 67, 's': 115}#key+value的形式創建字典,會自動排序,不能使用字符串形式,會自動變成字符串>>> dict4=dict(小甲魚='編程改變世界',花花='小甲魚為什么不加引號')>>> dict4{'花花': '小甲魚為什么不加引號', '小甲魚': '編程改變世界'}>>> dict4['花花']='就是這種形式,實際上會自動添加引號'>>> dict4['草草']='直接給鍵賦值,存在的就修改,不存在就添加'>>> dict4{'草草': '直接給鍵賦值,存在的就修改,不存在就添加', '花花': '就是這種形式,實際上會自動添加引號', '小甲魚': '編程改變世界'}#字典比序列更智能,能添加也能修改>>> dict1={}>>> dict1.fromkeys((1,2,3)){1: None, 2: None, 3: None}>>> dict1.fromkeys((1,2,3),'number'){1: 'number', 2: 'number', 3: 'number'}>>> dict1.fromkeys((1,2,3),('noe','two','three')){1: ('noe', 'two', 'three'), 2: ('noe', 'two', 'three'), 3: ('noe', 'two', 'three')}>>> dict1=dict1.fromkeys(range(5),'贊')>>> dict1{0: '贊', 1: '贊', 2: '贊', 3: '贊', 4: '贊'}>>> for eachkey in dict1.keys(): print (eachkey)01234>>> for eachvalue in dict1.values(): print (eachvalue)贊贊贊贊贊>>> for eachitem in dict1.items(): print (eachitem)(0, '贊')(1, '贊')(2, '贊')(3, '贊')(4, '贊')>>> print(dict1[4])贊>>> print(dict1[5])'''Traceback (most recent call last): File "<pyshell#50>", line 1, in <module> print(dict1[5])KeyError: 5'''>>> print(dict1.get(5))None>>> print(dict1.get(5,'未有'))未有>>> print(dict1.get(4,'未有'))贊>>> 5 in dict1 #查找的是鍵不是值False>>> 4 in dict1True>>> dict1{0: '贊', 1: '贊', 2: '贊', 3: '贊', 4: '贊'}>>> dict1.clear()>>> dict1{}#清空字典用clear(),使用{}并沒有將字典刪除>>> a={1:"good"}>>> b=a>>> a={}>>> a{}>>> b{1: 'good'}>>> a=b>>> a.clear()>>> a{}>>> b{}>>> a={1:"one",2:"two",3:"three"}>>> b=a.copy()>>> c=a>>> a{1: 'one', 2: 'two', 3: 'three'}>>> b{1: 'one', 2: 'two', 3: 'three'}>>> c{1: 'one', 2: 'two', 3: 'three'}>>> id(a)54295368>>> id(b)57779848>>> id(c)54295368>>> c[4]='four'>>> c{1: 'one', 2: 'two', 3: 'three', 4: 'four'}>>> a{1: 'one', 2: 'two', 3: 'three', 4: 'four'}>>> b{1: 'one', 2: 'two', 3: 'three'}>>> a.pop(2)#給定鍵彈出值'two'>>> a{1: 'one', 3: 'three', 4: 'four'}>>> a.popitem()#彈出項,popitem可以看做從字典隨機彈出一個,因為字典沒有序(1, 'one')>>> a{3: 'three', 4: 'four'}>>> a.setdefault("小白")>>> a{3: 'three', 4: 'four', '小白': None}>>> a.setdefault(5,'five')'five'>>> a{3: 'three', 4: 'four', 5: 'five', '小白': None}>>> b={"小白":'狗'}>>> a.update(b)#update用一個字典更新另一個字典>>> a{3: 'three', 4: 'four', 5: 'five', '小白': '狗'}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 辉南县| 乌拉特前旗| 嘉祥县| 九江市| 临安市| 琼结县| 河西区| 尼勒克县| 汉中市| 昭通市| 南宫市| 洪湖市| 都江堰市| 湛江市| 射洪县| 正安县| 开封市| 颍上县| 保德县| 木里| 沙坪坝区| 金阳县| 开化县| 柳河县| 沙洋县| 麦盖提县| 湛江市| 屏东市| 铁岭市| 上犹县| 营山县| 壤塘县| 湟源县| 德钦县| 榆林市| 太湖县| 新乐市| 定南县| 沂水县| 大英县| 遂溪县|