2
def makeDict(filename):
    with open(filename,"r") as f:
         dict_={}
         for line in f:
             (key,value1,value2)=line.split()
             dict_[key]=[(float(value1),float(value2))]

mylist=[]
for value1 in dict_:
     if number < value1 < number:
        mylist.append(key)

one last question and I should be done here. It appears that my values are still not coming out as floats because I am trying to check if the value is between 2 numbers and i get this error

TypeError: unorderable types: int() < str()

    newDict=makeDict("file.txt")
     >>> testdict(500,530,newDict)

    line 28, in testdict
    for k,v in newDict.iteritems():
 AttributeError: 'dict' object has no attribute 'iteritems'
frank
  • 21
  • 3

4 Answers4

2

Your function makeDict does not return a value.

def makeDict(filename):
    with open(filename,"r") as f:
         d={}
         for line in f:
             (key,value1,value2)=line.split()
             d[key]=[(float(value1),float(value2))]
    # Return the dictionary
    return d

EDIT

The problem in your code is that when you iterate through a dictionary with only one value, in this case value1, it will only iterate through the keys. What you want is to iterate through the values which can be done with the method .values()

def makeDict(filename):
    with open(filename,"r") as f:
         dict_={}
         for line in f:
             (key,value1,value2)=line.split()
             dict_[key]=[(float(value1),float(value2))]

mylist=[]
# Iterate through the values instead
for value1 in dict_.values():
     if number < value1 < number:
        mylist.append(key)

The last error that you have is likely caused by the Python version you are using. See this post.

In Py3.x, things are more clean, since there are only dict.items(), dict.keys() and dict.values() available, which return the view objects just as dict.viewitems() in Py2.x did.

So you should change the code in testdict to for k, v in newDict.items():

Community
  • 1
  • 1
Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38
1

Use the float() function to convert your values to floating point numbers.

Also, you should close() your file after working with it, or wrap your code in a with block (which will close the file for you):

def makedict(filename):
    with open(filename,"r") as f:
        d={}
        for line in f:
            (key,value1,value2)=line.split()
            d[key]=(float(value1),float(value2))
    return d

Finally, don't use dict as a variable name, because it already exists as a builtin. Use dict_ instead (or another name altogether):

dict_ = makeDict["file.txt"]

EDIT

Take this with a grain of salt, as it's just a guess based on the info in your question, but you want to change this:

for value in dict_:
    if number < value1 < number:
         mylist.append(key)

to this:

for k, v in dict_.items():
    if number < v[0] < number:
        mylist.append(k)
Daniel
  • 2,345
  • 4
  • 19
  • 36
  • so is that all i need to do to turn them into a dictionary? – frank May 28 '16 at 03:26
  • Yup. If you are encountering an error, just edit your post above for more help. – Daniel May 28 '16 at 03:39
  • im just curious is there a way to print(d) just to check or anything cuz everything ive tried ends up saying d isnt defined – frank May 28 '16 at 03:54
  • Sure; you could `print d` to see the current value for `d`. If you copy/paste the exact error you are getting to your question, that's a huge help. – Daniel May 28 '16 at 03:58
  • Just edited my answer per the additional info in your question. – Daniel May 28 '16 at 04:38
  • and how would i go about returning the key after? or appending to a list – frank May 28 '16 at 05:58
  • Just edited my answer. Since you are new to SO, if that answers your question(s), it's a good practice to accept the answer so that other folks can learn from your post. – Daniel May 28 '16 at 06:10
  • Use `dict_.items()` instead of `dict_.iteritems()` if you are using Python 3. – Daniel May 28 '16 at 06:55
0
def makedict(filename):
    filename=open(filename,"r")
    d={}
    for line in filename:
         (key,value1,value2)=line.split()
          d[key]=(float(value1),float(value2))
Loïc
  • 11,804
  • 1
  • 31
  • 49
0

Use this :

def makedict(fname):
    filename=open(fname,"r")
    d={}
    for line in filename:
         (key,value1,value2)=line.split()
         d[key]=[(value1),(value2)]
    print(d) ## print the dictionary

Here you are storing the values as a list.

For example :

File -

A B C
1 2 E
3 4 E
5 6 E
7 8 E

Output (dictionary)-

{'3': ['4', 'E'], '7': ['8', 'E'], 'A': ['B', 'C'], '5': ['6', 'E'], '1': ['2', 'E']}

Here A,1,3,.. are keys and ['B', 'C'], ['2', 'E'], .. are corresponding values.

Ani Menon
  • 27,209
  • 16
  • 105
  • 126