将用户输入整数与字典值进行比较? (Python)

发布于 2024-08-13 22:35:46 字数 917 浏览 1 评论 0原文

我是一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

追星践月 2024-08-20 22:35:46

我想这就是您正在寻找的:

for a in dupNumberCount.keys():
  if dupNumberCount[a]>=userInput:
     print a

I think this is what you are looking for:

for a in dupNumberCount.keys():
  if dupNumberCount[a]>=userInput:
     print a
苦笑流年记忆 2024-08-20 22:35:46

另一种解决方案,可能会在学习Python时帮助您:

import fileinput

dupNumberCount = {}

# Create dictionary while reading file
for line in fileinput.input(['PhoneLog.csv']):
    phoneNum = line.split(',')[1]
    try:
        dupNumberCount[phoneNum] += 1
    except KeyError:
        dupNumberCount[phoneNum] = 1

userInput3 = input("Numbers called greater than X times: ")

# iteritems method give you a tuple (key,value) for every item in dictionary
for phoneNum, count in dupNumberCount.iteritems():
    if count >= userInput3:
    print "Phone %s has been called %d" % (phoneNum, count)

还有一件事,您不需要将计数值转换为整数,因为它已经是整数了。无论如何,如果您需要转换文字整数(例如'2345'),可以使用内置函数int('2345')。还有 float() 它对于从字面量中获取浮点数很有用,例如 float('12.345')。亲自尝试一下。

Another solution, may help you while learning python:

import fileinput

dupNumberCount = {}

# Create dictionary while reading file
for line in fileinput.input(['PhoneLog.csv']):
    phoneNum = line.split(',')[1]
    try:
        dupNumberCount[phoneNum] += 1
    except KeyError:
        dupNumberCount[phoneNum] = 1

userInput3 = input("Numbers called greater than X times: ")

# iteritems method give you a tuple (key,value) for every item in dictionary
for phoneNum, count in dupNumberCount.iteritems():
    if count >= userInput3:
    print "Phone %s has been called %d" % (phoneNum, count)

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文