【发布时间】:2022-01-19 22:08:46
【问题描述】:
如何只用我的 len() 将我的字典中的项目打印一次,而不打印数字 9、9 次。请帮帮我,我想不通。
这是我的代码:
d = {
"Brand: ": "Honda",
"model: ": "Pilot",
"year: ": 2021
}
for x in d:
print(len(d))
ls = ["Chevrolet", "Dodge", "Ford", "Honda", "Jeep", "Nissan", "Saturn", "Subaru", "Tesla", "Toyota"]
for x in ls:
print(len(ls))
ri = raw_input("Enter 'd', if you want to see a dictionary for a car. Or enter 'ls', if you want to see a list of cars: ")
def cars(d, ls):
print ri
if ri == 'd':
print d
if ri == 'ls':
print ls
cars(d, ls)
【问题讨论】:
-
喜欢
print(d)? -
你为什么写
for x in d: print(len(d))?这确实可以满足您的要求。 -
为什么该代码混合了 python2 和 python3 代码?这是不可能的;)
-
将
for x in d: print(len(d))更改为for x in d.values(): print(x)` -
也许你想要的是
for x in ls: print(len(x))
标签: python function loops dictionary