将用户输入整数与字典值进行比较? (Python)
我是一个 python 菜鸟,我正在尝试编写一个程序,该程序将向用户显示拨打次数超过 X 次(用户输入 X 次)的电话号码列表。我已经让程序成功读取重复项并对它们进行计数(数字存储在字典中,其中 {phoneNumber : numberOfTimesCalled}),但我需要将用户输入(一个整数)与字典中的值进行比较,然后打印被呼叫 X 次或更多次的电话号码。到目前为止,这是我的代码:
import fileinput
dupNumberCount = {}
phoneNumLog = list()
for line in fileinput.input(['PhoneLog.csv']):
phoneNumLog.append(line.split(',')[1])
userInput3 = input("Numbers called greater than X times: ")
for i in phoneNumLog:
if i not in dupNumberCount:
dupNumberCount[i] = 0
dupNumberCount[i] += 1
print(dupNumberCount.values())
userInput = input("So you can view program in command line when program is finished")
基本上,我不知道如何将字典值转换为整数,将用户输入的整数与该值进行比较,并打印出与字典值对应的电话号码。非常感谢任何帮助!
顺便说一句,我的字典有大约 10,000 个键:值,它们的组织方式如下:
'6627793661': 1, '6724734762': 1, '1908262401': 1, '7510957407': 1
希望我已经为大家提供了足够的信息来帮助我完成该程序!
I'm a python noob and I'm trying to write a program that will show a user a list of phone numbers called greater than X times (X input by users). I've got the program to successfully read in the duplicates and count them (the numbers are stored in a dictionary where {phoneNumber : numberOfTimesCalled}), but I need to compare the user input, an integer, with the value in the dictionary and then print the phone numbers that were called X or more times. This is my code thus far:
import fileinput
dupNumberCount = {}
phoneNumLog = list()
for line in fileinput.input(['PhoneLog.csv']):
phoneNumLog.append(line.split(',')[1])
userInput3 = input("Numbers called greater than X times: ")
for i in phoneNumLog:
if i not in dupNumberCount:
dupNumberCount[i] = 0
dupNumberCount[i] += 1
print(dupNumberCount.values())
userInput = input("So you can view program in command line when program is finished")
Basically, I can't figure out how to convert the dictionary values to integers, compare the user input integer to that value, and print out the phone number that corresponds to the dictionary value. Any help GREATLY appreciated!
By the way, my dictionary has about 10,000 keys:values that are organized like this:
'6627793661': 1, '6724734762': 1, '1908262401': 1, '7510957407': 1
Hopefully I've given enough information for you all to help me out with the program!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想这就是您正在寻找的:
I think this is what you are looking for:
另一种解决方案,可能会在学习Python时帮助您:
还有一件事,您不需要将计数值转换为整数,因为它已经是整数了。无论如何,如果您需要转换文字整数(例如'2345'),可以使用内置函数int('2345')。还有 float() 它对于从字面量中获取浮点数很有用,例如 float('12.345')。亲自尝试一下。
Another solution, may help you while learning python:
One more thing, you don't need to convert count value to integer because it's already an integer. Anyway, if you need to convert a literal integer (eg '2345') there is the builtin function int('2345'). Also there is float() which is useful to get float from literal like float('12.345'). Try it for your self.