joining outputs corresponding to their original list
Hi I have a nested for loop which processes each element in the list and gives it right alignment and whitespace at the same time.
I have successfully managed to output the individual elements, but I am currently having trouble joining the outputs into their original lists.
Below is my code.
input = [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
space = len(str(input[-1][-1]))
for row in input:
for e in row:
new_element = ':>d'.format(e, space + 1)
print(new_element)
>>> [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
# current output
1
1
1
1
2
3
1
3
6
# desired output
1 1 1
1 2 3
1 3 6
I have little clue how to reassemble the outputs into their original groupings. What methods can I use?
python python-3.x python-2.7
add a comment |
Hi I have a nested for loop which processes each element in the list and gives it right alignment and whitespace at the same time.
I have successfully managed to output the individual elements, but I am currently having trouble joining the outputs into their original lists.
Below is my code.
input = [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
space = len(str(input[-1][-1]))
for row in input:
for e in row:
new_element = ':>d'.format(e, space + 1)
print(new_element)
>>> [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
# current output
1
1
1
1
2
3
1
3
6
# desired output
1 1 1
1 2 3
1 3 6
I have little clue how to reassemble the outputs into their original groupings. What methods can I use?
python python-3.x python-2.7
If you accept as an answer please no upvote! Thanks.
– d_kennetz
Nov 15 '18 at 4:03
add a comment |
Hi I have a nested for loop which processes each element in the list and gives it right alignment and whitespace at the same time.
I have successfully managed to output the individual elements, but I am currently having trouble joining the outputs into their original lists.
Below is my code.
input = [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
space = len(str(input[-1][-1]))
for row in input:
for e in row:
new_element = ':>d'.format(e, space + 1)
print(new_element)
>>> [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
# current output
1
1
1
1
2
3
1
3
6
# desired output
1 1 1
1 2 3
1 3 6
I have little clue how to reassemble the outputs into their original groupings. What methods can I use?
python python-3.x python-2.7
Hi I have a nested for loop which processes each element in the list and gives it right alignment and whitespace at the same time.
I have successfully managed to output the individual elements, but I am currently having trouble joining the outputs into their original lists.
Below is my code.
input = [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
space = len(str(input[-1][-1]))
for row in input:
for e in row:
new_element = ':>d'.format(e, space + 1)
print(new_element)
>>> [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
# current output
1
1
1
1
2
3
1
3
6
# desired output
1 1 1
1 2 3
1 3 6
I have little clue how to reassemble the outputs into their original groupings. What methods can I use?
python python-3.x python-2.7
python python-3.x python-2.7
asked Nov 15 '18 at 3:42
V AnonV Anon
2236
2236
If you accept as an answer please no upvote! Thanks.
– d_kennetz
Nov 15 '18 at 4:03
add a comment |
If you accept as an answer please no upvote! Thanks.
– d_kennetz
Nov 15 '18 at 4:03
If you accept as an answer please no upvote! Thanks.
– d_kennetz
Nov 15 '18 at 4:03
If you accept as an answer please no upvote! Thanks.
– d_kennetz
Nov 15 '18 at 4:03
add a comment |
1 Answer
1
active
oldest
votes
you are pretty close! You shouldn't use "input" as the name of your list because it is a python function. I changed it to input2. Try this:
input2 = [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
for row in input2:
print(" ".join([str(x) for x in row]))
The "join" method joins items in a list by the characters provided in the quotes. You can call join on the list comprehension for list of lists to display each list in the list of lists how you'd like to view it:
out:
>>> for row in input2:
print(" ".join([str(x) for x in row]))
1 1 1
1 2 3
1 3 6
if I changed the join to a comma:
>>> for row in input2:
... print(",".join([str(x) for x in row]))
1,1,1
1,2,3
1,3,6
If you'd like to retain the whitespace in front of each element and store them into a list of their own (I think I understand):
z =
for row in input2:
z.append(" ".join([str(x) for x in row]))
print(z)
###['1 1 1', '1 2 3', '1 3 6']
for y in z:
print(y)
1 1 1
1 2 3
1 3 6
Now you understand "join" a little better!
Is there a way to group them after I have inserted space before each element? In my code I tried to do 1.) give whitespace in front of each element 2.) then group them together according to their original list.
– V Anon
Nov 15 '18 at 4:03
I'm not sure I understand. Would you like to group all of them now like1 1 11 2 31 3 6? Like as little strings in a new list with a space in between numbers? perhaps you should edit the question as this answered your original one.
– d_kennetz
Nov 15 '18 at 4:04
1
I currently have the elements printing in separate lines as you can see in my code. My goal is to group those elements in their original list as you can see in my desired output.
– V Anon
Nov 15 '18 at 4:07
Several of the methods above should do what you ask, I hope this helps!
– d_kennetz
Nov 15 '18 at 4:11
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%2f53312105%2fjoining-outputs-corresponding-to-their-original-list%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
you are pretty close! You shouldn't use "input" as the name of your list because it is a python function. I changed it to input2. Try this:
input2 = [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
for row in input2:
print(" ".join([str(x) for x in row]))
The "join" method joins items in a list by the characters provided in the quotes. You can call join on the list comprehension for list of lists to display each list in the list of lists how you'd like to view it:
out:
>>> for row in input2:
print(" ".join([str(x) for x in row]))
1 1 1
1 2 3
1 3 6
if I changed the join to a comma:
>>> for row in input2:
... print(",".join([str(x) for x in row]))
1,1,1
1,2,3
1,3,6
If you'd like to retain the whitespace in front of each element and store them into a list of their own (I think I understand):
z =
for row in input2:
z.append(" ".join([str(x) for x in row]))
print(z)
###['1 1 1', '1 2 3', '1 3 6']
for y in z:
print(y)
1 1 1
1 2 3
1 3 6
Now you understand "join" a little better!
Is there a way to group them after I have inserted space before each element? In my code I tried to do 1.) give whitespace in front of each element 2.) then group them together according to their original list.
– V Anon
Nov 15 '18 at 4:03
I'm not sure I understand. Would you like to group all of them now like1 1 11 2 31 3 6? Like as little strings in a new list with a space in between numbers? perhaps you should edit the question as this answered your original one.
– d_kennetz
Nov 15 '18 at 4:04
1
I currently have the elements printing in separate lines as you can see in my code. My goal is to group those elements in their original list as you can see in my desired output.
– V Anon
Nov 15 '18 at 4:07
Several of the methods above should do what you ask, I hope this helps!
– d_kennetz
Nov 15 '18 at 4:11
add a comment |
you are pretty close! You shouldn't use "input" as the name of your list because it is a python function. I changed it to input2. Try this:
input2 = [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
for row in input2:
print(" ".join([str(x) for x in row]))
The "join" method joins items in a list by the characters provided in the quotes. You can call join on the list comprehension for list of lists to display each list in the list of lists how you'd like to view it:
out:
>>> for row in input2:
print(" ".join([str(x) for x in row]))
1 1 1
1 2 3
1 3 6
if I changed the join to a comma:
>>> for row in input2:
... print(",".join([str(x) for x in row]))
1,1,1
1,2,3
1,3,6
If you'd like to retain the whitespace in front of each element and store them into a list of their own (I think I understand):
z =
for row in input2:
z.append(" ".join([str(x) for x in row]))
print(z)
###['1 1 1', '1 2 3', '1 3 6']
for y in z:
print(y)
1 1 1
1 2 3
1 3 6
Now you understand "join" a little better!
Is there a way to group them after I have inserted space before each element? In my code I tried to do 1.) give whitespace in front of each element 2.) then group them together according to their original list.
– V Anon
Nov 15 '18 at 4:03
I'm not sure I understand. Would you like to group all of them now like1 1 11 2 31 3 6? Like as little strings in a new list with a space in between numbers? perhaps you should edit the question as this answered your original one.
– d_kennetz
Nov 15 '18 at 4:04
1
I currently have the elements printing in separate lines as you can see in my code. My goal is to group those elements in their original list as you can see in my desired output.
– V Anon
Nov 15 '18 at 4:07
Several of the methods above should do what you ask, I hope this helps!
– d_kennetz
Nov 15 '18 at 4:11
add a comment |
you are pretty close! You shouldn't use "input" as the name of your list because it is a python function. I changed it to input2. Try this:
input2 = [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
for row in input2:
print(" ".join([str(x) for x in row]))
The "join" method joins items in a list by the characters provided in the quotes. You can call join on the list comprehension for list of lists to display each list in the list of lists how you'd like to view it:
out:
>>> for row in input2:
print(" ".join([str(x) for x in row]))
1 1 1
1 2 3
1 3 6
if I changed the join to a comma:
>>> for row in input2:
... print(",".join([str(x) for x in row]))
1,1,1
1,2,3
1,3,6
If you'd like to retain the whitespace in front of each element and store them into a list of their own (I think I understand):
z =
for row in input2:
z.append(" ".join([str(x) for x in row]))
print(z)
###['1 1 1', '1 2 3', '1 3 6']
for y in z:
print(y)
1 1 1
1 2 3
1 3 6
Now you understand "join" a little better!
you are pretty close! You shouldn't use "input" as the name of your list because it is a python function. I changed it to input2. Try this:
input2 = [[1, 1, 1], [1, 2, 3], [1, 3, 6]]
for row in input2:
print(" ".join([str(x) for x in row]))
The "join" method joins items in a list by the characters provided in the quotes. You can call join on the list comprehension for list of lists to display each list in the list of lists how you'd like to view it:
out:
>>> for row in input2:
print(" ".join([str(x) for x in row]))
1 1 1
1 2 3
1 3 6
if I changed the join to a comma:
>>> for row in input2:
... print(",".join([str(x) for x in row]))
1,1,1
1,2,3
1,3,6
If you'd like to retain the whitespace in front of each element and store them into a list of their own (I think I understand):
z =
for row in input2:
z.append(" ".join([str(x) for x in row]))
print(z)
###['1 1 1', '1 2 3', '1 3 6']
for y in z:
print(y)
1 1 1
1 2 3
1 3 6
Now you understand "join" a little better!
edited Nov 15 '18 at 4:10
answered Nov 15 '18 at 3:57
d_kennetzd_kennetz
2,2483927
2,2483927
Is there a way to group them after I have inserted space before each element? In my code I tried to do 1.) give whitespace in front of each element 2.) then group them together according to their original list.
– V Anon
Nov 15 '18 at 4:03
I'm not sure I understand. Would you like to group all of them now like1 1 11 2 31 3 6? Like as little strings in a new list with a space in between numbers? perhaps you should edit the question as this answered your original one.
– d_kennetz
Nov 15 '18 at 4:04
1
I currently have the elements printing in separate lines as you can see in my code. My goal is to group those elements in their original list as you can see in my desired output.
– V Anon
Nov 15 '18 at 4:07
Several of the methods above should do what you ask, I hope this helps!
– d_kennetz
Nov 15 '18 at 4:11
add a comment |
Is there a way to group them after I have inserted space before each element? In my code I tried to do 1.) give whitespace in front of each element 2.) then group them together according to their original list.
– V Anon
Nov 15 '18 at 4:03
I'm not sure I understand. Would you like to group all of them now like1 1 11 2 31 3 6? Like as little strings in a new list with a space in between numbers? perhaps you should edit the question as this answered your original one.
– d_kennetz
Nov 15 '18 at 4:04
1
I currently have the elements printing in separate lines as you can see in my code. My goal is to group those elements in their original list as you can see in my desired output.
– V Anon
Nov 15 '18 at 4:07
Several of the methods above should do what you ask, I hope this helps!
– d_kennetz
Nov 15 '18 at 4:11
Is there a way to group them after I have inserted space before each element? In my code I tried to do 1.) give whitespace in front of each element 2.) then group them together according to their original list.
– V Anon
Nov 15 '18 at 4:03
Is there a way to group them after I have inserted space before each element? In my code I tried to do 1.) give whitespace in front of each element 2.) then group them together according to their original list.
– V Anon
Nov 15 '18 at 4:03
I'm not sure I understand. Would you like to group all of them now like
1 1 1 1 2 3 1 3 6? Like as little strings in a new list with a space in between numbers? perhaps you should edit the question as this answered your original one.– d_kennetz
Nov 15 '18 at 4:04
I'm not sure I understand. Would you like to group all of them now like
1 1 1 1 2 3 1 3 6? Like as little strings in a new list with a space in between numbers? perhaps you should edit the question as this answered your original one.– d_kennetz
Nov 15 '18 at 4:04
1
1
I currently have the elements printing in separate lines as you can see in my code. My goal is to group those elements in their original list as you can see in my desired output.
– V Anon
Nov 15 '18 at 4:07
I currently have the elements printing in separate lines as you can see in my code. My goal is to group those elements in their original list as you can see in my desired output.
– V Anon
Nov 15 '18 at 4:07
Several of the methods above should do what you ask, I hope this helps!
– d_kennetz
Nov 15 '18 at 4:11
Several of the methods above should do what you ask, I hope this helps!
– d_kennetz
Nov 15 '18 at 4:11
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%2f53312105%2fjoining-outputs-corresponding-to-their-original-list%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
If you accept as an answer please no upvote! Thanks.
– d_kennetz
Nov 15 '18 at 4:03