Is there a build in function in Python to get the size of a list like C++?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
When a list has only 1 dimension, len()
could be used to get the size of it, is there a function to get the size of a nested list?
for example, test_list = [[1], [2], [3]]
, len(test_list)
return 3, how to get (3, 1)
?
python
add a comment |
When a list has only 1 dimension, len()
could be used to get the size of it, is there a function to get the size of a nested list?
for example, test_list = [[1], [2], [3]]
, len(test_list)
return 3, how to get (3, 1)
?
python
6
Why should the result be(3, 1)
? The sublists can all be different lengths, and can be nested to any depth. They can even be recursive.
– PM 2Ring
Nov 23 '18 at 15:34
1
You could usenumpy
:np.array([[1], [2], [3]]).shape == (3, 1)
. If the inner lists don't have a consistent length, this will create an array of objects, but if they're consistent you'll get a multi-dimensional array.
– jonrsharpe
Nov 23 '18 at 15:35
list(map(len, values))
=>[1, 1, 1]
– Peter Wood
Nov 23 '18 at 15:54
@PM 2Ring, eh, I assumed the sublists are in same length, but not realized to figure it out, it is my bad, Thank you for your kind reminder.
– buxizhizhoum
Nov 24 '18 at 1:33
add a comment |
When a list has only 1 dimension, len()
could be used to get the size of it, is there a function to get the size of a nested list?
for example, test_list = [[1], [2], [3]]
, len(test_list)
return 3, how to get (3, 1)
?
python
When a list has only 1 dimension, len()
could be used to get the size of it, is there a function to get the size of a nested list?
for example, test_list = [[1], [2], [3]]
, len(test_list)
return 3, how to get (3, 1)
?
python
python
edited Nov 24 '18 at 1:35
buxizhizhoum
asked Nov 23 '18 at 15:32
buxizhizhoumbuxizhizhoum
373512
373512
6
Why should the result be(3, 1)
? The sublists can all be different lengths, and can be nested to any depth. They can even be recursive.
– PM 2Ring
Nov 23 '18 at 15:34
1
You could usenumpy
:np.array([[1], [2], [3]]).shape == (3, 1)
. If the inner lists don't have a consistent length, this will create an array of objects, but if they're consistent you'll get a multi-dimensional array.
– jonrsharpe
Nov 23 '18 at 15:35
list(map(len, values))
=>[1, 1, 1]
– Peter Wood
Nov 23 '18 at 15:54
@PM 2Ring, eh, I assumed the sublists are in same length, but not realized to figure it out, it is my bad, Thank you for your kind reminder.
– buxizhizhoum
Nov 24 '18 at 1:33
add a comment |
6
Why should the result be(3, 1)
? The sublists can all be different lengths, and can be nested to any depth. They can even be recursive.
– PM 2Ring
Nov 23 '18 at 15:34
1
You could usenumpy
:np.array([[1], [2], [3]]).shape == (3, 1)
. If the inner lists don't have a consistent length, this will create an array of objects, but if they're consistent you'll get a multi-dimensional array.
– jonrsharpe
Nov 23 '18 at 15:35
list(map(len, values))
=>[1, 1, 1]
– Peter Wood
Nov 23 '18 at 15:54
@PM 2Ring, eh, I assumed the sublists are in same length, but not realized to figure it out, it is my bad, Thank you for your kind reminder.
– buxizhizhoum
Nov 24 '18 at 1:33
6
6
Why should the result be
(3, 1)
? The sublists can all be different lengths, and can be nested to any depth. They can even be recursive.– PM 2Ring
Nov 23 '18 at 15:34
Why should the result be
(3, 1)
? The sublists can all be different lengths, and can be nested to any depth. They can even be recursive.– PM 2Ring
Nov 23 '18 at 15:34
1
1
You could use
numpy
: np.array([[1], [2], [3]]).shape == (3, 1)
. If the inner lists don't have a consistent length, this will create an array of objects, but if they're consistent you'll get a multi-dimensional array.– jonrsharpe
Nov 23 '18 at 15:35
You could use
numpy
: np.array([[1], [2], [3]]).shape == (3, 1)
. If the inner lists don't have a consistent length, this will create an array of objects, but if they're consistent you'll get a multi-dimensional array.– jonrsharpe
Nov 23 '18 at 15:35
list(map(len, values))
=> [1, 1, 1]
– Peter Wood
Nov 23 '18 at 15:54
list(map(len, values))
=> [1, 1, 1]
– Peter Wood
Nov 23 '18 at 15:54
@PM 2Ring, eh, I assumed the sublists are in same length, but not realized to figure it out, it is my bad, Thank you for your kind reminder.
– buxizhizhoum
Nov 24 '18 at 1:33
@PM 2Ring, eh, I assumed the sublists are in same length, but not realized to figure it out, it is my bad, Thank you for your kind reminder.
– buxizhizhoum
Nov 24 '18 at 1:33
add a comment |
2 Answers
2
active
oldest
votes
It's because amount of elements of your array is indeed 3, if you'd like to check depths of its contents you could do (note that depth of second dimension will be maximum length of its contents)
test_list = [[1], [2], [3]]
print((len(test_list), max(len(i) for i in test_list))) # -> (3, 1)
Or using numpy
import numpy as np
test_list = np.array([[1], [2], [3]])
print(test_list.shape) # -> (3, 1)
add a comment |
If you use a numpy array instead of list then you can get the shape which will be (3, 1).
import numpy as np
a = np.array([[1], [2], [3]])
print(a.shape)
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%2f53449392%2fis-there-a-build-in-function-in-python-to-get-the-size-of-a-list-like-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's because amount of elements of your array is indeed 3, if you'd like to check depths of its contents you could do (note that depth of second dimension will be maximum length of its contents)
test_list = [[1], [2], [3]]
print((len(test_list), max(len(i) for i in test_list))) # -> (3, 1)
Or using numpy
import numpy as np
test_list = np.array([[1], [2], [3]])
print(test_list.shape) # -> (3, 1)
add a comment |
It's because amount of elements of your array is indeed 3, if you'd like to check depths of its contents you could do (note that depth of second dimension will be maximum length of its contents)
test_list = [[1], [2], [3]]
print((len(test_list), max(len(i) for i in test_list))) # -> (3, 1)
Or using numpy
import numpy as np
test_list = np.array([[1], [2], [3]])
print(test_list.shape) # -> (3, 1)
add a comment |
It's because amount of elements of your array is indeed 3, if you'd like to check depths of its contents you could do (note that depth of second dimension will be maximum length of its contents)
test_list = [[1], [2], [3]]
print((len(test_list), max(len(i) for i in test_list))) # -> (3, 1)
Or using numpy
import numpy as np
test_list = np.array([[1], [2], [3]])
print(test_list.shape) # -> (3, 1)
It's because amount of elements of your array is indeed 3, if you'd like to check depths of its contents you could do (note that depth of second dimension will be maximum length of its contents)
test_list = [[1], [2], [3]]
print((len(test_list), max(len(i) for i in test_list))) # -> (3, 1)
Or using numpy
import numpy as np
test_list = np.array([[1], [2], [3]])
print(test_list.shape) # -> (3, 1)
answered Nov 23 '18 at 15:37
Filip MłynarskiFilip Młynarski
2,0241415
2,0241415
add a comment |
add a comment |
If you use a numpy array instead of list then you can get the shape which will be (3, 1).
import numpy as np
a = np.array([[1], [2], [3]])
print(a.shape)
add a comment |
If you use a numpy array instead of list then you can get the shape which will be (3, 1).
import numpy as np
a = np.array([[1], [2], [3]])
print(a.shape)
add a comment |
If you use a numpy array instead of list then you can get the shape which will be (3, 1).
import numpy as np
a = np.array([[1], [2], [3]])
print(a.shape)
If you use a numpy array instead of list then you can get the shape which will be (3, 1).
import numpy as np
a = np.array([[1], [2], [3]])
print(a.shape)
edited Nov 23 '18 at 15:43
Jean-François Fabre♦
106k1058116
106k1058116
answered Nov 23 '18 at 15:37
Attila BognárAttila Bognár
1299
1299
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%2f53449392%2fis-there-a-build-in-function-in-python-to-get-the-size-of-a-list-like-c%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
6
Why should the result be
(3, 1)
? The sublists can all be different lengths, and can be nested to any depth. They can even be recursive.– PM 2Ring
Nov 23 '18 at 15:34
1
You could use
numpy
:np.array([[1], [2], [3]]).shape == (3, 1)
. If the inner lists don't have a consistent length, this will create an array of objects, but if they're consistent you'll get a multi-dimensional array.– jonrsharpe
Nov 23 '18 at 15:35
list(map(len, values))
=>[1, 1, 1]
– Peter Wood
Nov 23 '18 at 15:54
@PM 2Ring, eh, I assumed the sublists are in same length, but not realized to figure it out, it is my bad, Thank you for your kind reminder.
– buxizhizhoum
Nov 24 '18 at 1:33