Create a 'dynamic list' during iteration in python



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















Background



Let there be a set of integers



trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]


Then it is possible to classify them into different equivalence classes modulo 6



Problem



Could we create an algorithm to classify all these integers into their respective equivalence class and store the results in a dictionary in python?



For example



d = "class0": [112,1432,..], "class1": [231,...], ...


More importantly, can we make d changes its size and names of the keys as the integer by which we define equivalence class (in this example, 6) changes?



Progress



It is possible to store all integers of equivalence class 0 modulo 6 in a list. But it is not clear how one can create a 'dynamic' dictionary that adjusts its size and names of the key when the integer in question changes (for example from 6 to 121).



moduloclasszero=
for num in trialinteg:
while num % 6 != 0:

print(f"num is not of class 0")
print(f"But num is of class num % 6")
print("now proceed to restore it to 0")

num = num + (6-(num % 6))
else:
print(f"num is of class 0")
moduloclasszero.append(num)









share|improve this question




























    1















    Background



    Let there be a set of integers



    trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]


    Then it is possible to classify them into different equivalence classes modulo 6



    Problem



    Could we create an algorithm to classify all these integers into their respective equivalence class and store the results in a dictionary in python?



    For example



    d = "class0": [112,1432,..], "class1": [231,...], ...


    More importantly, can we make d changes its size and names of the keys as the integer by which we define equivalence class (in this example, 6) changes?



    Progress



    It is possible to store all integers of equivalence class 0 modulo 6 in a list. But it is not clear how one can create a 'dynamic' dictionary that adjusts its size and names of the key when the integer in question changes (for example from 6 to 121).



    moduloclasszero=
    for num in trialinteg:
    while num % 6 != 0:

    print(f"num is not of class 0")
    print(f"But num is of class num % 6")
    print("now proceed to restore it to 0")

    num = num + (6-(num % 6))
    else:
    print(f"num is of class 0")
    moduloclasszero.append(num)









    share|improve this question
























      1












      1








      1








      Background



      Let there be a set of integers



      trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]


      Then it is possible to classify them into different equivalence classes modulo 6



      Problem



      Could we create an algorithm to classify all these integers into their respective equivalence class and store the results in a dictionary in python?



      For example



      d = "class0": [112,1432,..], "class1": [231,...], ...


      More importantly, can we make d changes its size and names of the keys as the integer by which we define equivalence class (in this example, 6) changes?



      Progress



      It is possible to store all integers of equivalence class 0 modulo 6 in a list. But it is not clear how one can create a 'dynamic' dictionary that adjusts its size and names of the key when the integer in question changes (for example from 6 to 121).



      moduloclasszero=
      for num in trialinteg:
      while num % 6 != 0:

      print(f"num is not of class 0")
      print(f"But num is of class num % 6")
      print("now proceed to restore it to 0")

      num = num + (6-(num % 6))
      else:
      print(f"num is of class 0")
      moduloclasszero.append(num)









      share|improve this question














      Background



      Let there be a set of integers



      trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]


      Then it is possible to classify them into different equivalence classes modulo 6



      Problem



      Could we create an algorithm to classify all these integers into their respective equivalence class and store the results in a dictionary in python?



      For example



      d = "class0": [112,1432,..], "class1": [231,...], ...


      More importantly, can we make d changes its size and names of the keys as the integer by which we define equivalence class (in this example, 6) changes?



      Progress



      It is possible to store all integers of equivalence class 0 modulo 6 in a list. But it is not clear how one can create a 'dynamic' dictionary that adjusts its size and names of the key when the integer in question changes (for example from 6 to 121).



      moduloclasszero=
      for num in trialinteg:
      while num % 6 != 0:

      print(f"num is not of class 0")
      print(f"But num is of class num % 6")
      print("now proceed to restore it to 0")

      num = num + (6-(num % 6))
      else:
      print(f"num is of class 0")
      moduloclasszero.append(num)






      python integer modulo






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 '18 at 17:20









      hephaeshephaes

      324




      324






















          2 Answers
          2






          active

          oldest

          votes


















          3














          You could use collections.defaultdict:



          from collections import defaultdict

          trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]

          d = defaultdict(list)

          for x in trialinteg:
          d[f'classx % 6'].append(x)

          print(d)
          # defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])





          share|improve this answer






























            0














            Use the class value itself for your dictionary key.



            my_mod = 6
            for num in trialinteg:
            d[num % my_mod].append(num)


            I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.



            A dict comprehension can do this in a single assignment statement:



            trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
            d = equi: [i for i in trial if i%my_mod == equi]
            for equi in range(my_mod)


            Resulting value of d:



            0: [7896, 7776, 42342],
            1: [355, 27421],
            2: ,
            3: [231, 5235],
            4: [112, 1432, 2434],
            5:





            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%2f53324803%2fcreate-a-dynamic-list-during-iteration-in-python%23new-answer', 'question_page');

              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes









              3














              You could use collections.defaultdict:



              from collections import defaultdict

              trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]

              d = defaultdict(list)

              for x in trialinteg:
              d[f'classx % 6'].append(x)

              print(d)
              # defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])





              share|improve this answer



























                3














                You could use collections.defaultdict:



                from collections import defaultdict

                trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]

                d = defaultdict(list)

                for x in trialinteg:
                d[f'classx % 6'].append(x)

                print(d)
                # defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])





                share|improve this answer

























                  3












                  3








                  3







                  You could use collections.defaultdict:



                  from collections import defaultdict

                  trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]

                  d = defaultdict(list)

                  for x in trialinteg:
                  d[f'classx % 6'].append(x)

                  print(d)
                  # defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])





                  share|improve this answer













                  You could use collections.defaultdict:



                  from collections import defaultdict

                  trialinteg = [231,355,112,1432,2434,5235,7896,7776,27421,42342]

                  d = defaultdict(list)

                  for x in trialinteg:
                  d[f'classx % 6'].append(x)

                  print(d)
                  # defaultdict(<class 'list'>, 'class3': [231, 5235], 'class1': [355, 27421], 'class4': [112, 1432, 2434], 'class0': [7896, 7776, 42342])






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 17:26









                  AustinAustin

                  13.5k31031




                  13.5k31031























                      0














                      Use the class value itself for your dictionary key.



                      my_mod = 6
                      for num in trialinteg:
                      d[num % my_mod].append(num)


                      I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.



                      A dict comprehension can do this in a single assignment statement:



                      trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
                      d = equi: [i for i in trial if i%my_mod == equi]
                      for equi in range(my_mod)


                      Resulting value of d:



                      0: [7896, 7776, 42342],
                      1: [355, 27421],
                      2: ,
                      3: [231, 5235],
                      4: [112, 1432, 2434],
                      5:





                      share|improve this answer





























                        0














                        Use the class value itself for your dictionary key.



                        my_mod = 6
                        for num in trialinteg:
                        d[num % my_mod].append(num)


                        I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.



                        A dict comprehension can do this in a single assignment statement:



                        trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
                        d = equi: [i for i in trial if i%my_mod == equi]
                        for equi in range(my_mod)


                        Resulting value of d:



                        0: [7896, 7776, 42342],
                        1: [355, 27421],
                        2: ,
                        3: [231, 5235],
                        4: [112, 1432, 2434],
                        5:





                        share|improve this answer



























                          0












                          0








                          0







                          Use the class value itself for your dictionary key.



                          my_mod = 6
                          for num in trialinteg:
                          d[num % my_mod].append(num)


                          I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.



                          A dict comprehension can do this in a single assignment statement:



                          trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
                          d = equi: [i for i in trial if i%my_mod == equi]
                          for equi in range(my_mod)


                          Resulting value of d:



                          0: [7896, 7776, 42342],
                          1: [355, 27421],
                          2: ,
                          3: [231, 5235],
                          4: [112, 1432, 2434],
                          5:





                          share|improve this answer















                          Use the class value itself for your dictionary key.



                          my_mod = 6
                          for num in trialinteg:
                          d[num % my_mod].append(num)


                          I'll assume that you can already handle initializing the dict; if not, look at supporting questions on this site.



                          A dict comprehension can do this in a single assignment statement:



                          trial = [231,355,112,1432,2434,5235,7896,7776,27421,42342]
                          d = equi: [i for i in trial if i%my_mod == equi]
                          for equi in range(my_mod)


                          Resulting value of d:



                          0: [7896, 7776, 42342],
                          1: [355, 27421],
                          2: ,
                          3: [231, 5235],
                          4: [112, 1432, 2434],
                          5:






                          share|improve this answer














                          share|improve this answer



                          share|improve this answer








                          edited Nov 15 '18 at 17:34

























                          answered Nov 15 '18 at 17:25









                          PrunePrune

                          46.2k143759




                          46.2k143759



























                              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%2f53324803%2fcreate-a-dynamic-list-during-iteration-in-python%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

                              Darth Vader #20

                              How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

                              Ondo