Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Minimum size
#1
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
Reply
#2
(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).
Reply
#3
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]
Reply
#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 ...
Reply
#5
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
Reply
#6
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']]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#7
(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
Reply
#8
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
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#9
(Jul-09-2019, 07:49 AM)Amniote Wrote: Is it possible to get the results in this format?

Have you tried printing it?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to get indices of minimum time difference Mekala 1 2,147 Nov-10-2020, 11:09 PM
Last Post: deanhystad
  How to get index of minimum element between 3 & 8 in list Mekala 2 2,511 Nov-10-2020, 12:56 PM
Last Post: DeaD_EyE
  Finding MINIMUM number in a random list is not working Mona 5 3,035 Nov-18-2019, 07:27 PM
Last Post: ThomasL
  Delete minimum occurence in a string RavCOder 10 3,920 Nov-12-2019, 01:08 PM
Last Post: RavCOder
  size of set vs size of dict zweb 0 2,137 Oct-11-2019, 01:32 AM
Last Post: zweb
  Armstrong in minimum lines Gaurav 1 1,994 Sep-03-2018, 03:46 PM
Last Post: j.crater
  Watson Personality Insight: minimum number of words kiton 0 2,732 Apr-28-2018, 03:28 PM
Last Post: kiton
  CSV file created is huge in size. How to reduce the size? pramoddsrb 0 10,477 Apr-26-2018, 12:38 AM
Last Post: pramoddsrb
  Python find the minimum length of string to differentiate dictionary items zydjohn 3 3,612 Mar-03-2018, 05:23 PM
Last Post: Gribouillis
  Product of maximum in first array and minimum in second Thethispointer 9 5,273 Jan-19-2018, 07:38 PM
Last Post: Thethispointer

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020