Python Forum

Full Version: Minimum size
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone,

I have the following two-dimensional list:

['1', '6', '66', '9', '33'], ['1', '6', '9', '33'], ['1', '66', '6', '9', '33'], ['1', '66', '9', '33'], ['1', '4', '9', '33']]

And I try to display all the lists containing the least number

So here:
1 6 9 33
1 66 9 33
1 4 9 33

Here is my code for now:

L=[['1', '6', '66', '9', '33'], ['1', '6', '9', '33'], ['1', '66', '6', '9', '33'], ['1', '66', '9', '33'], ['1', '4', '9', '33']]
print(L)
print("")
for z in L:
	mylen = lambda s: s.count(' ') + 1
	min_len = mylen(min(z,key=mylen))
	mins = [e for e in z if mylen(e) == min_len]
	print(mins)
And here is the result I get with this code:

['1', '6', '66', '9', '33']
['1', '6', '9', '33']
['1', '66', '6', '9', '33']
['1', '66', '9', '33']
['1', '4', '9', '33']

Can you help me ?

Thanks
(Jul-08-2019, 09:14 PM)Amniote Wrote: [ -> ]mylen = lambda s: s.count(' ') + 1

What's your intention with that line?
There's no spaces in any of your values, so counting how many blank spaces there are will always give zero. So getting the min value will always return 1, since the min is 0+1. So all of them are printed, because all of them have the same min (because none of them have spaces in them).
Start by writing code that computes the lengths of L's items, so
L=[['1', '6', '66', '9', '33'], ['1', '6', '9', '33'], ['1', '66', '6', '9', '33'], ['1', '66', '9', '33'], ['1', '4', '9', '33']]
... # your code here
print(result)
Output:
[5, 4, 5, 4, 4]
(Jul-08-2019, 09:18 PM)nilamo Wrote: [ -> ]
(Jul-08-2019, 09:14 PM)Amniote Wrote: [ -> ]mylen = lambda s: s.count(' ') + 1
What's your intention with that line?


I wanted to separate the data according to the spaces present after each comma.

I do not see how else to isolate each number in my lists to then calculate the number of elements and display the lists with the least elements ...
Amniote Wrote:I do not see how else to isolate each number in my lists to then calculate the number of elements and display the lists with the least elements ...
The len() function computes the number of elements in a list
>>> data = ['1', '6', '66', '9', '33']
>>> len(data)
5
You are on right track (lines # 6 and 7) but slight adjustments needed.

You need:

- find minimum length from list elements
- find elements which length is equal to minimum length

>>> lst = [['1', '6', '66', '9', '33'], 
...        ['1', '6', '9', '33'], 
...        ['1', '66', '6', '9', '33'], 
...        ['1', '66', '9', '33'], 
...        ['1', '4', '9', '33']] 
>>> shortest = min(len(row) for row in lst) 
>>> shortest
4
>>> [row for row in lst if len(row) == shortest] 
[['1', '6', '9', '33'], ['1', '66', '9', '33'], ['1', '4', '9', '33']]
(Jul-09-2019, 06:58 AM)perfringo Wrote: [ -> ][['1', '6', '9', '33'], ['1', '66', '9', '33'], ['1', '4', '9', '33']]

Thank you very much I get the expected results ! :)

Is it possible to get the results in this format?

Output:
1 6 9 33 1 66 9 33 1 4 9 33
What do you mean by 'this format'? Do you want to print result on screen? If printing on screen then you can do joining-unpacking or joining-joining, something like below:

>>> m = [['1', '6', '9', '33'], 
...      ['1', '66', '9', '33'], 
...      ['1', '4', '9', '33']]
>>> print(*[' '.join(row) for row in m], sep='\n')
1 6 9 33
1 66 9 33
1 4 9 33
>>> print('\n'.join([' '.join(row) for row in m]))
1 6 9 33
1 66 9 33
1 4 9 33
(Jul-09-2019, 07:49 AM)Amniote Wrote: [ -> ]Is it possible to get the results in this format?

Have you tried printing it?