after i print a list, it just despeared
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
add a comment |
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
add a comment |
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
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
python-3.x printing
asked Nov 15 '18 at 1:56
Hi ZengbiaoHi Zengbiao
11
11
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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.
Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
– Andreas
Nov 15 '18 at 2:46
how to accept both two answers,I accepted one but the other was cancelled by the system.
– Hi Zengbiao
Nov 15 '18 at 2:55
I don't think you can accept more than 1 answers
– Andreas
Nov 15 '18 at 4:19
add a comment |
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))
it works , thank you.
– Hi Zengbiao
Nov 15 '18 at 2:43
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
– Andreas
Nov 15 '18 at 2:46
how to accept both two answers,I accepted one but the other was cancelled by the system.
– Hi Zengbiao
Nov 15 '18 at 2:55
I don't think you can accept more than 1 answers
– Andreas
Nov 15 '18 at 4:19
add a comment |
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.
Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
– Andreas
Nov 15 '18 at 2:46
how to accept both two answers,I accepted one but the other was cancelled by the system.
– Hi Zengbiao
Nov 15 '18 at 2:55
I don't think you can accept more than 1 answers
– Andreas
Nov 15 '18 at 4:19
add a comment |
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.
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.
answered Nov 15 '18 at 2:08
AndreasAndreas
2,05431423
2,05431423
Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
– Andreas
Nov 15 '18 at 2:46
how to accept both two answers,I accepted one but the other was cancelled by the system.
– Hi Zengbiao
Nov 15 '18 at 2:55
I don't think you can accept more than 1 answers
– Andreas
Nov 15 '18 at 4:19
add a comment |
Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
– Andreas
Nov 15 '18 at 2:46
how to accept both two answers,I accepted one but the other was cancelled by the system.
– Hi Zengbiao
Nov 15 '18 at 2:55
I don't think you can accept more than 1 answers
– Andreas
Nov 15 '18 at 4:19
Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
– Andreas
Nov 15 '18 at 2:46
Please remember to accept the answer that suits you the most, so that StackOverflow can properly archive it.
– Andreas
Nov 15 '18 at 2:46
how to accept both two answers,I accepted one but the other was cancelled by the system.
– Hi Zengbiao
Nov 15 '18 at 2:55
how to accept both two answers,I accepted one but the other was cancelled by the system.
– Hi Zengbiao
Nov 15 '18 at 2:55
I don't think you can accept more than 1 answers
– Andreas
Nov 15 '18 at 4:19
I don't think you can accept more than 1 answers
– Andreas
Nov 15 '18 at 4:19
add a comment |
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))
it works , thank you.
– Hi Zengbiao
Nov 15 '18 at 2:43
add a comment |
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))
it works , thank you.
– Hi Zengbiao
Nov 15 '18 at 2:43
add a comment |
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))
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))
answered Nov 15 '18 at 2:07
SuperStormerSuperStormer
272614
272614
it works , thank you.
– Hi Zengbiao
Nov 15 '18 at 2:43
add a comment |
it works , thank you.
– Hi Zengbiao
Nov 15 '18 at 2:43
it works , thank you.
– Hi Zengbiao
Nov 15 '18 at 2:43
it works , thank you.
– Hi Zengbiao
Nov 15 '18 at 2:43
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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