simple question about dictionaries

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skazhy

    simple question about dictionaries

    hi, i am new to python, so i've a really simple question about
    dictionaries.
    if i have a dictionary and I make have an input after it (to input
    numbers) can i get the key of value that was in input?

    somehting like this:
    dict = { "key1"=100,"key 2"=200,"key3"=3 00}
    a = input()
    print 'the key of inputted value is', dict['a']

    this syntax of course is wrong, but i hope you got the point..

    thanks in advance!

  • Berco Beute

    #2
    Re: simple question about dictionaries

    Sounds like a school assignment. Find the answer yourself here:
    Explore the power of Python in the 2025: see our free tutorials and obtain the "Dive into Python" book by Mark Pilgrim in PDF and Kindle version.


    You'll learn a lot more in the process.

    2B

    Comment

    • Jeff

      #3
      Re: simple question about dictionaries

      On Jul 21, 7:35 am, skazhy <ska...@gmail.c omwrote:
      hi, i am new to python, so i've a really simple question about
      dictionaries.
      if i have a dictionary and I make have an input after it (to input
      numbers) can i get the key of value that was in input?
      >
      somehting like this:
      dict = { "key1"=100,"key 2"=200,"key3"=3 00}
      a = input()
      print 'the key of inputted value is', dict['a']
      >
      this syntax of course is wrong, but i hope you got the point..
      >
      thanks in advance!
      I don't think there is a built-in that retrieves dict keys by value,
      but if the dictionary were small enough, you could search the list of
      key/value pairs returned by dict.items(). You can also iterate
      through all pairs with dict.iteritems( ), which returns an iterator.
      Something like:

      def key_by_value(dc t, val):
      for k, v in dct.iteritems() :
      if v == val:
      return v
      throw KeyError('%s not found' % str(val))

      Comment

      • Fredrik Lundh

        #4
        Re: simple question about dictionaries

        skazhy wrote:
        hi, i am new to python, so i've a really simple question about
        dictionaries.
        if i have a dictionary and I make have an input after it (to input
        numbers) can i get the key of value that was in input?
        A dictionary contains (key, value) pairs, and is optimized for quickly
        finding the value in a pair, if given the key.
        somehting like this:
        dict = { "key1"=100,"key 2"=200,"key3"=3 00}
        dict() is a built-in function; you're free to reuse the names of such
        functions as variable names in Python, but if you do, you won't be able
        to use those functions in that part of your program. And in this case,
        the dict() built-in might be useful (see below):
        a = input()
        print 'the key of inputted value is', dict['a']
        >
        this syntax of course is wrong, but i hope you got the point..
        Assuming that you want the *key* in Python's sense, for a given value,
        you can either loop over the dictionary (called D below):

        for k, v in D.items():
        if v == a:
        print 'the key of inputted value is', v
        break
        else:
        print "not found"

        or create an reverse mapping and use that (you only have to do this
        every time the dictionary changes, of course), e.g.

        reverse_D = dict((D[k], k) for k in D)

        a = input()
        print "the key for value", a, "is", reverse_D[a]

        (this doesn't work if you call your dictionary "dict", obviously.)

        </F>

        Comment

        • Fredrik Lundh

          #5
          Re: simple question about dictionaries

          Jeff wrote:
          throw KeyError('%s not found' % str(val))
          "throw"? and shouldn't that be a ValueError? ;-)

          </F>

          Comment

          • Jeff

            #6
            Re: simple question about dictionaries

            On Jul 21, 8:14 am, Fredrik Lundh <fred...@python ware.comwrote:
            Jeff wrote:
              throw KeyError('%s not found' % str(val))
            >
            "throw"?  and shouldn't that be a ValueError? ;-)
            >
            </F>
            Whoops. Been working in too many different languages at the same
            time :).

            Comment

            • Jeff

              #7
              Re: simple question about dictionaries

              On Jul 21, 8:14 am, Fredrik Lundh <fred...@python ware.comwrote:
              Jeff wrote:
                throw KeyError('%s not found' % str(val))
              >
              "throw"?  and shouldn't that be a ValueError? ;-)
              >
              </F>
              Whoops. Been working in too many different languages at the same
              time :).

              Comment

              Working...