How do i sort a 2D array or multiple arrays by the length of the array using bubble sort

2024/7/4 14:38:51

trying to write a Python function: def compare_lengths(x, y, z)

which takes as arguments three arrays and checks their lengths and returns them as a triple in order of length.

For example, if the function takes [1,2,3], [10,20,30,40] and [65,32,7] as input, want it to return either ([1,2,3], [65,32,7], [10,20,30,40]) or ([65,32,7], [1,2,3], [10,20,30,40])

it can take it as either:

Array = [1,2,3],[10,20,30,40],[65,32,7]

or:

x = [1,2,3]
y = [10,20,30,40]
z = [65,32,7]

but it needs to be sorted as either:

([1,2,3], [65,32,7], [10,20,30,40])

or:

([65,32,7], [1,2,3], [10,20,30,40])

using bubble sort

Answer

You can do this, the only difference being the condition used is the length of the array instead of an individual value.

n = len(arr)
for i in range(n):for j in range(n-i-1):if len(arr[j]) > len(arr[j+1]):arr[j], arr[j+1] = arr[j+1], arr[j]
https://en.xdnf.cn/q/120025.html

Related Q&A

How to split a string in Python by 2 or 3, etc [duplicate]

This question already has answers here:Split string every nth character(21 answers)How to iterate over a list in chunks(40 answers)Closed 10 years ago.Does anyone know if its possible in python to spli…

.LAS into a .CSV file using python

How to change a .las file into a .csv file? Have been trying myself but no luck no far. I am just looking for something decently short that will save some time when I have to convert big .olas files i…

using pandas read_excel to read from stdin

Note: I have solve this problem as per below:I can use to_csv to write to stdout in python / pandas. Something like this works fine:final_df.to_csv(sys.stdout, index=False)I would like to read in an a…

How to print a string x times based on user input [duplicate]

This question already has answers here:How can I read inputs as numbers?(10 answers)Understanding for loops in Python(4 answers)Closed 1 year ago.I am trying to make a simple application that will pri…

How does .join work in Python?

I want to display each row of a SQL query result on a webpage. I found some code, but I dont understand what this line does.u"<br>".join([u"{0}".format(row.combination) for r…

Login, Navigate and Retrieve data behind a proxy with Python

I want, with a python script, to be able to login a website and retrieve some data. This behind my companys proxy.I know that this question seems a duplicate of others that you can find searching, but …

overflow in exp, python

cant really figure out why this error RuntimeWarning: overflow encountered in exp is showing up. The function Im trying to implement is:Id = lambda t_u, yp: Is * (np.exp((Vin(t_u) - L*yp)/(n_Ut*Ut)) - …

Getting a matlab codes results on python

I have code in matlab whose results I would like to use in python code (as a matrix or as a .dat file). Could anyone tell me how this could be done?

My Status object is not saving in Django

When I enter the shell, I run into the following problem:from users.models import Status from django.utils import timezoneStatus.objects.all() >>> [] p = Status() p.status_time = timezone.date…

Too many instances are running for Django Server

When Django server gets started, I can see only one instance of Django server running in the background. But after a while, I can see multiple instances are running.Output:root@GoldenGate:~# ps |grep p…