Two different lists from a tuple generator
up vote
-2
down vote
favorite
I have a generator, that yields me a tuple and a function that collects information from the generator and creates two separate lists:
labels =
dataset =
for data, label in list_data(folder):
dataset.append(data)
labels.append(label)
return dataset, labels
I'm not sure, if I'm not over-engineering here, but it feels like it's not the most pythonic way to solve it.
I've tried some ideas with list comprehension, but it always resulted in syntax errors.
Or maybe is it the best solution as it ensures readability?
python python3
add a comment |Â
up vote
-2
down vote
favorite
I have a generator, that yields me a tuple and a function that collects information from the generator and creates two separate lists:
labels =
dataset =
for data, label in list_data(folder):
dataset.append(data)
labels.append(label)
return dataset, labels
I'm not sure, if I'm not over-engineering here, but it feels like it's not the most pythonic way to solve it.
I've tried some ideas with list comprehension, but it always resulted in syntax errors.
Or maybe is it the best solution as it ensures readability?
python python3
Yes there are super-clever ways to do it. But, six month from now, will you remember how to maintain those super-clever ways? Your current way is understandable and maintainable - nothing wrong with that.
â user535733
Feb 17 at 0:25
add a comment |Â
up vote
-2
down vote
favorite
up vote
-2
down vote
favorite
I have a generator, that yields me a tuple and a function that collects information from the generator and creates two separate lists:
labels =
dataset =
for data, label in list_data(folder):
dataset.append(data)
labels.append(label)
return dataset, labels
I'm not sure, if I'm not over-engineering here, but it feels like it's not the most pythonic way to solve it.
I've tried some ideas with list comprehension, but it always resulted in syntax errors.
Or maybe is it the best solution as it ensures readability?
python python3
I have a generator, that yields me a tuple and a function that collects information from the generator and creates two separate lists:
labels =
dataset =
for data, label in list_data(folder):
dataset.append(data)
labels.append(label)
return dataset, labels
I'm not sure, if I'm not over-engineering here, but it feels like it's not the most pythonic way to solve it.
I've tried some ideas with list comprehension, but it always resulted in syntax errors.
Or maybe is it the best solution as it ensures readability?
python python3
python python3
asked Feb 17 at 0:06
Nav
315
315
Yes there are super-clever ways to do it. But, six month from now, will you remember how to maintain those super-clever ways? Your current way is understandable and maintainable - nothing wrong with that.
â user535733
Feb 17 at 0:25
add a comment |Â
Yes there are super-clever ways to do it. But, six month from now, will you remember how to maintain those super-clever ways? Your current way is understandable and maintainable - nothing wrong with that.
â user535733
Feb 17 at 0:25
Yes there are super-clever ways to do it. But, six month from now, will you remember how to maintain those super-clever ways? Your current way is understandable and maintainable - nothing wrong with that.
â user535733
Feb 17 at 0:25
Yes there are super-clever ways to do it. But, six month from now, will you remember how to maintain those super-clever ways? Your current way is understandable and maintainable - nothing wrong with that.
â user535733
Feb 17 at 0:25
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Yes, there is a more Pythonic method of doing this exact interaction: zip
dataset, labels = zip(*list_data(folder))
The star (*
) is important to tell zip
to unzip the tuples of data. In your function then, either return just zip(*list_data(folder))
(no need for a for loop or the temporary variables, or simply use inline with no need for a function.
For future reference, note that this was more of a programming question, and thus likely better asked on stackoverflow.com.
Great! That's exactly what I've been looking for. Thank you. And you are definitely right, I was quite sure I was on the SO page, not AskUbuntu.
â Nav
Feb 17 at 9:38
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Yes, there is a more Pythonic method of doing this exact interaction: zip
dataset, labels = zip(*list_data(folder))
The star (*
) is important to tell zip
to unzip the tuples of data. In your function then, either return just zip(*list_data(folder))
(no need for a for loop or the temporary variables, or simply use inline with no need for a function.
For future reference, note that this was more of a programming question, and thus likely better asked on stackoverflow.com.
Great! That's exactly what I've been looking for. Thank you. And you are definitely right, I was quite sure I was on the SO page, not AskUbuntu.
â Nav
Feb 17 at 9:38
add a comment |Â
up vote
1
down vote
accepted
Yes, there is a more Pythonic method of doing this exact interaction: zip
dataset, labels = zip(*list_data(folder))
The star (*
) is important to tell zip
to unzip the tuples of data. In your function then, either return just zip(*list_data(folder))
(no need for a for loop or the temporary variables, or simply use inline with no need for a function.
For future reference, note that this was more of a programming question, and thus likely better asked on stackoverflow.com.
Great! That's exactly what I've been looking for. Thank you. And you are definitely right, I was quite sure I was on the SO page, not AskUbuntu.
â Nav
Feb 17 at 9:38
add a comment |Â
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Yes, there is a more Pythonic method of doing this exact interaction: zip
dataset, labels = zip(*list_data(folder))
The star (*
) is important to tell zip
to unzip the tuples of data. In your function then, either return just zip(*list_data(folder))
(no need for a for loop or the temporary variables, or simply use inline with no need for a function.
For future reference, note that this was more of a programming question, and thus likely better asked on stackoverflow.com.
Yes, there is a more Pythonic method of doing this exact interaction: zip
dataset, labels = zip(*list_data(folder))
The star (*
) is important to tell zip
to unzip the tuples of data. In your function then, either return just zip(*list_data(folder))
(no need for a for loop or the temporary variables, or simply use inline with no need for a function.
For future reference, note that this was more of a programming question, and thus likely better asked on stackoverflow.com.
answered Feb 17 at 4:32
hunteke
2838
2838
Great! That's exactly what I've been looking for. Thank you. And you are definitely right, I was quite sure I was on the SO page, not AskUbuntu.
â Nav
Feb 17 at 9:38
add a comment |Â
Great! That's exactly what I've been looking for. Thank you. And you are definitely right, I was quite sure I was on the SO page, not AskUbuntu.
â Nav
Feb 17 at 9:38
Great! That's exactly what I've been looking for. Thank you. And you are definitely right, I was quite sure I was on the SO page, not AskUbuntu.
â Nav
Feb 17 at 9:38
Great! That's exactly what I've been looking for. Thank you. And you are definitely right, I was quite sure I was on the SO page, not AskUbuntu.
â Nav
Feb 17 at 9:38
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2faskubuntu.com%2fquestions%2f1006926%2ftwo-different-lists-from-a-tuple-generator%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
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
Yes there are super-clever ways to do it. But, six month from now, will you remember how to maintain those super-clever ways? Your current way is understandable and maintainable - nothing wrong with that.
â user535733
Feb 17 at 0:25