[英]How to print out my items in my dictionary only once with my len() without it printing the number 9, 9 times
如何使用 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)
for x in d: print(len(d))
您有一個 for 循環正在運行,它正在多次打印,將其切換為print(len(d))
並修復它
您正在打印一個len
,這就是您所看到的,更改為您迭代的變量
d = {"Brand: ": "Honda", "model: ": "Pilot", "year: ": 2021}
for key, val in d.items():
print(key, val)
ls = ["Chevrolet", "Dodge", "Ford", "Honda", "Jeep", "Nissan", "Saturn", "Subaru", "Tesla", "Toyota"]
for x in ls:
print(x)
您的需求未明確。 這是我從您的需求中理解的實現。
d = {
"Brand: ": "Honda",
"model: ": "Pilot",
"year: ": 2021
}
ls = ["Chevrolet", "Dodge", "Ford", "Honda", "Jeep", "Nissan", "Saturn", "Subaru", "Tesla", "Toyota"]
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(len(ls))
for l in ls:
print(l)
cars(d, ls)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.