Extracting a coordinate from a python list of tuples
I have a python list
training_data=[(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)]
and I wish to extract a list of x
values
training_data_x=[x_1, x_2, ..., x_n]
I have tried
for j in range(0, len(training_data)):
training_data_x=[i for i in training_data[j][0]]
and
training_data_x=[i for i in training_data[j][0] for j in range(0, len(training_data))]
but neither worked. How can I do this?
python list tuples
add a comment |
I have a python list
training_data=[(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)]
and I wish to extract a list of x
values
training_data_x=[x_1, x_2, ..., x_n]
I have tried
for j in range(0, len(training_data)):
training_data_x=[i for i in training_data[j][0]]
and
training_data_x=[i for i in training_data[j][0] for j in range(0, len(training_data))]
but neither worked. How can I do this?
python list tuples
[coord[0] for coord in training_data]
?
– Paulo Scardine
Nov 27 '18 at 5:20
add a comment |
I have a python list
training_data=[(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)]
and I wish to extract a list of x
values
training_data_x=[x_1, x_2, ..., x_n]
I have tried
for j in range(0, len(training_data)):
training_data_x=[i for i in training_data[j][0]]
and
training_data_x=[i for i in training_data[j][0] for j in range(0, len(training_data))]
but neither worked. How can I do this?
python list tuples
I have a python list
training_data=[(x_1, y_1), (x_2, y_2), ..., (x_n, y_n)]
and I wish to extract a list of x
values
training_data_x=[x_1, x_2, ..., x_n]
I have tried
for j in range(0, len(training_data)):
training_data_x=[i for i in training_data[j][0]]
and
training_data_x=[i for i in training_data[j][0] for j in range(0, len(training_data))]
but neither worked. How can I do this?
python list tuples
python list tuples
asked Nov 27 '18 at 5:17
Shrey JoshiShrey Joshi
1205
1205
[coord[0] for coord in training_data]
?
– Paulo Scardine
Nov 27 '18 at 5:20
add a comment |
[coord[0] for coord in training_data]
?
– Paulo Scardine
Nov 27 '18 at 5:20
[coord[0] for coord in training_data]
?– Paulo Scardine
Nov 27 '18 at 5:20
[coord[0] for coord in training_data]
?– Paulo Scardine
Nov 27 '18 at 5:20
add a comment |
3 Answers
3
active
oldest
votes
I prefer a list comprehension with a self-documenting tuple unpacking:
[x for x,y in training_data]
Complete program:
training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
training_data_x = [x for x,y in training_data]
print(training_data_x)
add a comment |
A comprehension would do:
training_data_x = [x[0] for x in training_data]
add a comment |
Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :
training_data_x =
for i in training_data:
training_data_x.append(i[0])
another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.
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%2f53493173%2fextracting-a-coordinate-from-a-python-list-of-tuples%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
I prefer a list comprehension with a self-documenting tuple unpacking:
[x for x,y in training_data]
Complete program:
training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
training_data_x = [x for x,y in training_data]
print(training_data_x)
add a comment |
I prefer a list comprehension with a self-documenting tuple unpacking:
[x for x,y in training_data]
Complete program:
training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
training_data_x = [x for x,y in training_data]
print(training_data_x)
add a comment |
I prefer a list comprehension with a self-documenting tuple unpacking:
[x for x,y in training_data]
Complete program:
training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
training_data_x = [x for x,y in training_data]
print(training_data_x)
I prefer a list comprehension with a self-documenting tuple unpacking:
[x for x,y in training_data]
Complete program:
training_data=[(1.5, 11), (2.5, 22), (7.5, 77)]
training_data_x = [x for x,y in training_data]
print(training_data_x)
answered Nov 27 '18 at 5:21
RobᵩRobᵩ
117k13138222
117k13138222
add a comment |
add a comment |
A comprehension would do:
training_data_x = [x[0] for x in training_data]
add a comment |
A comprehension would do:
training_data_x = [x[0] for x in training_data]
add a comment |
A comprehension would do:
training_data_x = [x[0] for x in training_data]
A comprehension would do:
training_data_x = [x[0] for x in training_data]
answered Nov 27 '18 at 5:20
NetwaveNetwave
12.5k22144
12.5k22144
add a comment |
add a comment |
Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :
training_data_x =
for i in training_data:
training_data_x.append(i[0])
another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.
add a comment |
Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :
training_data_x =
for i in training_data:
training_data_x.append(i[0])
another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.
add a comment |
Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :
training_data_x =
for i in training_data:
training_data_x.append(i[0])
another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.
Looks like you just have a list of tuples. I'm still a bit new to python, but I would try :
training_data_x =
for i in training_data:
training_data_x.append(i[0])
another way you could do is create a function that takes tuple and returns the x. Then map the function to your training_data_x variable.
answered Nov 27 '18 at 6:20
RockAndRoleCoderRockAndRoleCoder
5614
5614
add a comment |
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%2f53493173%2fextracting-a-coordinate-from-a-python-list-of-tuples%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
[coord[0] for coord in training_data]
?– Paulo Scardine
Nov 27 '18 at 5:20