2018年1月11日 星期四

Python: Extract single key-value pair of a dictionary in variables

Python: Extract single key-value pair of a dictionary in variables

Python Basic: Exercise-137 with Solution

Write a Python program to extract single key-value pair of a dictionary in variables.
Sample Solution:-
# Convert tuple to list:
print("# Convert tuple to list:")
t = ('my', 'name', 'is', 'mr', 'tuple')
print(t)
print("Convert to ")
t1=list(t)
print(t1)
print()
#Convert list to tuple:
print("#Convert list to tuple:")
list1 = ['my', 'name', 'is', 'mr', 'list']
print(list1)
print("Convert to ")
list2=tuple(list1)
print(list2)

print()


print("pair of a dictionary in variables")
d = {'Red': 'Green'}
print(d)
(c1, c2), = d.items()
print(c1)
print(c2)

#My dictionary
print()
food = {'pizza': 324, 'sandwich': 78, 'hot dog': 90}
print("dictionary",end='')
print(food)
food_list=list(food.values())
print(food_list)

=========== RESTART: F:/Python_APSC/py-ex-basic-137.py ==========
# Convert tuple to list:
('my', 'name', 'is', 'mr', 'tuple')
Convert to 
['my', 'name', 'is', 'mr', 'tuple']

#Convert list to tuple:
['my', 'name', 'is', 'mr', 'list']
Convert to 
('my', 'name', 'is', 'mr', 'list')

pair of a dictionary in variables
{'Red': 'Green'}
Red
Green

dictionary{'sandwich': 78, 'hot dog': 90, 'pizza': 324}
[78, 90, 324]
>>> 

沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...