How to create a dictionary to reverse lookup list items that each have sets?How do I sort a list of dictionaries by a value of the dictionary?How to randomly select an item from a list?How to remove items from a list while iterating?Create a dictionary with list comprehensionHow can I count the occurrences of a list item?Create List of Single Item Repeated n Times in PythonIn Python, when to use a Dictionary, List or Set?How can I reverse a list in Python?Convert Set to List without creating new ListComparing list values between two dictionaries that have no matching keys

Why can't I get the argument count of a template function at compile-time?

Break down the phrase "shitsurei shinakereba naranaindesu"

What is the motivation behind designing a control stick that does not move?

New coworker has strange workplace requirements - how should I deal with them?

Sum and average calculator

Welche normative Autorität hat der Duden? / What's the normative authority of the Duden?

How do I get my neighbour to stop disturbing with loud music?

I was given someone else's visa, stamped in my passport

LINQ Extension methods MinBy and MaxBy

IList<T> implementation

How to Calculate this definite integral or how to solve this series?

How many possible file types in the output `ls -l` command?

What checks exist against overuse of presidential pardons in the USA?

How to load files as a quickfix window at start-up

How to run a command 1 out of N times in Bash

How does the search space affect the speed of an ILP solver?

Is it good practice to speed up and slow down where not written in a song?

Should a TA point out a professor's mistake while attending their lecture?

Is it possible to use pgfplots in Rmarkdown rendered as HTML?

Can the inductive kick be discharged without a freewheeling diode, in this example?

How can I store milk for long periods of time?

Who declared the Last Alliance to be the "last" and why?

Four day weekend?

Does FERPA require parental notification of disability assessment?



How to create a dictionary to reverse lookup list items that each have sets?


How do I sort a list of dictionaries by a value of the dictionary?How to randomly select an item from a list?How to remove items from a list while iterating?Create a dictionary with list comprehensionHow can I count the occurrences of a list item?Create List of Single Item Repeated n Times in PythonIn Python, when to use a Dictionary, List or Set?How can I reverse a list in Python?Convert Set to List without creating new ListComparing list values between two dictionaries that have no matching keys






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








1















I have test names in a list like this in Python v2.7:



all_tests = ['test1', 'test2', 'test3', ..., 'testN']


And for each test name, I can lookup a set of properties that the test has:



series['test'] = 'prop1', 'prop2', 'prop3', ..., 'propN'


E.g.,



series['test1'] = 'yellow', 'blue', 'orange'
series['test2'] = 'blue', 'red', 'black'
series['test3'] = 'yellow', 'green', 'black'


Now, I wish to reverse the direction and create a dictionary from properties to the set of tests that have that property.



So for the above example three tests, I'd like to create this dictionary:



result = 
'yellow': 'test1', 'test3',
'blue': 'test1', 'test2',
'orange': 'test1',
'red': 'test2',
'black': 'test2', 'test3'
'green': 'test3'



I imagine there is a way with list comprehension to build the dictionary, but I'm unclear how to do so. I'm thinking something along these lines:



tuple( series[test], test ) for test in all_tests


But don't know how to put that into the result dictionary and have the set of dictionary values for each key, keep getting added to.










share|improve this question
































    1















    I have test names in a list like this in Python v2.7:



    all_tests = ['test1', 'test2', 'test3', ..., 'testN']


    And for each test name, I can lookup a set of properties that the test has:



    series['test'] = 'prop1', 'prop2', 'prop3', ..., 'propN'


    E.g.,



    series['test1'] = 'yellow', 'blue', 'orange'
    series['test2'] = 'blue', 'red', 'black'
    series['test3'] = 'yellow', 'green', 'black'


    Now, I wish to reverse the direction and create a dictionary from properties to the set of tests that have that property.



    So for the above example three tests, I'd like to create this dictionary:



    result = 
    'yellow': 'test1', 'test3',
    'blue': 'test1', 'test2',
    'orange': 'test1',
    'red': 'test2',
    'black': 'test2', 'test3'
    'green': 'test3'



    I imagine there is a way with list comprehension to build the dictionary, but I'm unclear how to do so. I'm thinking something along these lines:



    tuple( series[test], test ) for test in all_tests


    But don't know how to put that into the result dictionary and have the set of dictionary values for each key, keep getting added to.










    share|improve this question




























      1












      1








      1








      I have test names in a list like this in Python v2.7:



      all_tests = ['test1', 'test2', 'test3', ..., 'testN']


      And for each test name, I can lookup a set of properties that the test has:



      series['test'] = 'prop1', 'prop2', 'prop3', ..., 'propN'


      E.g.,



      series['test1'] = 'yellow', 'blue', 'orange'
      series['test2'] = 'blue', 'red', 'black'
      series['test3'] = 'yellow', 'green', 'black'


      Now, I wish to reverse the direction and create a dictionary from properties to the set of tests that have that property.



      So for the above example three tests, I'd like to create this dictionary:



      result = 
      'yellow': 'test1', 'test3',
      'blue': 'test1', 'test2',
      'orange': 'test1',
      'red': 'test2',
      'black': 'test2', 'test3'
      'green': 'test3'



      I imagine there is a way with list comprehension to build the dictionary, but I'm unclear how to do so. I'm thinking something along these lines:



      tuple( series[test], test ) for test in all_tests


      But don't know how to put that into the result dictionary and have the set of dictionary values for each key, keep getting added to.










      share|improve this question
















      I have test names in a list like this in Python v2.7:



      all_tests = ['test1', 'test2', 'test3', ..., 'testN']


      And for each test name, I can lookup a set of properties that the test has:



      series['test'] = 'prop1', 'prop2', 'prop3', ..., 'propN'


      E.g.,



      series['test1'] = 'yellow', 'blue', 'orange'
      series['test2'] = 'blue', 'red', 'black'
      series['test3'] = 'yellow', 'green', 'black'


      Now, I wish to reverse the direction and create a dictionary from properties to the set of tests that have that property.



      So for the above example three tests, I'd like to create this dictionary:



      result = 
      'yellow': 'test1', 'test3',
      'blue': 'test1', 'test2',
      'orange': 'test1',
      'red': 'test2',
      'black': 'test2', 'test3'
      'green': 'test3'



      I imagine there is a way with list comprehension to build the dictionary, but I'm unclear how to do so. I'm thinking something along these lines:



      tuple( series[test], test ) for test in all_tests


      But don't know how to put that into the result dictionary and have the set of dictionary values for each key, keep getting added to.







      python python-2.7 dictionary set list-comprehension






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 27 at 22:04









      martineau

      75.2k11 gold badges103 silver badges198 bronze badges




      75.2k11 gold badges103 silver badges198 bronze badges










      asked Mar 27 at 21:58









      WilliamKFWilliamKF

      16.3k51 gold badges155 silver badges253 bronze badges




      16.3k51 gold badges155 silver badges253 bronze badges

























          4 Answers
          4






          active

          oldest

          votes


















          2















          A straightforward looping solution using collections.defaultdict



          from collections import defaultdict
          d = defaultdict(set)
          for k,v in series.items():
          for prop in v:
          d[prop].add(k)
          print(d)
          #defaultdict(set,
          # 'black': 'test2', 'test3',
          # 'blue': 'test1', 'test2',
          # 'green': 'test3',
          # 'orange': 'test1',
          # 'red': 'test2',
          # 'yellow': 'test1', 'test3')



          Another approach using pandas



          First convert series into a DataFrame:



          import pandas as pd
          df = pd.DataFrame(k: list(v) for k, v in series.items())
          print(df)
          # test1 test2 test3
          #0 blue blue black
          #1 orange black green
          #2 yellow red yellow


          Next melt the DataFrame, groupby the value and use set as the aggregate function:



          print(pd.melt(df).groupby("value", as_index=True).agg(set))
          # variable
          #value
          #black test3, test2
          #blue test1, test2
          #green test3
          #orange test1
          #red test2
          #yellow test1, test3


          Finally to go back to a dictionary, call to_records() and apply the dict constructor:



          print(dict(pd.melt(df).groupby("value", as_index=True).agg(set).to_records()))
          #'black': 'test2', 'test3',
          # 'blue': 'test1', 'test2',
          # 'green': 'test3',
          # 'orange': 'test1',
          # 'red': 'test2',
          # 'yellow': 'test1', 'test3'





          share|improve this answer


































            1















            Here's an alternative that creates a regular dictionary:



            series = 
            'test1': 'yellow', 'blue', 'orange',
            'test2': 'blue', 'red', 'black',
            'test3': 'yellow', 'green', 'black',


            result =
            for name, props in series.items():
            for prop in props:
            result.setdefault(prop, set()).add(name)

            from pprint import pprint
            pprint(result)


            Output:



            'black': set(['test2', 'test3']),
            'blue': set(['test1', 'test2']),
            'green': set(['test3']),
            'orange': set(['test1']),
            'red': set(['test2']),
            'yellow': set(['test1', 'test3'])


            Another alternative to get same result:



            from itertools import chain

            properties = set(chain.from_iterable(series.values())) # All possible.
            result = prop: name for name, props in series.items() if prop in props
            for prop in properties

            from pprint import pprint
            pprint(result)





            share|improve this answer


































              0















              Do you already have a set or any iterable containing all properties, e.g. all_props?

              Then your reverse dict could be created with



              rev_series = p: k for k, v in series.items() if p in v for p in all_props


              If not:



              all_props = set()
              for s in series.values():
              all_props = all_props.union(s)





              share|improve this answer
































                0















                Build a set of all the available colors. Then, for each color create a set of the series entries that contain that color:



                colors = set( color for colors in series.values() for color in colors ) 
                result = color:set(test for test in series if color in series[test]) for color in colors
                print(result)
                # 'green': 'test3', 'red': 'test2', 'black': 'test2', 'test3', 'yellow': 'test1', 'test3', 'orange': 'test1', 'blue': 'test1', 'test2'





                share|improve this answer





























                  Your Answer






                  StackExchange.ifUsing("editor", function ()
                  StackExchange.using("externalEditor", function ()
                  StackExchange.using("snippets", function ()
                  StackExchange.snippets.init();
                  );
                  );
                  , "code-snippets");

                  StackExchange.ready(function()
                  var channelOptions =
                  tags: "".split(" "),
                  id: "1"
                  ;
                  initTagRenderer("".split(" "), "".split(" "), channelOptions);

                  StackExchange.using("externalEditor", function()
                  // Have to fire editor after snippets, if snippets enabled
                  if (StackExchange.settings.snippets.snippetsEnabled)
                  StackExchange.using("snippets", function()
                  createEditor();
                  );

                  else
                  createEditor();

                  );

                  function createEditor()
                  StackExchange.prepareEditor(
                  heartbeatType: 'answer',
                  autoActivateHeartbeat: false,
                  convertImagesToLinks: true,
                  noModals: true,
                  showLowRepImageUploadWarning: true,
                  reputationToPostImages: 10,
                  bindNavPrevention: true,
                  postfix: "",
                  imageUploader:
                  brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                  contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                  allowUrls: true
                  ,
                  onDemand: true,
                  discardSelector: ".discard-answer"
                  ,immediatelyShowMarkdownHelp:true
                  );



                  );













                  draft saved

                  draft discarded


















                  StackExchange.ready(
                  function ()
                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387108%2fhow-to-create-a-dictionary-to-reverse-lookup-list-items-that-each-have-sets%23new-answer', 'question_page');

                  );

                  Post as a guest















                  Required, but never shown

























                  4 Answers
                  4






                  active

                  oldest

                  votes








                  4 Answers
                  4






                  active

                  oldest

                  votes









                  active

                  oldest

                  votes






                  active

                  oldest

                  votes









                  2















                  A straightforward looping solution using collections.defaultdict



                  from collections import defaultdict
                  d = defaultdict(set)
                  for k,v in series.items():
                  for prop in v:
                  d[prop].add(k)
                  print(d)
                  #defaultdict(set,
                  # 'black': 'test2', 'test3',
                  # 'blue': 'test1', 'test2',
                  # 'green': 'test3',
                  # 'orange': 'test1',
                  # 'red': 'test2',
                  # 'yellow': 'test1', 'test3')



                  Another approach using pandas



                  First convert series into a DataFrame:



                  import pandas as pd
                  df = pd.DataFrame(k: list(v) for k, v in series.items())
                  print(df)
                  # test1 test2 test3
                  #0 blue blue black
                  #1 orange black green
                  #2 yellow red yellow


                  Next melt the DataFrame, groupby the value and use set as the aggregate function:



                  print(pd.melt(df).groupby("value", as_index=True).agg(set))
                  # variable
                  #value
                  #black test3, test2
                  #blue test1, test2
                  #green test3
                  #orange test1
                  #red test2
                  #yellow test1, test3


                  Finally to go back to a dictionary, call to_records() and apply the dict constructor:



                  print(dict(pd.melt(df).groupby("value", as_index=True).agg(set).to_records()))
                  #'black': 'test2', 'test3',
                  # 'blue': 'test1', 'test2',
                  # 'green': 'test3',
                  # 'orange': 'test1',
                  # 'red': 'test2',
                  # 'yellow': 'test1', 'test3'





                  share|improve this answer































                    2















                    A straightforward looping solution using collections.defaultdict



                    from collections import defaultdict
                    d = defaultdict(set)
                    for k,v in series.items():
                    for prop in v:
                    d[prop].add(k)
                    print(d)
                    #defaultdict(set,
                    # 'black': 'test2', 'test3',
                    # 'blue': 'test1', 'test2',
                    # 'green': 'test3',
                    # 'orange': 'test1',
                    # 'red': 'test2',
                    # 'yellow': 'test1', 'test3')



                    Another approach using pandas



                    First convert series into a DataFrame:



                    import pandas as pd
                    df = pd.DataFrame(k: list(v) for k, v in series.items())
                    print(df)
                    # test1 test2 test3
                    #0 blue blue black
                    #1 orange black green
                    #2 yellow red yellow


                    Next melt the DataFrame, groupby the value and use set as the aggregate function:



                    print(pd.melt(df).groupby("value", as_index=True).agg(set))
                    # variable
                    #value
                    #black test3, test2
                    #blue test1, test2
                    #green test3
                    #orange test1
                    #red test2
                    #yellow test1, test3


                    Finally to go back to a dictionary, call to_records() and apply the dict constructor:



                    print(dict(pd.melt(df).groupby("value", as_index=True).agg(set).to_records()))
                    #'black': 'test2', 'test3',
                    # 'blue': 'test1', 'test2',
                    # 'green': 'test3',
                    # 'orange': 'test1',
                    # 'red': 'test2',
                    # 'yellow': 'test1', 'test3'





                    share|improve this answer





























                      2














                      2










                      2









                      A straightforward looping solution using collections.defaultdict



                      from collections import defaultdict
                      d = defaultdict(set)
                      for k,v in series.items():
                      for prop in v:
                      d[prop].add(k)
                      print(d)
                      #defaultdict(set,
                      # 'black': 'test2', 'test3',
                      # 'blue': 'test1', 'test2',
                      # 'green': 'test3',
                      # 'orange': 'test1',
                      # 'red': 'test2',
                      # 'yellow': 'test1', 'test3')



                      Another approach using pandas



                      First convert series into a DataFrame:



                      import pandas as pd
                      df = pd.DataFrame(k: list(v) for k, v in series.items())
                      print(df)
                      # test1 test2 test3
                      #0 blue blue black
                      #1 orange black green
                      #2 yellow red yellow


                      Next melt the DataFrame, groupby the value and use set as the aggregate function:



                      print(pd.melt(df).groupby("value", as_index=True).agg(set))
                      # variable
                      #value
                      #black test3, test2
                      #blue test1, test2
                      #green test3
                      #orange test1
                      #red test2
                      #yellow test1, test3


                      Finally to go back to a dictionary, call to_records() and apply the dict constructor:



                      print(dict(pd.melt(df).groupby("value", as_index=True).agg(set).to_records()))
                      #'black': 'test2', 'test3',
                      # 'blue': 'test1', 'test2',
                      # 'green': 'test3',
                      # 'orange': 'test1',
                      # 'red': 'test2',
                      # 'yellow': 'test1', 'test3'





                      share|improve this answer















                      A straightforward looping solution using collections.defaultdict



                      from collections import defaultdict
                      d = defaultdict(set)
                      for k,v in series.items():
                      for prop in v:
                      d[prop].add(k)
                      print(d)
                      #defaultdict(set,
                      # 'black': 'test2', 'test3',
                      # 'blue': 'test1', 'test2',
                      # 'green': 'test3',
                      # 'orange': 'test1',
                      # 'red': 'test2',
                      # 'yellow': 'test1', 'test3')



                      Another approach using pandas



                      First convert series into a DataFrame:



                      import pandas as pd
                      df = pd.DataFrame(k: list(v) for k, v in series.items())
                      print(df)
                      # test1 test2 test3
                      #0 blue blue black
                      #1 orange black green
                      #2 yellow red yellow


                      Next melt the DataFrame, groupby the value and use set as the aggregate function:



                      print(pd.melt(df).groupby("value", as_index=True).agg(set))
                      # variable
                      #value
                      #black test3, test2
                      #blue test1, test2
                      #green test3
                      #orange test1
                      #red test2
                      #yellow test1, test3


                      Finally to go back to a dictionary, call to_records() and apply the dict constructor:



                      print(dict(pd.melt(df).groupby("value", as_index=True).agg(set).to_records()))
                      #'black': 'test2', 'test3',
                      # 'blue': 'test1', 'test2',
                      # 'green': 'test3',
                      # 'orange': 'test1',
                      # 'red': 'test2',
                      # 'yellow': 'test1', 'test3'






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 27 at 22:40

























                      answered Mar 27 at 22:06









                      paultpault

                      21.1k4 gold badges36 silver badges60 bronze badges




                      21.1k4 gold badges36 silver badges60 bronze badges


























                          1















                          Here's an alternative that creates a regular dictionary:



                          series = 
                          'test1': 'yellow', 'blue', 'orange',
                          'test2': 'blue', 'red', 'black',
                          'test3': 'yellow', 'green', 'black',


                          result =
                          for name, props in series.items():
                          for prop in props:
                          result.setdefault(prop, set()).add(name)

                          from pprint import pprint
                          pprint(result)


                          Output:



                          'black': set(['test2', 'test3']),
                          'blue': set(['test1', 'test2']),
                          'green': set(['test3']),
                          'orange': set(['test1']),
                          'red': set(['test2']),
                          'yellow': set(['test1', 'test3'])


                          Another alternative to get same result:



                          from itertools import chain

                          properties = set(chain.from_iterable(series.values())) # All possible.
                          result = prop: name for name, props in series.items() if prop in props
                          for prop in properties

                          from pprint import pprint
                          pprint(result)





                          share|improve this answer































                            1















                            Here's an alternative that creates a regular dictionary:



                            series = 
                            'test1': 'yellow', 'blue', 'orange',
                            'test2': 'blue', 'red', 'black',
                            'test3': 'yellow', 'green', 'black',


                            result =
                            for name, props in series.items():
                            for prop in props:
                            result.setdefault(prop, set()).add(name)

                            from pprint import pprint
                            pprint(result)


                            Output:



                            'black': set(['test2', 'test3']),
                            'blue': set(['test1', 'test2']),
                            'green': set(['test3']),
                            'orange': set(['test1']),
                            'red': set(['test2']),
                            'yellow': set(['test1', 'test3'])


                            Another alternative to get same result:



                            from itertools import chain

                            properties = set(chain.from_iterable(series.values())) # All possible.
                            result = prop: name for name, props in series.items() if prop in props
                            for prop in properties

                            from pprint import pprint
                            pprint(result)





                            share|improve this answer





























                              1














                              1










                              1









                              Here's an alternative that creates a regular dictionary:



                              series = 
                              'test1': 'yellow', 'blue', 'orange',
                              'test2': 'blue', 'red', 'black',
                              'test3': 'yellow', 'green', 'black',


                              result =
                              for name, props in series.items():
                              for prop in props:
                              result.setdefault(prop, set()).add(name)

                              from pprint import pprint
                              pprint(result)


                              Output:



                              'black': set(['test2', 'test3']),
                              'blue': set(['test1', 'test2']),
                              'green': set(['test3']),
                              'orange': set(['test1']),
                              'red': set(['test2']),
                              'yellow': set(['test1', 'test3'])


                              Another alternative to get same result:



                              from itertools import chain

                              properties = set(chain.from_iterable(series.values())) # All possible.
                              result = prop: name for name, props in series.items() if prop in props
                              for prop in properties

                              from pprint import pprint
                              pprint(result)





                              share|improve this answer















                              Here's an alternative that creates a regular dictionary:



                              series = 
                              'test1': 'yellow', 'blue', 'orange',
                              'test2': 'blue', 'red', 'black',
                              'test3': 'yellow', 'green', 'black',


                              result =
                              for name, props in series.items():
                              for prop in props:
                              result.setdefault(prop, set()).add(name)

                              from pprint import pprint
                              pprint(result)


                              Output:



                              'black': set(['test2', 'test3']),
                              'blue': set(['test1', 'test2']),
                              'green': set(['test3']),
                              'orange': set(['test1']),
                              'red': set(['test2']),
                              'yellow': set(['test1', 'test3'])


                              Another alternative to get same result:



                              from itertools import chain

                              properties = set(chain.from_iterable(series.values())) # All possible.
                              result = prop: name for name, props in series.items() if prop in props
                              for prop in properties

                              from pprint import pprint
                              pprint(result)






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Mar 27 at 23:34

























                              answered Mar 27 at 22:56









                              martineaumartineau

                              75.2k11 gold badges103 silver badges198 bronze badges




                              75.2k11 gold badges103 silver badges198 bronze badges
























                                  0















                                  Do you already have a set or any iterable containing all properties, e.g. all_props?

                                  Then your reverse dict could be created with



                                  rev_series = p: k for k, v in series.items() if p in v for p in all_props


                                  If not:



                                  all_props = set()
                                  for s in series.values():
                                  all_props = all_props.union(s)





                                  share|improve this answer





























                                    0















                                    Do you already have a set or any iterable containing all properties, e.g. all_props?

                                    Then your reverse dict could be created with



                                    rev_series = p: k for k, v in series.items() if p in v for p in all_props


                                    If not:



                                    all_props = set()
                                    for s in series.values():
                                    all_props = all_props.union(s)





                                    share|improve this answer



























                                      0














                                      0










                                      0









                                      Do you already have a set or any iterable containing all properties, e.g. all_props?

                                      Then your reverse dict could be created with



                                      rev_series = p: k for k, v in series.items() if p in v for p in all_props


                                      If not:



                                      all_props = set()
                                      for s in series.values():
                                      all_props = all_props.union(s)





                                      share|improve this answer













                                      Do you already have a set or any iterable containing all properties, e.g. all_props?

                                      Then your reverse dict could be created with



                                      rev_series = p: k for k, v in series.items() if p in v for p in all_props


                                      If not:



                                      all_props = set()
                                      for s in series.values():
                                      all_props = all_props.union(s)






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 27 at 23:15









                                      SpghttCdSpghttCd

                                      6,2552 gold badges5 silver badges17 bronze badges




                                      6,2552 gold badges5 silver badges17 bronze badges
























                                          0















                                          Build a set of all the available colors. Then, for each color create a set of the series entries that contain that color:



                                          colors = set( color for colors in series.values() for color in colors ) 
                                          result = color:set(test for test in series if color in series[test]) for color in colors
                                          print(result)
                                          # 'green': 'test3', 'red': 'test2', 'black': 'test2', 'test3', 'yellow': 'test1', 'test3', 'orange': 'test1', 'blue': 'test1', 'test2'





                                          share|improve this answer































                                            0















                                            Build a set of all the available colors. Then, for each color create a set of the series entries that contain that color:



                                            colors = set( color for colors in series.values() for color in colors ) 
                                            result = color:set(test for test in series if color in series[test]) for color in colors
                                            print(result)
                                            # 'green': 'test3', 'red': 'test2', 'black': 'test2', 'test3', 'yellow': 'test1', 'test3', 'orange': 'test1', 'blue': 'test1', 'test2'





                                            share|improve this answer





























                                              0














                                              0










                                              0









                                              Build a set of all the available colors. Then, for each color create a set of the series entries that contain that color:



                                              colors = set( color for colors in series.values() for color in colors ) 
                                              result = color:set(test for test in series if color in series[test]) for color in colors
                                              print(result)
                                              # 'green': 'test3', 'red': 'test2', 'black': 'test2', 'test3', 'yellow': 'test1', 'test3', 'orange': 'test1', 'blue': 'test1', 'test2'





                                              share|improve this answer















                                              Build a set of all the available colors. Then, for each color create a set of the series entries that contain that color:



                                              colors = set( color for colors in series.values() for color in colors ) 
                                              result = color:set(test for test in series if color in series[test]) for color in colors
                                              print(result)
                                              # 'green': 'test3', 'red': 'test2', 'black': 'test2', 'test3', 'yellow': 'test1', 'test3', 'orange': 'test1', 'blue': 'test1', 'test2'






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited Mar 28 at 0:10

























                                              answered Mar 28 at 0:05









                                              Alain T.Alain T.

                                              11.8k1 gold badge16 silver badges34 bronze badges




                                              11.8k1 gold badge16 silver badges34 bronze badges






























                                                  draft saved

                                                  draft discarded
















































                                                  Thanks for contributing an answer to Stack Overflow!


                                                  • Please be sure to answer the question. Provide details and share your research!

                                                  But avoid


                                                  • Asking for help, clarification, or responding to other answers.

                                                  • Making statements based on opinion; back them up with references or personal experience.

                                                  To learn more, see our tips on writing great answers.




                                                  draft saved


                                                  draft discarded














                                                  StackExchange.ready(
                                                  function ()
                                                  StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55387108%2fhow-to-create-a-dictionary-to-reverse-lookup-list-items-that-each-have-sets%23new-answer', 'question_page');

                                                  );

                                                  Post as a guest















                                                  Required, but never shown





















































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown

































                                                  Required, but never shown














                                                  Required, but never shown












                                                  Required, but never shown







                                                  Required, but never shown







                                                  Popular posts from this blog

                                                  SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                                                  용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                                                  155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해