Dictionaries|Dictionaries And Identity Operators
Dictionaries
Dictionaries can have keys of any immutable type, like integers or tuples even list, not just strings.
elements = {"hydrogen": 1, "helium": 2, "carbon": 6}# attention mutable can't be used as a key in dictionary
print(elements["helium"])# print the value mapped to "helium"
elements["lithium"] = 3# insert "lithium" with a value of 3 into the dictionary
animals = {'dogs': [20, 10, 15, 8, 32, 15], 'cats'(this is the key): [3,4,2,8,2,4](this is the value), 'rabbits': [2, 3, 3], 'fish': [0.3, 0.5, 0.8, 0.3, 1]} # the example of defining the lists as values
# The result of animals['dogs'][3] is 8
attention mutable can't be used as a key in dictionaryLook Up
print("carbon" in elements)
print(elements.get("dilithium"))
# output:
True
None
Identity Operators
You can check if a key returned None with the is operator. You can check for the opposite using is not.
n = elements.get("dilithium")
print(n is None)
print(n is not None)
Compound Data Structures In Dictionary Defined
elements = {"hydrogen": {"number": 1,
"weight": 1.00794,
"symbol": "H"},
"helium": {"number": 2,
"weight": 4.002602,
"symbol": "He"}}
Access Elements
helium = elements["helium"]# get the helium dictionary
hydrogen_weight = elements["hydrogen"]["weight"]# get hydrogen's weight
Add
oxygen = {"number":8,"weight":15.999,"symbol":"O"}# create a new oxygen dictionary
elements["oxygen"] = oxygen# assign 'oxygen' as a key to the elements dictionary
print('elements = ', elements)
# output:
elements ={"hydrogen": {"number": 1,
"weight": 1.00794,
"symbol": 'H'},
"helium": {"number": 2,
"weight": 4.002602,
"symbol": "He"},
"oxygen": {"number": 8,
"weight": 15.999,
"symbol": "O"}}
文章图片
15705531.png
【Dictionaries|Dictionaries And Identity Operators】to solved this problem my solution is
文章图片
15860375.png
remember boolean is also can be a value in dictionary
so the anwser is
文章图片
16078453.png
the nature of boolean is integer "1" and "0"
推荐阅读
- android第三方框架(五)ButterKnife
- Android中的AES加密-下
- Eddy小文
- 带有Hilt的Android上的依赖注入
- android|android studio中ndk的使用
- Android事件传递源码分析
- RxJava|RxJava 在Android项目中的使用(一)
- Android7.0|Android7.0 第三方应用无法访问私有库
- 深入理解|深入理解 Android 9.0 Crash 机制(二)
- EditText默认不获取焦点弹出键盘