Pyhton: Retrieve the length of a list value that's within a list value
I recently picked up Python and I am alternating between this tutorial and a book called Automate The Boring Stuff With Python.
I find the book quite easy to understand and the exercises are fun.
There is one in particular that I just solved but honestly, I am not satisfied with my answer.
In the question, one is given a grid as follows:
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
Using loops only, one is then required to produce the following output:
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
My solution (which works) to this problem is as follows:
for y in range(0,len(grid[0])):
for x in range(0, len(grid)):
print(grid[x][y], end='')
print()
My problem is the grid[0]. I have tried to generalize the code to accommodate scenarios in which the lists within grid are of different values, but failed.
My question, therefore, is how can I retrieve the len
of grid[0] in general variable terms?
In other words, how can I retrieve the length of a list within a list?
python
|
show 1 more comment
I recently picked up Python and I am alternating between this tutorial and a book called Automate The Boring Stuff With Python.
I find the book quite easy to understand and the exercises are fun.
There is one in particular that I just solved but honestly, I am not satisfied with my answer.
In the question, one is given a grid as follows:
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
Using loops only, one is then required to produce the following output:
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
My solution (which works) to this problem is as follows:
for y in range(0,len(grid[0])):
for x in range(0, len(grid)):
print(grid[x][y], end='')
print()
My problem is the grid[0]. I have tried to generalize the code to accommodate scenarios in which the lists within grid are of different values, but failed.
My question, therefore, is how can I retrieve the len
of grid[0] in general variable terms?
In other words, how can I retrieve the length of a list within a list?
python
So, do you want a nice solution withzip
or are we stuck to onlyfor
andprint
here? :)
– timgeb
Nov 14 '18 at 8:32
Kind of confusing what exactly you are asking, but just dolen(grid(row_number))
– Tomothy32
Nov 14 '18 at 8:33
@Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.
– Mexen
Nov 14 '18 at 8:49
1
@timgeb Thanks. The book is yet to introduce zip so just for/while and print.
– Mexen
Nov 14 '18 at 8:51
Do you want something likefor sublist in grid: len(sublist)
?
– Tomothy32
Nov 14 '18 at 9:00
|
show 1 more comment
I recently picked up Python and I am alternating between this tutorial and a book called Automate The Boring Stuff With Python.
I find the book quite easy to understand and the exercises are fun.
There is one in particular that I just solved but honestly, I am not satisfied with my answer.
In the question, one is given a grid as follows:
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
Using loops only, one is then required to produce the following output:
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
My solution (which works) to this problem is as follows:
for y in range(0,len(grid[0])):
for x in range(0, len(grid)):
print(grid[x][y], end='')
print()
My problem is the grid[0]. I have tried to generalize the code to accommodate scenarios in which the lists within grid are of different values, but failed.
My question, therefore, is how can I retrieve the len
of grid[0] in general variable terms?
In other words, how can I retrieve the length of a list within a list?
python
I recently picked up Python and I am alternating between this tutorial and a book called Automate The Boring Stuff With Python.
I find the book quite easy to understand and the exercises are fun.
There is one in particular that I just solved but honestly, I am not satisfied with my answer.
In the question, one is given a grid as follows:
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
Using loops only, one is then required to produce the following output:
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
My solution (which works) to this problem is as follows:
for y in range(0,len(grid[0])):
for x in range(0, len(grid)):
print(grid[x][y], end='')
print()
My problem is the grid[0]. I have tried to generalize the code to accommodate scenarios in which the lists within grid are of different values, but failed.
My question, therefore, is how can I retrieve the len
of grid[0] in general variable terms?
In other words, how can I retrieve the length of a list within a list?
python
python
asked Nov 14 '18 at 8:28
MexenMexen
18319
18319
So, do you want a nice solution withzip
or are we stuck to onlyfor
andprint
here? :)
– timgeb
Nov 14 '18 at 8:32
Kind of confusing what exactly you are asking, but just dolen(grid(row_number))
– Tomothy32
Nov 14 '18 at 8:33
@Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.
– Mexen
Nov 14 '18 at 8:49
1
@timgeb Thanks. The book is yet to introduce zip so just for/while and print.
– Mexen
Nov 14 '18 at 8:51
Do you want something likefor sublist in grid: len(sublist)
?
– Tomothy32
Nov 14 '18 at 9:00
|
show 1 more comment
So, do you want a nice solution withzip
or are we stuck to onlyfor
andprint
here? :)
– timgeb
Nov 14 '18 at 8:32
Kind of confusing what exactly you are asking, but just dolen(grid(row_number))
– Tomothy32
Nov 14 '18 at 8:33
@Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.
– Mexen
Nov 14 '18 at 8:49
1
@timgeb Thanks. The book is yet to introduce zip so just for/while and print.
– Mexen
Nov 14 '18 at 8:51
Do you want something likefor sublist in grid: len(sublist)
?
– Tomothy32
Nov 14 '18 at 9:00
So, do you want a nice solution with
zip
or are we stuck to only for
and print
here? :)– timgeb
Nov 14 '18 at 8:32
So, do you want a nice solution with
zip
or are we stuck to only for
and print
here? :)– timgeb
Nov 14 '18 at 8:32
Kind of confusing what exactly you are asking, but just do
len(grid(row_number))
– Tomothy32
Nov 14 '18 at 8:33
Kind of confusing what exactly you are asking, but just do
len(grid(row_number))
– Tomothy32
Nov 14 '18 at 8:33
@Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.
– Mexen
Nov 14 '18 at 8:49
@Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.
– Mexen
Nov 14 '18 at 8:49
1
1
@timgeb Thanks. The book is yet to introduce zip so just for/while and print.
– Mexen
Nov 14 '18 at 8:51
@timgeb Thanks. The book is yet to introduce zip so just for/while and print.
– Mexen
Nov 14 '18 at 8:51
Do you want something like
for sublist in grid: len(sublist)
?– Tomothy32
Nov 14 '18 at 9:00
Do you want something like
for sublist in grid: len(sublist)
?– Tomothy32
Nov 14 '18 at 9:00
|
show 1 more comment
5 Answers
5
active
oldest
votes
You don't actually need to retrieve length.
You can do it in one line without knowing dimensions:
print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
Step by step:
zip(*grid)
will transpose your 2-d array
>>> list(zip(*grid))
[('.', '.', '0', '0', '.', '0', '0', '.', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '.', '0', '0', '0', '0', '0', '.', '.'),
('.', '.', '.', '0', '0', '0', '.', '.', '.'),
('.', '.', '.', '.', '0', '.', '.', '.', '.')]
map(lambda x: ''.join(x), zip(*grid))
will join every line
>>> list(map(lambda x: ''.join(x), zip(*grid)))
['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']
- And finally
'n'.join
joins with separator as new line
>>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
Note: we could also omit 3rd step by print
ing with sep='n
, however in Python3:
Why does map return a map object instead of a list in Python 3? and we would need to convert it to list
before printing it. Like so:
print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')
1
Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!
– Mexen
Nov 14 '18 at 13:51
add a comment |
For example taking the longes of all the content list:
>>> grid = [['.', '.', '.', '.', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['0', '0', '0', '0', '0', '.'],
... ['.', '0', '0', '0', '0', '0'],
... ['0', '0', '0', '0', '0', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['.', '.', '.', '.', '.', '.']]
>>>
>>> max(map(len, grid))
6
BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid))
:
>>> for l in zip(*grid):
... print("".join(l))
...
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
1
You don't need to convert yourzip
object intolist
since you iterating over it.
– vishes_shell
Nov 14 '18 at 8:47
add a comment |
If you look at your desired output, you'll observe that final
matrix is transpose
of your given list.
The transpose
of a matrix could be found by using numpy
transpose function.
You could use a list comprehension in combination with join
method in order to achieve what you want.
import numpy as np
transpose = np.array(grid).transpose()
print('n'.join([''.join(row) for row in transpose]))
Output
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.
– Mexen
Nov 14 '18 at 13:52
@Mexen, you're welcome. Don't forget to vote answers in order to help other people.
– Mihai Alexandru-Ionut
Nov 14 '18 at 14:49
add a comment |
If you really must get the length of a list in a list, just use len(grid[index])
:
for i in range(len(grid)):
for j in range(len(grid[i])):
print(mylist[i][j], end='')
print()
However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use
for row in mylist:
for element in row:
print(element, end='')
print()
add a comment |
Another way of doing this is to start by transposing the grid, then just loop over the elements of each row
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
grid2=zip(*grid)
# print(grid2)
for row in grid2:
for el in row:
print(el, end='')
print('n')
1
You don't actually need to convertgrid2
into alist
, since you can iterate over it.
– vishes_shell
Nov 14 '18 at 8:45
Very true, editing
– robotHamster
Nov 14 '18 at 8:47
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%2f53295848%2fpyhton-retrieve-the-length-of-a-list-value-thats-within-a-list-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
You don't actually need to retrieve length.
You can do it in one line without knowing dimensions:
print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
Step by step:
zip(*grid)
will transpose your 2-d array
>>> list(zip(*grid))
[('.', '.', '0', '0', '.', '0', '0', '.', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '.', '0', '0', '0', '0', '0', '.', '.'),
('.', '.', '.', '0', '0', '0', '.', '.', '.'),
('.', '.', '.', '.', '0', '.', '.', '.', '.')]
map(lambda x: ''.join(x), zip(*grid))
will join every line
>>> list(map(lambda x: ''.join(x), zip(*grid)))
['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']
- And finally
'n'.join
joins with separator as new line
>>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
Note: we could also omit 3rd step by print
ing with sep='n
, however in Python3:
Why does map return a map object instead of a list in Python 3? and we would need to convert it to list
before printing it. Like so:
print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')
1
Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!
– Mexen
Nov 14 '18 at 13:51
add a comment |
You don't actually need to retrieve length.
You can do it in one line without knowing dimensions:
print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
Step by step:
zip(*grid)
will transpose your 2-d array
>>> list(zip(*grid))
[('.', '.', '0', '0', '.', '0', '0', '.', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '.', '0', '0', '0', '0', '0', '.', '.'),
('.', '.', '.', '0', '0', '0', '.', '.', '.'),
('.', '.', '.', '.', '0', '.', '.', '.', '.')]
map(lambda x: ''.join(x), zip(*grid))
will join every line
>>> list(map(lambda x: ''.join(x), zip(*grid)))
['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']
- And finally
'n'.join
joins with separator as new line
>>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
Note: we could also omit 3rd step by print
ing with sep='n
, however in Python3:
Why does map return a map object instead of a list in Python 3? and we would need to convert it to list
before printing it. Like so:
print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')
1
Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!
– Mexen
Nov 14 '18 at 13:51
add a comment |
You don't actually need to retrieve length.
You can do it in one line without knowing dimensions:
print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
Step by step:
zip(*grid)
will transpose your 2-d array
>>> list(zip(*grid))
[('.', '.', '0', '0', '.', '0', '0', '.', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '.', '0', '0', '0', '0', '0', '.', '.'),
('.', '.', '.', '0', '0', '0', '.', '.', '.'),
('.', '.', '.', '.', '0', '.', '.', '.', '.')]
map(lambda x: ''.join(x), zip(*grid))
will join every line
>>> list(map(lambda x: ''.join(x), zip(*grid)))
['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']
- And finally
'n'.join
joins with separator as new line
>>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
Note: we could also omit 3rd step by print
ing with sep='n
, however in Python3:
Why does map return a map object instead of a list in Python 3? and we would need to convert it to list
before printing it. Like so:
print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')
You don't actually need to retrieve length.
You can do it in one line without knowing dimensions:
print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
Step by step:
zip(*grid)
will transpose your 2-d array
>>> list(zip(*grid))
[('.', '.', '0', '0', '.', '0', '0', '.', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '0', '0', '0', '0', '0', '0', '0', '.'),
('.', '.', '0', '0', '0', '0', '0', '.', '.'),
('.', '.', '.', '0', '0', '0', '.', '.', '.'),
('.', '.', '.', '.', '0', '.', '.', '.', '.')]
map(lambda x: ''.join(x), zip(*grid))
will join every line
>>> list(map(lambda x: ''.join(x), zip(*grid)))
['..00.00..', '.0000000.', '.0000000.', '..00000..', '...000...', '....0....']
- And finally
'n'.join
joins with separator as new line
>>> print('n'.join(map(lambda x: ''.join(x), zip(*grid))))
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
Note: we could also omit 3rd step by print
ing with sep='n
, however in Python3:
Why does map return a map object instead of a list in Python 3? and we would need to convert it to list
before printing it. Like so:
print(list(map(lambda x: ''.join(x), zip(*grid))), sep='n')
edited Nov 14 '18 at 8:42
answered Nov 14 '18 at 8:34
vishes_shellvishes_shell
10.4k34045
10.4k34045
1
Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!
– Mexen
Nov 14 '18 at 13:51
add a comment |
1
Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!
– Mexen
Nov 14 '18 at 13:51
1
1
Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!
– Mexen
Nov 14 '18 at 13:51
Very interesting, thank you for sharing! At the time of writing this comment, I have not read about zip yet so implementation is based on loops and print alone but it is one to look out for, for sure!
– Mexen
Nov 14 '18 at 13:51
add a comment |
For example taking the longes of all the content list:
>>> grid = [['.', '.', '.', '.', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['0', '0', '0', '0', '0', '.'],
... ['.', '0', '0', '0', '0', '0'],
... ['0', '0', '0', '0', '0', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['.', '.', '.', '.', '.', '.']]
>>>
>>> max(map(len, grid))
6
BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid))
:
>>> for l in zip(*grid):
... print("".join(l))
...
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
1
You don't need to convert yourzip
object intolist
since you iterating over it.
– vishes_shell
Nov 14 '18 at 8:47
add a comment |
For example taking the longes of all the content list:
>>> grid = [['.', '.', '.', '.', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['0', '0', '0', '0', '0', '.'],
... ['.', '0', '0', '0', '0', '0'],
... ['0', '0', '0', '0', '0', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['.', '.', '.', '.', '.', '.']]
>>>
>>> max(map(len, grid))
6
BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid))
:
>>> for l in zip(*grid):
... print("".join(l))
...
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
1
You don't need to convert yourzip
object intolist
since you iterating over it.
– vishes_shell
Nov 14 '18 at 8:47
add a comment |
For example taking the longes of all the content list:
>>> grid = [['.', '.', '.', '.', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['0', '0', '0', '0', '0', '.'],
... ['.', '0', '0', '0', '0', '0'],
... ['0', '0', '0', '0', '0', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['.', '.', '.', '.', '.', '.']]
>>>
>>> max(map(len, grid))
6
BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid))
:
>>> for l in zip(*grid):
... print("".join(l))
...
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
For example taking the longes of all the content list:
>>> grid = [['.', '.', '.', '.', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['0', '0', '0', '0', '0', '.'],
... ['.', '0', '0', '0', '0', '0'],
... ['0', '0', '0', '0', '0', '.'],
... ['0', '0', '0', '0', '.', '.'],
... ['.', '0', '0', '.', '.', '.'],
... ['.', '.', '.', '.', '.', '.']]
>>>
>>> max(map(len, grid))
6
BTW, a pythonic way of solving that (without loops) would look like this list(zip(*grid))
:
>>> for l in zip(*grid):
... print("".join(l))
...
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
edited Nov 14 '18 at 8:47
answered Nov 14 '18 at 8:32
NetwaveNetwave
12.5k22144
12.5k22144
1
You don't need to convert yourzip
object intolist
since you iterating over it.
– vishes_shell
Nov 14 '18 at 8:47
add a comment |
1
You don't need to convert yourzip
object intolist
since you iterating over it.
– vishes_shell
Nov 14 '18 at 8:47
1
1
You don't need to convert your
zip
object into list
since you iterating over it.– vishes_shell
Nov 14 '18 at 8:47
You don't need to convert your
zip
object into list
since you iterating over it.– vishes_shell
Nov 14 '18 at 8:47
add a comment |
If you look at your desired output, you'll observe that final
matrix is transpose
of your given list.
The transpose
of a matrix could be found by using numpy
transpose function.
You could use a list comprehension in combination with join
method in order to achieve what you want.
import numpy as np
transpose = np.array(grid).transpose()
print('n'.join([''.join(row) for row in transpose]))
Output
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.
– Mexen
Nov 14 '18 at 13:52
@Mexen, you're welcome. Don't forget to vote answers in order to help other people.
– Mihai Alexandru-Ionut
Nov 14 '18 at 14:49
add a comment |
If you look at your desired output, you'll observe that final
matrix is transpose
of your given list.
The transpose
of a matrix could be found by using numpy
transpose function.
You could use a list comprehension in combination with join
method in order to achieve what you want.
import numpy as np
transpose = np.array(grid).transpose()
print('n'.join([''.join(row) for row in transpose]))
Output
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.
– Mexen
Nov 14 '18 at 13:52
@Mexen, you're welcome. Don't forget to vote answers in order to help other people.
– Mihai Alexandru-Ionut
Nov 14 '18 at 14:49
add a comment |
If you look at your desired output, you'll observe that final
matrix is transpose
of your given list.
The transpose
of a matrix could be found by using numpy
transpose function.
You could use a list comprehension in combination with join
method in order to achieve what you want.
import numpy as np
transpose = np.array(grid).transpose()
print('n'.join([''.join(row) for row in transpose]))
Output
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
If you look at your desired output, you'll observe that final
matrix is transpose
of your given list.
The transpose
of a matrix could be found by using numpy
transpose function.
You could use a list comprehension in combination with join
method in order to achieve what you want.
import numpy as np
transpose = np.array(grid).transpose()
print('n'.join([''.join(row) for row in transpose]))
Output
..00.00..
.0000000.
.0000000.
..00000..
...000...
....0....
edited Nov 14 '18 at 8:52
answered Nov 14 '18 at 8:41
Mihai Alexandru-IonutMihai Alexandru-Ionut
30.3k63971
30.3k63971
I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.
– Mexen
Nov 14 '18 at 13:52
@Mexen, you're welcome. Don't forget to vote answers in order to help other people.
– Mihai Alexandru-Ionut
Nov 14 '18 at 14:49
add a comment |
I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.
– Mexen
Nov 14 '18 at 13:52
@Mexen, you're welcome. Don't forget to vote answers in order to help other people.
– Mihai Alexandru-Ionut
Nov 14 '18 at 14:49
I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.
– Mexen
Nov 14 '18 at 13:52
I have heard of numpy but I am yet to use it. I will keep this mind, thank you very much.
– Mexen
Nov 14 '18 at 13:52
@Mexen, you're welcome. Don't forget to vote answers in order to help other people.
– Mihai Alexandru-Ionut
Nov 14 '18 at 14:49
@Mexen, you're welcome. Don't forget to vote answers in order to help other people.
– Mihai Alexandru-Ionut
Nov 14 '18 at 14:49
add a comment |
If you really must get the length of a list in a list, just use len(grid[index])
:
for i in range(len(grid)):
for j in range(len(grid[i])):
print(mylist[i][j], end='')
print()
However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use
for row in mylist:
for element in row:
print(element, end='')
print()
add a comment |
If you really must get the length of a list in a list, just use len(grid[index])
:
for i in range(len(grid)):
for j in range(len(grid[i])):
print(mylist[i][j], end='')
print()
However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use
for row in mylist:
for element in row:
print(element, end='')
print()
add a comment |
If you really must get the length of a list in a list, just use len(grid[index])
:
for i in range(len(grid)):
for j in range(len(grid[i])):
print(mylist[i][j], end='')
print()
However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use
for row in mylist:
for element in row:
print(element, end='')
print()
If you really must get the length of a list in a list, just use len(grid[index])
:
for i in range(len(grid)):
for j in range(len(grid[i])):
print(mylist[i][j], end='')
print()
However, it is easier to to iterate over the elements of the list than to get the indexes of the list. For example, instead of the above solution, use
for row in mylist:
for element in row:
print(element, end='')
print()
answered Nov 14 '18 at 21:58
Tomothy32Tomothy32
4,8161322
4,8161322
add a comment |
add a comment |
Another way of doing this is to start by transposing the grid, then just loop over the elements of each row
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
grid2=zip(*grid)
# print(grid2)
for row in grid2:
for el in row:
print(el, end='')
print('n')
1
You don't actually need to convertgrid2
into alist
, since you can iterate over it.
– vishes_shell
Nov 14 '18 at 8:45
Very true, editing
– robotHamster
Nov 14 '18 at 8:47
add a comment |
Another way of doing this is to start by transposing the grid, then just loop over the elements of each row
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
grid2=zip(*grid)
# print(grid2)
for row in grid2:
for el in row:
print(el, end='')
print('n')
1
You don't actually need to convertgrid2
into alist
, since you can iterate over it.
– vishes_shell
Nov 14 '18 at 8:45
Very true, editing
– robotHamster
Nov 14 '18 at 8:47
add a comment |
Another way of doing this is to start by transposing the grid, then just loop over the elements of each row
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
grid2=zip(*grid)
# print(grid2)
for row in grid2:
for el in row:
print(el, end='')
print('n')
Another way of doing this is to start by transposing the grid, then just loop over the elements of each row
grid = [['.', '.', '.', '.', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['0', '0', '0', '0', '.', '.'],
['0', '0', '0', '0', '0', '.'],
['.', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0', '.'],
['0', '0', '0', '0', '.', '.'],
['.', '0', '0', '.', '.', '.'],
['.', '.', '.', '.', '.', '.']]
grid2=zip(*grid)
# print(grid2)
for row in grid2:
for el in row:
print(el, end='')
print('n')
edited Nov 15 '18 at 7:52
answered Nov 14 '18 at 8:36
robotHamsterrobotHamster
343215
343215
1
You don't actually need to convertgrid2
into alist
, since you can iterate over it.
– vishes_shell
Nov 14 '18 at 8:45
Very true, editing
– robotHamster
Nov 14 '18 at 8:47
add a comment |
1
You don't actually need to convertgrid2
into alist
, since you can iterate over it.
– vishes_shell
Nov 14 '18 at 8:45
Very true, editing
– robotHamster
Nov 14 '18 at 8:47
1
1
You don't actually need to convert
grid2
into a list
, since you can iterate over it.– vishes_shell
Nov 14 '18 at 8:45
You don't actually need to convert
grid2
into a list
, since you can iterate over it.– vishes_shell
Nov 14 '18 at 8:45
Very true, editing
– robotHamster
Nov 14 '18 at 8:47
Very true, editing
– robotHamster
Nov 14 '18 at 8:47
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%2f53295848%2fpyhton-retrieve-the-length-of-a-list-value-thats-within-a-list-value%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
So, do you want a nice solution with
zip
or are we stuck to onlyfor
andprint
here? :)– timgeb
Nov 14 '18 at 8:32
Kind of confusing what exactly you are asking, but just do
len(grid(row_number))
– Tomothy32
Nov 14 '18 at 8:33
@Tomothy32 that's exactly what I did. But how would I loop through list values of varying length? Let's imagine that I do not know their lengths either.
– Mexen
Nov 14 '18 at 8:49
1
@timgeb Thanks. The book is yet to introduce zip so just for/while and print.
– Mexen
Nov 14 '18 at 8:51
Do you want something like
for sublist in grid: len(sublist)
?– Tomothy32
Nov 14 '18 at 9:00