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]
>>> 

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...