简体   繁体   English

如何使用我的 len() 仅在字典中打印我的项目一次而不打印数字 9、9 次

[英]How to print out my items in my dictionary only once with my len() without it printing the number 9, 9 times

How do I print out my items in my dictionary only once with my len() without it printing the number 9, 9 times.如何使用 len() 仅在字典中打印一次我的项目,而不打印数字 9、9 次。 Please help me, I cant figure this out.请帮助我,我无法解决这个问题。

Heres my code:这是我的代码:

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

You have a for loop running which is printing it multiple times, switch it to just print(len(d)) and that will fix it您有一个 for 循环正在运行,它正在多次打印,将其切换为print(len(d))并修复它

You're printing a len so that is what you see, change to the variable you iterate on您正在打印一个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)

Your need is not clarified.您的需求未明确。 Here is implementation for what i understood from your need.这是我从您的需求中理解的实现。

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.

相关问题 如何打印字典中的选定项目? - How do I print out select items from my dictionary? 我的 python output 打印了两次,而我希望它打印一次 - My python output is printing out twice and while I want it to print out once 当我打印清单上的项目时,如何编号? - How would I number the items on my list out when I print them? 为什么只打印我字典中的第一个值 - Why is only the first value in my dictionary printing 如何打印出一个阶乘数? 我的代码不断打印出太多数字,我不知道为什么? - How do I print out one factorial number? My code keeps printing out too many numbers and I do not know why? 为什么我的代码只打印 x 和 y 一次? - Why is my code only printing x and y once? 如何打印字典,以便将我的项目打印在引号和括号中? - How do I print a dictionary so my items are printed in quotation marks and in brackets? 用于打印字典项目一次而不重复打印的 for 循环 function - A for loop to print dictionary items once without repeating the print function 当我的Python字典中出现KeyError时,如何打印出某些内容? - How can I print out something when I have KeyError for my dictionary in Python? 字典中的键错误。 如何使Python打印我的字典? - Key error in dictionary. How to make Python print my dictionary?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM