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
python-3.x printing
add a comment |
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
python-3.x printing
add a comment |
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
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 at 1:56
Hi Zengbiao
11
11
add a comment |
add a comment |
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.
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
add a comment |
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))
it works , thank you.
– Hi Zengbiao
Nov 15 at 2:43
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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 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
add a comment |
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
add a comment |
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))
it works , thank you.
– Hi Zengbiao
Nov 15 at 2:43
add a comment |
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))
it works , thank you.
– Hi Zengbiao
Nov 15 at 2:43
add a comment |
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))
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 at 2:07
SuperStormer
274614
274614
it works , thank you.
– Hi Zengbiao
Nov 15 at 2:43
add a comment |
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
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.
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.
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