1

I am trying to figure out what is wrong with my code. Currently, I am trying to get the averages of everything with the same temp (ex temp 18 = 225 conductivity average, temp 19 = 15 conductivity average, etc).

Could someone tell me if this is a simple coding mistake or a algorithm mistake and offer some help to fix this problem?

temp = [18,18,19,19,20]
conductivity = [200,250,20,10,15]

tempcheck = temp[0];
conductivitysum = 0;
datapoint = 0;

assert len(temp) == len(conductivity)

for i in range(len(temp)):
    if tempcheck == temp[i]:
        datapoint+=1
        conductivitysum+=conductivity[i]
    else:
        print conductivitysum/datapoint
        datapoint=0
        conductivitysum=0
        tempcheck=temp[i]

For some reason, it is printing out

225

10

When it should be printing out

225

15

15

3
  • print float(conductivitysum)/datapoint Commented Nov 29, 2014 at 8:51
  • Yes, but that does not solve the problem. What I am getting in the output is 225, and then 10 Commented Nov 29, 2014 at 8:53
  • It's not a weird reason, that's what you told it to do. You reset datapoint to 0 instead of to 1, and conductivitysum to 0 instead of putting the current value in it. Commented Nov 29, 2014 at 8:54

3 Answers 3

3

in else clause put :

conductivitysum=0
datapoint=0
tempcheck = temp[i]
conductivitysum+=conductivity[i]
datapoint+=1

because when you go to else clause, you miss that particular conductivity of i. It doesn't get saved. So before moving to next i, save that conductivity

Sign up to request clarification or add additional context in comments.

3 Comments

I sure would like to know the reasons for downvote. That will be helpful in my learning process :)
I upvoted your first answer; when you added code and I read it, your code was wrong, so I removed my upvote. It may have looked like a downvote. Now you've edited again with code that looks right, I've re-upvoted.
thanks. Its just I gave lines to append after tempcheck = temp[i] assuming the OP will understand that rest all code has to be kept intact. After the downvote (which apparently wasn't one) I figured it wasn't good idea to give incomplete piece of code. So changed it back :) thanks for the explanation. Points reciprocated :)
2

Change the else to:

for i in range(len(temp)):
    if tempcheck == temp[i]:
        datapoint+=1
        conductivitysum+=conductivity[i]
    else:
        print conductivitysum/datapoint
        datapoint=1
        conductivitysum=conductivity[i]
        tempcheck=temp[i]

When you get to the pair (19, 20) you need to keep them and count one datapoint, not 0 datapoints. At the moment you are skipping them and only keeping the next one - (19, 10).

Alternatively, rewrite it as

>>> temp = [18,18,19,19,20]
>>> conductivity = [200,250,20,10,15]

# build a dictionary to group the conductivities by temperature
>>> groups = {}
>>> for (t, c) in zip(temp, conductivity):
...     groups[t] = groups.get(t, []) + [c]
...     

# view it
>>> groups
{18: [200, 250], 19: [20, 10], 20: [15]}

# average the conductivities for each temperature
>>> for t, cs in groups.items():
        print t, float(sum(cs))/len(cs)
... 
18 225
19 15
20 15
>>> 

Comments

0

When I saw this code, the first thing that popped into my head was the zip function. I hope that the following code is what you want.

temp = [18,18,19,19,20]
conductivity = [200,250,20,10,15]

assert len(temp) == len(conductivity)

# Matches each temp value to its corresponding conductivity value with zip

relations = [x for x in zip(temp, conductivity)]

for possible_temp in set(temp): # Takes each possible temparature (18,19,20)
    total = 0
    divide_by = 0

    # The next four lines of code will check each match and figure out the
    # summed total conductivity value for each temp value and how much it
    # should be divided by to create an average.

    for relation in relations:
        if relation[0] == possible_temp:
            total += relation[1]
            divide_by += 1
    print(int(total / divide_by))

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.