这是字典
cars = {'A':{'speed':70,
'color':2},
'B':{'speed':60,
'color':3}}
Run Code Online (Sandbox Code Playgroud)
用这个 for loop
for keys,values in cars.items():
print(keys)
print(values)
Run Code Online (Sandbox Code Playgroud)
它打印以下内容:
B
{'color': 3, 'speed': 60}
A
{'color': 2, 'speed': 70}
Run Code Online (Sandbox Code Playgroud)
但我希望程序打印出来像这样:
B
color : 3
speed : 60
A
color : 2
speed : 70
Run Code Online (Sandbox Code Playgroud)
我刚开始学习字典,所以我不知道该怎么做.
例如,假设我们有以下字典:
dictionary = {'A':4,
'B':6,
'C':-2,
'D':-8}
Run Code Online (Sandbox Code Playgroud)
如果给出它的价值,你如何打印某个键?
print(dictionary.get('A')) #This will print 4
Run Code Online (Sandbox Code Playgroud)
你怎么能倒退呢?即,不是通过引用键获取值,而是通过引用该值来获取键.
可以说我有一个这样的字典:
{'1': 2, '0': 0, '3': 4, '2': 4, '5': 1, '4': 1, '7': 0, '6': 0, '9': 0, '8': 0}
Run Code Online (Sandbox Code Playgroud)
我想删除值为零的所有项目
所以它就像这样
{'1': 2, '3': 4, '2': 4, '5': 1, '4': 1}
Run Code Online (Sandbox Code Playgroud) 比如说
numbers=input("Enter numbers: ")
Run Code Online (Sandbox Code Playgroud)
如果有人输入11234458881
我怎样才能输出
1发生3次
2发生1次
3发生1次
4次发生2次
等等