问题
This is an example: I have four time periods labeled as t=0, t=1, t=2, t=3. Each time has a value associated with it as shown below:
the format of the text file is as follows:
0,213
1,-999
2,456
3,-1100
Basically, each of the value is in one period. What I want to do is to use those values and get all the values to t=0. If I draw a timeline, then I would have two values on the positive at t=0 and t=2, and two values on the negative at t=1 and t=3.
Now, I want to go from the right hand side of the timeline to the left to get to t=0. So, at t=3, which is the last value in the timeline needs to move two units to the left to be added to the value of t=1 because they both are on the negative side, then finally move that value from t=1 to t=0. Similarly, I will need to do that for the positive side.
Following is my code. It may not be correct, but I am trying:
import numpy as np
workspace = 'C:\\Users\MM\Desktop'
time= np.loadtxt(workspace +'\\Assign1.txt', delimiter = ',', usecols = range(1))
value = np.loadtxt(workspace +'\\Assign1.txt', delimiter = ',', usecols = range(1,2))
for x in time:
x+1;
print(x)
for y in value:
y+1
print(y[-1])
# I want this to read the last value(-1100) from the array
# of the text file I have. I already got the values
# pulled from the text file. It gives me an error.
If I do get that to work, then since this is a negative value, I need to add that to the previous negative value, and so on.
The content I have is just a sample. There could be more or less than 4 time values, and the negative values and positive values could be anywhere on the timeline. The goal is to get the all the negative and positive values in t=0 and see if the negative values equals the positive values or not. The values needed to be considered equal if one of the values is greater by 1 but smaller than or equal to 15.
回答1:
Algebraically, what you describe is much simpler than the process you've laid out. You add up all the numbers and see whether the result is in the range [-15, 15]. Replace your two intended loops with this conditional check:
if -15 <= sum(value) <= 15:
# Here, insert the code for matching stuff.
As for why your given code fails ... the loops are not correct. I'm not sure what you're trying to do with the two +1 expressions; they don't affect the data flow in any way, because you don't store the value.
The second loop is confused. In each iteration, you take a single value from the list. Your print statement treats that single value as if it were a list, too (you can make a list of lists in Python, but this program doesn't do that). When you try to access the last element of a single integer, Python informs you of the confusion.
To print the last value of the list, simply use print value[-1]; don't iterate through the entire list to find the last item.
That said, we now have the original problem: how to sum the negative and positive values using the algorithm you describe. You need to run your lists in the reverse order. I'll do this in two loops, one each for positive and negative:
time = range(10)
value = [213, -999, 456, -1100, -16, 5, 42, -80, 9, 10]
last_neg = 0
for i in range(len(value)-1, -1, -1): # Go through the list in reverse order
if value[i] < 0:
value[i] += last_neg
last_neg = value[i]
last_pos = 0
for i in range(len(value)-1, -1, -1): # Go through the list in reverse order
if value[i] > 0:
value[i] += last_pos
last_pos = value[i]
print "pos & neg sums", value[0], value[1]
value[0] += value[1]
print "grand sum", value[0]
print "Check the summation:", value
Output:
pos & neg sums 735 -2195
grand sum -1460
Check the summation: [-1460, -2195, 522, -1196, -96, 66, 61, -80, 19, 10]
来源:https://stackoverflow.com/questions/35140621/read-and-add-the-last-value-from-an-array-pulled-from-a-text-file