1

I have a series of attributes from different tables that I need to check against. In total there are about 40 items, like

origin, age, amount, etc

I have not really figured out the best way to collect data yet, maybe pull the data from the different sources and create a dict

i created a function for each test.

def age(value):
    if value < 3:
        Score = 1
    elif 3 <= value < 5:
        Score = 7
    elif value >= 5:
        Score = 10
    else:
        Score = 10
    Weight = 5
    return (Score, Weight)

def origin(value):
    if value.upper() == "USA":
        Score = 1
    elif value.upper() == "JAPAN":
        Score = 1
    elif value.upper() == "CHINA":
        Score = 10
    elif value.upper() == "OTHER":
        Score = 7
    else:
        Score = 10
    Weight = 1
    return (Score, Weight)

I take the values and append to a list

[[(7, 1)], [(1, 1)]]...

then

## calling all 40 functions doesnt seem efficient
lst.append([age(4)])
lst.append([origin("JAPAN")])


score = [item[0][0] for item in lst]
weight = [item[0][1] for item in lst]
scoresum = (listsum(score))
weightsum = (listsum(weight))

total = scoresum/weightsum

Does anyone know a better way to accomplish? I dont know, but it seems there has to be a better way

Messak
  • 423
  • 1
  • 3
  • 16

1 Answers1

1

If you stick with summing the tuples, you can simplify that operation by using this trick (taken from this answer):

# Option 1
[sum(x) for x in zip(*lst)]

# Option 2
map(sum, zip(*l))

This will result in a list of the sums (e.g. [25, 32]), which you can then use to compute the total.

You will need to have a flat list of tuples, however, so that's a change you may want to make. The list should look something like the following:

[(2, 1), (1, 1), ...]
Jonah Bishop
  • 12,279
  • 6
  • 49
  • 74