有時,在使用 Python 字典時,我們可能會遇到一個問題,即鍵與值映射,其中鍵表示必須放置值的列表索引。這類問題可以應用於所有數據領域,例如Web開發。讓我們討論執行此任務的某些方法。
Input : test_dict = { 1 : ‘Gfg’, 3 : ‘is’, 5 : ‘Best’ }
Output : [0, ‘Gfg’, 0, ‘is’, 0, ‘Best’, 0, 0, 0, 0, 0]Input : test_dict = { 2 : ‘Gfg’, 6 : ‘Best’ }
Output : [0, 0, ‘Gfg’, 0, 0, 0, ‘Best’, 0, 0, 0, 0]
方法#1:使用列表推導式 + keys() 上述函數的組合可以用來解決這個問題。在此,我們通過提取字典的鍵進行查找來執行為列表檢查索引分配值的任務。
Python3
# Python3 code to demonstrate working of
# Convert Index Dictionary to List
# Using list comprehension + keys()
# initializing dictionary
test_dict = { 2 : 'Gfg', 4 : 'is', 6 : 'Best' }
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Convert Index Dictionary to List
# Using list comprehension + keys()
res = [test_dict[key] if key in test_dict.keys() else 0 for key in range(10)]
# printing result
print("The converted list : " + str(res))
輸出:
The original dictionary is : {2: 'Gfg', 4: 'is', 6: 'Best'} The converted list : [0, 0, 'Gfg', 0, 'is', 0, 'Best', 0, 0, 0]
時間複雜度:O(n),其中n是列表test_dict的長度
輔助空間:創建大小為 n 的 O(n) 附加空間,其中 n 是 res 列表中的元素數量
方法#2:使用列表推導式 + get() 上述函數的組合可以用來解決這個問題。在此,我們使用get()提取元素,以便利用get()的默認值初始化屬性來分配默認值。
Python3
# Python3 code to demonstrate working of
# Convert Index Dictionary to List
# Using list comprehension + get()
# initializing dictionary
test_dict = { 2 : 'Gfg', 4 : 'is', 6 : 'Best' }
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Convert Index Dictionary to List
# Using list comprehension + get()
res = [test_dict.get(ele, 0) for ele in range(10)]
# printing result
print("The converted list : " + str(res))
輸出:
The original dictionary is : {2: 'Gfg', 4: 'is', 6: 'Best'} The converted list : [0, 0, 'Gfg', 0, 'is', 0, 'Best', 0, 0, 0]
方法#3:使用*運算符和insert()方法
- 創建一個 0 的列表
- 稍後訪問字典並在列表中的鍵索引處插入值
- 顯示列表
Python3
# Python3 code to demonstrate working of
# Convert Index Dictionary to List
# initializing dictionary
test_dict = {2: 'Gfg', 4: 'is', 6: 'Best'}
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
# Convert Index Dictionary to List
x = [0]*10
for i in list(test_dict.keys()):
x.insert(i, test_dict[i])
# printing result
print("The converted list : " + str(x))
輸出
The original dictionary is : {2: 'Gfg', 4: 'is', 6: 'Best'} The converted list : [0, 0, 'Gfg', 0, 'is', 0, 'Best', 0, 0, 0, 0, 0, 0]
時間複雜度:O(N)
輔助空間:O(N)
相關用法
- Python InteractiveConsole runcode()用法及代碼示例
- Python InteractiveInterpreter runsource()用法及代碼示例
- Python InteractiveInterpreter runcode()用法及代碼示例
- Python IncrementalEncoder encode()用法及代碼示例
- Python Int轉Bytes用法及代碼示例
- Python Integer Matrix轉String Matrix用法及代碼示例
- Python Inspect用法及代碼示例
- Python Integer和Float的區別用法及代碼示例
- Python Itertools.chain()用法及代碼示例
- Python Itertools.compress()用法及代碼示例
- Python Itertools.cycle()用法及代碼示例
- Python Itertools.dropwhile()用法及代碼示例
- Python Itertools.islice()用法及代碼示例
- Python Itertools.Permutations()用法及代碼示例
- Python Itertools.starmap()用法及代碼示例
- Python Itertools.takewhile()用法及代碼示例
- Python Itertools.Product()用法及代碼示例
- Python Itertools.count()用法及代碼示例
- Python Itertools.accumulate()用法及代碼示例
- Python Itertools.filterfalse()用法及代碼示例
- Python Itertools.tee()用法及代碼示例
- Python Itertools.Combinations_with_replacement()用法及代碼示例
- Python Itertools.zip_longest()用法及代碼示例
- Python Image轉PDF用法及代碼示例
- Python Import用法及代碼示例
注:本文由純淨天空篩選整理自manjeet_04大神的英文原創作品 Python – Convert Index Dictionary to List。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。