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.


    List = dictionary

    Pythonista
    dictionary list
    4
    8
    3303
    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.
    • AZOM
      AZOM last edited by

      I’m searching for a way to see if the numbers in a list = the numbers in a dictionary key. EX: dictionary = {12345:6, 01928:7} list = [1, 2, 3]. And the output would be like: the list fit in the key 12345.

      cvp 1 Reply Last reply Reply Quote 0
      • cvp
        cvp @AZOM last edited by cvp

        @AZOM not nice, I agree

        dict = {'12345':6, '01928':7}
        list = [1, 2, 3]
        
        for k in dict.keys():
        	ok = True
        	for e in list:
        		if str(e) not in k:
        			ok = False
        			break
        	if ok:
        		print('list in',k)
        		break 
        
        1 Reply Last reply Reply Quote 0
        • cvp
          cvp last edited by

          Better

          dict = {'12345':6, '1928':7}
          lst = [1, 2, 3]
          
          for k in dict.keys():
          	if  [x for x in lst if str(x) in k] == lst:
          		print('lst in',k)
          		break 
          
          1 Reply Last reply Reply Quote 0
          • pulbrich
            pulbrich last edited by

            dict = {'12345':6, '01928':7}
            list = [1, 2, 3]
            set_list = set(list)
            
            found = []
            for k in dict.keys():
              if set([int(aK) for aK in k]) >= set_list:
                found.append(k)
                
            print(found) 
            
            1 Reply Last reply Reply Quote 0
            • pulbrich
              pulbrich last edited by

              Or, if readability is not much of a concern:

              dict = {'12345':6, '01928':7}
              list = [1, 2, 3]
              
              found = [k for k in dict.keys() if set([int(aK) for aK in k]) >= set(list)]
              print(found) 
              
              cvp 1 Reply Last reply Reply Quote 0
              • cvp
                cvp @pulbrich last edited by

                @pulbrich I like it.

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

                  You almost never need to use dict.keys().

                  dct = {"12345": 6, "01928": 7}
                  lst = [1, 2, 3, 12, 34, 234]
                  print("\n".join(key for key in dct if all(str(i) in key for i in lst)))
                  
                  AZOM 1 Reply Last reply Reply Quote 3
                  • AZOM
                    AZOM @ccc last edited by

                    This post is deleted!
                    1 Reply Last reply Reply Quote 0
                    • First post
                      Last post
                    Powered by NodeBB Forums | Contributors