omz:forum

    • Register
    • Login
    • Search
    • Recent
    • Popular

    Welcome!

    This is the community forum for my apps Pythonista and Editorial.

    For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.


    Trouble with dictionary

    Pythonista
    4
    5
    3019
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • Dann239
      Dann239 last edited by

      I can find plenty of resources to create a dictionary with multiple keys per value, but I can't find how to set one up.
      https://gist.github.com/PyDann/0440454db5a2218f30f2
      I'm just passing a string to check of its existence in mydict. I thought I had it set up right but it fails.

      Thanks for any help! ; )

      1 Reply Last reply Reply Quote 0
      • Sebastian
        Sebastian last edited by

        Do you mean like this?

        # -*- coding: utf-8 -*-
        mydict = {('Sunny', 'Clear') : '☀️', 'Cloudy' : '☁️'}
         
        for key, value in mydict.items():
        	for k in key:
        		mydict[k] = value
        	
        print mydict['Sunny']
        print mydict['Clear']
        
        print mydict['Cloudy']
        
        1 Reply Last reply Reply Quote 0
        • Dann239
          Dann239 last edited by

          So my script takes the weather description out of the daily yahoo forecast and passes it to this function. Instead of writing 100 lines checking 'if description == ...". I figured a dictionary that I could just simple check the string against and return a value from.

          1 Reply Last reply Reply Quote 0
          • ccc
            ccc last edited by

            I updated my comments in the gist... I believe that is a working solution.

            1 Reply Last reply Reply Quote 0
            • henryiii
              henryiii last edited by

              I've added a comment too; modifying the original dict makes lookup faster, and inheritance is a clean solution for the 'smart default dict' function that ccc wrote.

              Note that

              d['one'] = x
              d['two'] = x
              

              will only 'store' one copy of the value x, but if x is immutable, it will be hard to tell that it really is only one copy. (This is just they way Python works)

              1 Reply Last reply Reply Quote 0
              • First post
                Last post
              Powered by NodeBB Forums | Contributors