views:

171

answers:

3

Hey everybody,

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!

A: 

I think this is what you are looking for:

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

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.

synasius
A: 

Wow, that easy, huh? Thank you SO much, can't express how much I appreciate the quick feedback!

WorkingStudent09