Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Python 2.6.4 - Integer list operations


ghost's Avatar
0 0

I need to find a way to compare a number of integers that have been assigned to variables, then drop the lowest out of the list. Here's an example of what I've got.

d2 = random.randint(1,5)
d3 = 5 + d1 - d2```

After, I need a line immediately after that that will combine d1, d2 and d3 (addition) then figure out which is the lowest and drop it, or one of them if there are multiples of the same value. (or the other way around, drop the lowest and combine the rest)

I attempted this:
```markupdr1 = min(list('d1''d2''d3'))
s1 = d1 + d2 + d3 - dr1```
But the "min(list())" outputs a string, and we all know that "integer + string = error"

Is there a way to make a array/pool/list of integers and then either compare them all and select the lowest, or just drop the lowest from it then total.

This is just a small example. I had thought using "compare()", but the large pools would have too many compare() functions to code efficiently.

ghost's Avatar
0 0

Hmm…. well I see a few things that could be revised:

dr1 = min(list('d1''d2''d3'))

probably should be

dr1 = min([d1,d2,d3])

list() takes only one argument, a sequence type such as a tuple, and converts it to a list. Additionally, enclosing the variables in ' ' will probably result in a conversion to strings. (i'm a little surprised that didnt error…) using [] will create a list and then min, will operate properly (as the object it was handed previously was wrong)

I'm a little bit confused in exactly what you're trying to do here, but here's your code re-written to do what i think you're getting at. With debugging and output:

import random

d1 = random.randint(1,10)
d2 = random.randint(1,5)
d3 = 5 + d1 - d2

print "d1: %s" % d1
print "d2: %s" % d2
print "d3: %s" % d3

dr1 = min([d1,d2,d3])
print "dr1: %s" % dr1
s1 = d1 + d2 + d3 - dr1
print "s1: %s" % s1

--- output ---
d1: 1
d2: 4
d3: 2
dr1: 1
s1: 6

~samurai


ghost's Avatar
0 0

Thanks. Just what I needed. Works like a charm.