0

im trying to create a ranking system to order the fruits in the order of biggest to smallest:

is_big = [None,None,10,1/100,-1/100]

i need to add the values of is_big[i] corresponding with the indexes of fruits object

fruits = [['Pear','green',2,3,5],
          ['Apple','red',5,2,10],
          ['mango','yellow',4,6,12]]

to make the ranking system im giving a score based on the fruits element values and the values of is_big i need to multiply is_big[i] with fruits[i] and sum it up ,where is_big != None the expected results should be fruits_ranking =[[19.98],[49.92],[39.94]] so that after sorting fruits_ranking i get the results

fruits =[['Apple','red',5,2,10],
          ['mango','yellow',4,6,12],
          ['Pear','green',2,3,5]]

my code so far is:

rs = []
for i in range(len(fruits)):
      for c in range(len(is_big)):
              if battery[c]!= None and not isinstance(is_big[i],str):
                      rs.append(is_big[i]*fruits[c])

as you can see my code does not work any kind of help would be appreciated

1
  • what is phones? Commented May 1, 2020 at 21:49

3 Answers 3

1

One way is using below

[[i,sum(x*y for x,y in zip(i[2:],[0 if v is None else v for v in is_big][2:]))] for i in prod]

Output:

[[['Pear', 'green', 2, 3, 5], 19.98],
 [['Apple', 'red', 5, 2, 10], 49.92],
 [['mango', 'yellow', 4, 6, 12], 39.940000000000005]]
Sign up to request clarification or add additional context in comments.

Comments

1

You could use sorted() with a specific function score as key parameter that computes the score you need. The score you describe is the sum of a dot product with is_big, that's why converting to np.ndarray proves useful as it enables you to do dot products as easily as with a *!

import numpy as np

# avoid Nones, convert to np.ndarray to enable dot product
coeffs = np.array(is_big[2:])  

def score(input_fruit):
    input_numeric = np.array(input_fruit[2:])  # keep only numerical values
    return (input_numeric * coeffs).sum()  # dot product & sum

First, you can check that scores match your requirements:

>>> print(list(zip(fruits, map(score, fruits))))

[
    (["Pear", "green", 2, 3, 5], 19.98),
    (["Apple", "red", 5, 2, 10], 49.92),
    (["mango", "yellow", 4, 6, 12], 39.940000000000005),
]

Now sort:

# reverse=True for descending order
>>> print(sorted(fruits, key=score, reverse=True))  

[["Apple", "red", 5, 2, 10], ["mango", "yellow", 4, 6, 12], ["Pear", "green", 2, 3, 5]]

Comments

0

An itertools solution might be:

import itertools as it
import operator as op

is_big = [None, None, 10, 1/100, -1/100]
fruits = [["Pear", "green", 2, 3, 5], ["Apple", "red", 5, 2, 10], ["mango", "yellow", 4, 6, 12]]

fr_ranks = [sum(fr*val for fr, val in zip_one if val is not None)
            for zip_one in it.starmap(zip, zip(fruits, it.repeat(is_big)))]
# [19.98, 49.92, 39.940000000000005]

sorted_fruits, _ = zip(*sorted(zip(fruits, fr_ranks), key=op.itemgetter(1), reverse=True))
# (['Apple', 'red', 5, 2, 10], ['mango', 'yellow', 4, 6, 12], ['Pear', 'green', 2, 3, 5])

First, we align each fruit list in fruits with the value list is_big via repeating is_big and zipping it with the fruits, which will give 2-tuples of such list pairs when evaluated. Then it.starmap, with its function argument being zip, generates the desired fruit-value pairs that will reside in zip_one. A sum of products while skipping None's will give the fruit rankings.

Then we sort the fruits and this is sorting two parallel lists where the key is the corresponding element of fr_ranks. This gives a tuple but you can easily cast to list.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.