Having the list say [(4.23, 6.43), 1.23, 3.444, (1.342, 3.212)]
, my code should separate tuples and integers and then multiply numbers in tuples(to obtain a rectangle square), also get the circle square (3,14 * radius ** 2
using integers in the list as radius, e.g. as I try to perform in my code. Then I need to compare all results and return a sorted list starting with the lowest value which was got as a result, but the list should have an appearance like the initial one, but sorted something like this:[ ( 1.342, 3.212 ), 1.23, ( 4.23, 6.43 ), 3.444 ]
. I indexed all list items suggesting using indexes in any way to order the list and return the required sorted list.
def sort_by_area(seq):
indexed_list = [item for item in enumerate(seq)]
radius_list = []
S_rectangle_list = []
S_circle_list = [(3,14 * [item][0] **2) for item in radius_list] # getting circle square
for id, i in enumerate(seq):
if type(i) is tuple:
for item in [i]:
S_rectangle_list.append((id,(item[0] * item[1])))
elif type(i) is float:
for item in [i]:
radius_list.append((id, item))
print(S_circle_list)
sort_by_area([(4.23, 6.43), 1.23, 3.444, (1.342, 3.212)])
so, I have two questions:
1. How to get a circle square in my case because I obtain empty S_circle_list = []
2. could you suggest how to return the initial appearance to the ordered list like in the above sample?
I hope I was clear ).
CodePudding user response:
def sort_by_area(seq):
area_list = []
s_rectangle_list = []
s_circle_list = []
for id, i in enumerate(seq):
if type(i) is tuple:
for item in [i]:
s_rectangle_list.append((id, [(item[0], item[1]), (item[0] * item[1])]))
area_list.append((id, [(item[0], item[1]), (item[0] * item[1])]))
elif type(i) is float:
s_circle_list.append((id,[i,(3.14*(i**2))]))
area_list.append((id,[i,(3.14*(i**2))]))
area_list.sort(key=lambda y: y[1][1])
result = map(lambda y: y[1][0], area_list)
print(s_rectangle_list) # [(0, [(4.23, 6.43), 27.198900000000002]), (3, [(1.342, 3.212), 4.310504000000001])]
print(s_circle_list) # [(1, [1.23, 4.750506]), (2, [3.444, 37.24396704])]
return list(result)
print(sort_by_area([(4.23, 6.43), 1.23, 3.444, (1.342, 3.212)])) # [(1.342, 3.212), 1.23, (4.23, 6.43), 3.444]
To answer your first question why s_circle_list is empty? Because you have run for loop for an empty array radius_list on line number 5 which you have added values later. For the second question, hope the code helps.