after i print a list, it just despeared











up vote
-2
down vote

favorite












In the following code, first create a list collection in the loadDataSet () function, and then use the map function to convert it into a set D ,after that,it can only print once and become enpty.Does anyone know what is going on here? thank you.



def loadDataSet():
return [ [ 1, 3, 4 ], [ 2, 3, 5 ], [ 1, 2, 3, 5 ], [ 2, 5 ] ]

if __name__ == '__main__':
myDat = loadDataSet()
D = map( set, myDat )
print("first print: ",list(D))
print("second print: ",list(D))
print("len of D: ",len(list(D)))


i use python 3.5.2 and the output is :



first print:     [{1, 3, 4}, {2, 3, 5}, {1, 2, 3, 5}, {2, 5}]
second print:
len of D: 0









share|improve this question


























    up vote
    -2
    down vote

    favorite












    In the following code, first create a list collection in the loadDataSet () function, and then use the map function to convert it into a set D ,after that,it can only print once and become enpty.Does anyone know what is going on here? thank you.



    def loadDataSet():
    return [ [ 1, 3, 4 ], [ 2, 3, 5 ], [ 1, 2, 3, 5 ], [ 2, 5 ] ]

    if __name__ == '__main__':
    myDat = loadDataSet()
    D = map( set, myDat )
    print("first print: ",list(D))
    print("second print: ",list(D))
    print("len of D: ",len(list(D)))


    i use python 3.5.2 and the output is :



    first print:     [{1, 3, 4}, {2, 3, 5}, {1, 2, 3, 5}, {2, 5}]
    second print:
    len of D: 0









    share|improve this question
























      up vote
      -2
      down vote

      favorite









      up vote
      -2
      down vote

      favorite











      In the following code, first create a list collection in the loadDataSet () function, and then use the map function to convert it into a set D ,after that,it can only print once and become enpty.Does anyone know what is going on here? thank you.



      def loadDataSet():
      return [ [ 1, 3, 4 ], [ 2, 3, 5 ], [ 1, 2, 3, 5 ], [ 2, 5 ] ]

      if __name__ == '__main__':
      myDat = loadDataSet()
      D = map( set, myDat )
      print("first print: ",list(D))
      print("second print: ",list(D))
      print("len of D: ",len(list(D)))


      i use python 3.5.2 and the output is :



      first print:     [{1, 3, 4}, {2, 3, 5}, {1, 2, 3, 5}, {2, 5}]
      second print:
      len of D: 0









      share|improve this question













      In the following code, first create a list collection in the loadDataSet () function, and then use the map function to convert it into a set D ,after that,it can only print once and become enpty.Does anyone know what is going on here? thank you.



      def loadDataSet():
      return [ [ 1, 3, 4 ], [ 2, 3, 5 ], [ 1, 2, 3, 5 ], [ 2, 5 ] ]

      if __name__ == '__main__':
      myDat = loadDataSet()
      D = map( set, myDat )
      print("first print: ",list(D))
      print("second print: ",list(D))
      print("len of D: ",len(list(D)))


      i use python 3.5.2 and the output is :



      first print:     [{1, 3, 4}, {2, 3, 5}, {1, 2, 3, 5}, {2, 5}]
      second print:
      len of D: 0






      python-3.x printing






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 15 at 1:56









      Hi Zengbiao

      11




      11
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Welcome to StackOverflow!



          The reason is because of the behavior of map() function itself. It returns a generator, which can only be consumed once. This means, map can only execute the function to the list of given inputs once and return the resulting object. After that, the generator is exhausted and cannot be used to generate the same result. Therefore, the practice is to save the return value in a variable if you are going to use it more than once.






          share|improve this answer





















          • Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
            – Andreas
            Nov 15 at 2:46










          • how to accept both two answers,I accepted one but the other was cancelled by the system.
            – Hi Zengbiao
            Nov 15 at 2:55










          • I don't think you can accept more than 1 answers
            – Andreas
            Nov 15 at 4:19


















          up vote
          1
          down vote













          map creates an iterator, that can only be iterated once. The second time you call list, D is already empty since it has been iterated through already.
          If you want to iterate through it multiple times, do D=list(map(set, myDat))






          share|improve this answer





















          • it works , thank you.
            – Hi Zengbiao
            Nov 15 at 2:43











          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',
          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%2f53311345%2fafter-i-print-a-list-it-just-despeared%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








          up vote
          0
          down vote



          accepted










          Welcome to StackOverflow!



          The reason is because of the behavior of map() function itself. It returns a generator, which can only be consumed once. This means, map can only execute the function to the list of given inputs once and return the resulting object. After that, the generator is exhausted and cannot be used to generate the same result. Therefore, the practice is to save the return value in a variable if you are going to use it more than once.






          share|improve this answer





















          • Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
            – Andreas
            Nov 15 at 2:46










          • how to accept both two answers,I accepted one but the other was cancelled by the system.
            – Hi Zengbiao
            Nov 15 at 2:55










          • I don't think you can accept more than 1 answers
            – Andreas
            Nov 15 at 4:19















          up vote
          0
          down vote



          accepted










          Welcome to StackOverflow!



          The reason is because of the behavior of map() function itself. It returns a generator, which can only be consumed once. This means, map can only execute the function to the list of given inputs once and return the resulting object. After that, the generator is exhausted and cannot be used to generate the same result. Therefore, the practice is to save the return value in a variable if you are going to use it more than once.






          share|improve this answer





















          • Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
            – Andreas
            Nov 15 at 2:46










          • how to accept both two answers,I accepted one but the other was cancelled by the system.
            – Hi Zengbiao
            Nov 15 at 2:55










          • I don't think you can accept more than 1 answers
            – Andreas
            Nov 15 at 4:19













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Welcome to StackOverflow!



          The reason is because of the behavior of map() function itself. It returns a generator, which can only be consumed once. This means, map can only execute the function to the list of given inputs once and return the resulting object. After that, the generator is exhausted and cannot be used to generate the same result. Therefore, the practice is to save the return value in a variable if you are going to use it more than once.






          share|improve this answer












          Welcome to StackOverflow!



          The reason is because of the behavior of map() function itself. It returns a generator, which can only be consumed once. This means, map can only execute the function to the list of given inputs once and return the resulting object. After that, the generator is exhausted and cannot be used to generate the same result. Therefore, the practice is to save the return value in a variable if you are going to use it more than once.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 at 2:08









          Andreas

          1,7761618




          1,7761618












          • Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
            – Andreas
            Nov 15 at 2:46










          • how to accept both two answers,I accepted one but the other was cancelled by the system.
            – Hi Zengbiao
            Nov 15 at 2:55










          • I don't think you can accept more than 1 answers
            – Andreas
            Nov 15 at 4:19


















          • Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
            – Andreas
            Nov 15 at 2:46










          • how to accept both two answers,I accepted one but the other was cancelled by the system.
            – Hi Zengbiao
            Nov 15 at 2:55










          • I don't think you can accept more than 1 answers
            – Andreas
            Nov 15 at 4:19
















          Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
          – Andreas
          Nov 15 at 2:46




          Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
          – Andreas
          Nov 15 at 2:46












          how to accept both two answers,I accepted one but the other was cancelled by the system.
          – Hi Zengbiao
          Nov 15 at 2:55




          how to accept both two answers,I accepted one but the other was cancelled by the system.
          – Hi Zengbiao
          Nov 15 at 2:55












          I don't think you can accept more than 1 answers
          – Andreas
          Nov 15 at 4:19




          I don't think you can accept more than 1 answers
          – Andreas
          Nov 15 at 4:19












          up vote
          1
          down vote













          map creates an iterator, that can only be iterated once. The second time you call list, D is already empty since it has been iterated through already.
          If you want to iterate through it multiple times, do D=list(map(set, myDat))






          share|improve this answer





















          • it works , thank you.
            – Hi Zengbiao
            Nov 15 at 2:43















          up vote
          1
          down vote













          map creates an iterator, that can only be iterated once. The second time you call list, D is already empty since it has been iterated through already.
          If you want to iterate through it multiple times, do D=list(map(set, myDat))






          share|improve this answer





















          • it works , thank you.
            – Hi Zengbiao
            Nov 15 at 2:43













          up vote
          1
          down vote










          up vote
          1
          down vote









          map creates an iterator, that can only be iterated once. The second time you call list, D is already empty since it has been iterated through already.
          If you want to iterate through it multiple times, do D=list(map(set, myDat))






          share|improve this answer












          map creates an iterator, that can only be iterated once. The second time you call list, D is already empty since it has been iterated through already.
          If you want to iterate through it multiple times, do D=list(map(set, myDat))







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 15 at 2:07









          SuperStormer

          274614




          274614












          • it works , thank you.
            – Hi Zengbiao
            Nov 15 at 2:43


















          • it works , thank you.
            – Hi Zengbiao
            Nov 15 at 2:43
















          it works , thank you.
          – Hi Zengbiao
          Nov 15 at 2:43




          it works , thank you.
          – Hi Zengbiao
          Nov 15 at 2:43


















          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.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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%2f53311345%2fafter-i-print-a-list-it-just-despeared%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

          How to change which sound is reproduced for terminal bell?

          Can I use Tabulator js library in my java Spring + Thymeleaf project?

          Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents