Find set with smallest number of elements from a list of sets [duplicate]
This question already has an answer here:
How does the min/max function on a nested list work?
4 answers
I have a list of sets -
inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
I want -
{4, 5}
(set with smallest number of elements)
My code -
length = float("inf")
small = {}
for x in inconsistent_case:
if len(x) < length:
length = len(x)
small = x
print(small)
Which gives me -
{4, 5}
Is there any fastest and/or easiest way to do this?
python-3.x nested
marked as duplicate by Patrick Haugh, Noctis Skytower, Community♦ Nov 13 '18 at 6:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
How does the min/max function on a nested list work?
4 answers
I have a list of sets -
inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
I want -
{4, 5}
(set with smallest number of elements)
My code -
length = float("inf")
small = {}
for x in inconsistent_case:
if len(x) < length:
length = len(x)
small = x
print(small)
Which gives me -
{4, 5}
Is there any fastest and/or easiest way to do this?
python-3.x nested
marked as duplicate by Patrick Haugh, Noctis Skytower, Community♦ Nov 13 '18 at 6:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Thanks @Patrick Haugh, marked it as duplicate.
– Ruturaj
Nov 13 '18 at 6:21
add a comment |
This question already has an answer here:
How does the min/max function on a nested list work?
4 answers
I have a list of sets -
inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
I want -
{4, 5}
(set with smallest number of elements)
My code -
length = float("inf")
small = {}
for x in inconsistent_case:
if len(x) < length:
length = len(x)
small = x
print(small)
Which gives me -
{4, 5}
Is there any fastest and/or easiest way to do this?
python-3.x nested
This question already has an answer here:
How does the min/max function on a nested list work?
4 answers
I have a list of sets -
inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
I want -
{4, 5}
(set with smallest number of elements)
My code -
length = float("inf")
small = {}
for x in inconsistent_case:
if len(x) < length:
length = len(x)
small = x
print(small)
Which gives me -
{4, 5}
Is there any fastest and/or easiest way to do this?
This question already has an answer here:
How does the min/max function on a nested list work?
4 answers
python-3.x nested
python-3.x nested
asked Nov 13 '18 at 3:15
Ruturaj
5619
5619
marked as duplicate by Patrick Haugh, Noctis Skytower, Community♦ Nov 13 '18 at 6:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by Patrick Haugh, Noctis Skytower, Community♦ Nov 13 '18 at 6:21
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
Thanks @Patrick Haugh, marked it as duplicate.
– Ruturaj
Nov 13 '18 at 6:21
add a comment |
Thanks @Patrick Haugh, marked it as duplicate.
– Ruturaj
Nov 13 '18 at 6:21
Thanks @Patrick Haugh, marked it as duplicate.
– Ruturaj
Nov 13 '18 at 6:21
Thanks @Patrick Haugh, marked it as duplicate.
– Ruturaj
Nov 13 '18 at 6:21
add a comment |
1 Answer
1
active
oldest
votes
Yes, specify the key for min
:
>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}
If multiple items are minimal, the function returns the first one encountered.
Thanks @wim that works perfectly fine!
– Ruturaj
Nov 13 '18 at 3:20
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Yes, specify the key for min
:
>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}
If multiple items are minimal, the function returns the first one encountered.
Thanks @wim that works perfectly fine!
– Ruturaj
Nov 13 '18 at 3:20
add a comment |
Yes, specify the key for min
:
>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}
If multiple items are minimal, the function returns the first one encountered.
Thanks @wim that works perfectly fine!
– Ruturaj
Nov 13 '18 at 3:20
add a comment |
Yes, specify the key for min
:
>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}
If multiple items are minimal, the function returns the first one encountered.
Yes, specify the key for min
:
>>> inconsistent_case = [{0, 1, 2, 3, 6, 7}, {4, 5}]
>>> min(inconsistent_case, key=len)
{4, 5}
If multiple items are minimal, the function returns the first one encountered.
answered Nov 13 '18 at 3:18
wim
158k50300430
158k50300430
Thanks @wim that works perfectly fine!
– Ruturaj
Nov 13 '18 at 3:20
add a comment |
Thanks @wim that works perfectly fine!
– Ruturaj
Nov 13 '18 at 3:20
Thanks @wim that works perfectly fine!
– Ruturaj
Nov 13 '18 at 3:20
Thanks @wim that works perfectly fine!
– Ruturaj
Nov 13 '18 at 3:20
add a comment |
Thanks @Patrick Haugh, marked it as duplicate.
– Ruturaj
Nov 13 '18 at 6:21