將字典鍵和值拆分為單獨的列表

2022-03-03 02:50:35 字數 2229 閱讀 3103

給定字典,任務是將該字典拆分為鍵,並將值拆分為不同的列表。

讓我們討論執行此操作的不同方法。

方法1:使用 

built-in functions

# python code to demonstrate

# to split dictionary

# into keys and values

# initialising _dictionary

ini_dict =

# printing iniial_dictionary

print(

"intial_dictionary

", str(ini_dict))

# split dictionary into keys and values

keys =ini_dict.keys()

values =ini_dict.values()

# printing keys and values separately

print(

"keys :

", str(keys))

print(

"values :

", str(values))

輸出:

intial_dictionary

鍵:dict_keys(['a','b','c'])

值:dict_values(['akshat','bhuvan','chandan'])

方法2:使用 

zip()

# python code to demonstrate

# to split dictionary

# into keys and values

# initialising _dictionary

ini_dict =

# printing iniial_dictionary

print(

"intial_dictionary

", str(ini_dict))

# split dictionary into keys and values

keys, values = zip(*ini_dict.items())

# printing keys and values separately

print(

"keys :

", str(keys))

print(

"values :

", str(values))

輸出:

intial_dictionary 

鍵:('a','c','b')

值:(「 akshat」,「 chandan」,「 bhuvan」)

方法#3:使用 

items()

# python code to demonstrate

# to split dictionary

# into keys and values

# initialising _dictionary

ini_dict =

# printing iniial_dictionary

print(

"intial_dictionary

", str(ini_dict))

# split dictionary into keys and values

keys =

values =

items =ini_dict.items()

for item in

items:

])# printing keys and values separately

print (

"keys :

", str(keys))

print (

"values :

", str(values))

出:

intial_dictionary 

鍵:['b','c','a']

值:['bhuvan','chandan','akshat']

將字串拆分為陣列

根據符號分string s1 sdsad,3232 222.ssss string s2 s1.split foreach string s3 in s2 console.readkey 去除空字元 string s4 aa,bb,cc,dd,string s5 s4.split new char,...

Acrobat如何將PDF拆分為多個文件

今天小e打算分享乙個電子書,結果一上傳準備發布外鏈的時候,居然超出了100m,只有會員才可以分享100m以上的文件或者軟體,小e窮,沒錢公升會員,所以只能把這個完整的電子書拆分成兩份,特把拆分的方法,分享如下 利用acrobat您可以將乙個或多個pdf文件拆分為多個更小的文件,當拆分文件時,可以指定...

將Go的main包拆分為多個檔案

將go的main包拆分為多個檔案的寫法和普通包是完全一致的,其使用規則也相同。如編寫main包結構如下 main main.go show.go 在main.go中編寫了main函式,main函式呼叫了在show中定義的函式或變數。到此和一般的go包都一致。但是當執行go run mian.go時會...