其他分享
首页 > 其他分享> > 代码运算

代码运算

作者:互联网




word1 = {'yi':'一','er':'二','san':'三'}
print(word1)

  

 

 

Dictionary = {'key1':'value1','key2':'value2','keyn':'valuen'}

  

word1 = {'yi':'一','er':'二','san':'三'}
print(word1)

  

key = ('yi','er','sen')           #元组
value = ['一','二','三']           #列表
word = {key : value}         #新字典

  

  

{{‘yi’,'er','san'}:['一','二','三']}

  

key = ['yi','er','san']      #音节索引列表
dictionary = dict.fromkeys(key)
print(dictionary)

  

{'yi':None,'er':None,'san':None}

  

print(dictionary["键名"]if"键名"in dictionary else "我的字典里没有对应结果")

  

dictionary.get(key,[default])

  

dictionary.items()          #访问每个键值对
print(dictionary.items()) #输出
for item in dictionary.items()
    print(items)               #for循环进行输出

  

for key,value in dictionary.items():
    print( key,"对应汉字是",value )

 

  

yi   对应汉字为  一
er   对应汉字为  二
san   对应汉字为  三

  

dictionary[key] = value

  

 

dictionary["si"] = "四"
print(dictionary)

  

 

dictionary["san"] = "四"
print(dictionary

  

if 'si' in dictionary:
    del dictionary['si']

  

 

import rendom
randomdict = {i:random.randint(10,100) for i in range(1,5)}
print(randomdict)

  

{1:43,2:65,3:34,4:56}

  

set1 = {'yi','er','san'}
print(set1)

  

set2 = {'yi','er','san','yi'}
print(set2)

  

key = ['yi','er','san']
set1 = set(key)      #转化为集合
print(set1)

  

 

setname.add(dldment)

  

set1 = set (['yi','er','san'])
set1.add('si')
print(set1)

  

set1 = set (['yi','er','san'])
set.remove('yi')

  

 

set1 = set (['yi','er','san'])
set1.pop()

  

set1 = set (['yi','er','san'])
if 'yi' in set1 :set1.remove('yi')

  

 

set1 = set (['yi','er','san'])
set1.clear()
print(set1)
  

  

 

set1 = set (['yi','er','san'])
del set1
print(set1)
  
  

  

 

set1 = set(['1','2','3'])
set2 = set(['3','4','5'])
print(set1 & set2)     #交集运算

  

set1 = set(['1','2','3'])
set2 = set(['3','4','5'])
print(set1 | set2)     #并集运算

  

set1 = set(['1','2','3'])
set2 = set(['3','4','5'])
print(set1 - set2)     #差集运算

  

 

标签:yi,san,dictionary,代码,set1,print,er,运算
来源: https://www.cnblogs.com/1963432477zxl/p/16688764.html