~ / archivo / stackoverflow / 50379319
Python for loops not iterating over 2nd condition correctly
-
R_J pregunta 2018-05-16 20:21
I have a long list of tuples that take the form
num_list = [('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A9', 19, 'BB', 919.21), ('A4', 14, 'CC', 109.80)]The first element of each tuple corresponds to an ID, the second what day an event occurred, the third the category of the event and the fourth the value of the event.
The task is to print out the maximum value in the previous 5 days, eg on day 6 only events from days 1 to 5 should be considered, for each separate ID i.e. something of the basic form
Day ID Max 7 A1 400 7 A2 350 8 A1 750Currently I have the following code after num_list is declared where ID is a set of all the ID values. 14 and 18 are used as the days goes up to day 19
first_value = 1 fifth_value = 5 for id in ID: while first_value <= 14 and fifth_value <= 18: result = max([i for i in num_list if i[1] <= fifth_value and i[1] >= first_value and i[0] == id], key = lambda x:x[3]) first_value += 1 fifth_value += 1 print(f"result[0]} {result[1]} {result[3]}")The problem is that this is only returning the maximum results for the first ID, A1 in this case. This it is doing correctly but I'm not sure why it's not doing it for every ID. I've checked and before the while loop it is returning every ID so I'm not sure what the problem is here
Thanks in advance, sorry if something similar has been posted before but I couldn't find it
- tobias_k 2018-05-16 21:02 Isn't there an
{missing in yourfstring? - Miguel Ortiz yo 2018-05-16 21:13 I've added a little snippet of code to find the highest value on a range of days. Hope you take a look on it.
- tobias_k 2018-05-16 21:02 Isn't there an
-
I'm not sure what you're after, but here are my 2 cents. Note that
idis a reserved variable name in Python. (hence the usage ofid_)#Let's say we have the data shown in the `num_list`. num_list = [('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A9', 19, 'BB', 919.21), ('A4', 14, 'CC', 109.80), ('A7', 6, 'DD', 243.12), ('A3', 11, 'GG', 612.21)] #First, we want to sort it by the `day` of occurence. Then we want to list all the possible IDs. sorted_list = sorted(num_list, key=lambda x:x[1]) ID = sorted(list(set(x[0] for x in num_list))) # Then, let's define a function, which will get the entry/row with the maximum value # We give the function two arguments. The `day`, which is the day of observation. # If the day of observation is 6, then, only look at the entries which have day of the event 1-5 # The another argument is the id_ we are looking at ('A1', 'A4', ..etc.) def get_max_within_time_window(day, id_): # Get all the entries with the wanted id entries_id = [x for x in sorted_list if x[0] == id_] # Get all the entries within the 5-day time window. The day of observation not included. entries_filtered = [x for x in entries_id if 0 < day-x[1] <= 5] # In case of zero matches, return empty list if not entries_filtered: return [] # Return the event with highest `value` return max(entries_filtered, key=lambda x:x[3]) # Now, let's put this in action. # Day*: The day of observation # Day: The day of event occurence. If day of observation is 6, then day of occurence can be 1, 2, 3, 4 or 5. print('Day*\tDay\tID\tMax') for day in range(20): for id_ in ID: found = get_max_within_time_window(day, id_) if not found: continue max_val = found[3] day_occured = found[1] print(f'{day}\t{day_occured}\t{id_}\t{max_val}')Results
Day* Day ID Max 5 4 A1 977.98 6 4 A1 977.98 7 4 A1 977.98 7 6 A7 243.12 8 4 A1 977.98 8 6 A7 243.12 9 4 A1 977.98 9 6 A7 243.12 10 6 A7 243.12 11 6 A7 243.12 12 11 A3 612.21 13 11 A3 612.21 14 11 A3 612.21 15 14 A1 386.42 15 11 A3 612.21 15 14 A4 249.12 16 14 A1 386.42 16 11 A3 612.21 16 14 A4 249.12 17 14 A1 386.42 17 14 A4 249.12 18 14 A1 386.42 18 14 A4 249.12 19 14 A1 386.42 19 14 A4 249.12- R_J 2018-05-16 21:45 If I wanted to add another column with the average in the same range after the max column, could I define it in the same way as here?
- Niko Fohr 2018-05-17 06:30 Sure. If the average is calculated from the set
entries_filteredinget_max_within_time_window, you could add the calculation of the average in that function (and perhaps rename it toget_max_and_average). Should you have any problems with this, you may ask here in the comments or add a new question. - R_J 2018-05-17 06:59 I ended up defining a new function get_avg_within_time_window in a similair way here. I used np.mean on the return section but I'm getting the following error File "Question_3.py", line 59, in get_avg_within_time_window return np.mean(entries_filtered_avg, key = lambda x:x[3]) TypeError: mean() got an unexpected keyword argument 'key'
-
With your example I can access quite easily to any range of tuples:
#!/usr/bin/python num_list = [('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A9', 19, 'BB', 919.21), ('A4', 14, 'CC', 109.80)] print [item for item in num_list if item[1] < 19 ]Output:
mortiz@alberta:~/Documents/projects/python$ python tuples.py [('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A4', 14, 'CC', 109.8)]If you want a range then change it using range():
print [item for item in num_list if item[1] in range(5,19) ]Output:
mortiz@alberta:~/Documents/projects/python$ python tuples.py [('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A4', 14, 'CC', 109.8)]Finally, the highest value of the selected range:
#!/usr/bin/python num_list = [('A1', 4, 'FF', 977.98), ('A4', 14, 'CC', 249.12), ('A1', 14, 'EE', 386.42), ('A9', 19, 'BB', 919.21), ('A4', 14, 'CC', 109.80)] result=[item for item in num_list if item[1] in range(5,19)] highest=[item[3] for item in result] print max(highest)Output (highest value)
mortiz@alberta:~/Documents/projects/python$ python tuples.py 386.42Is that what you want?
-
Tané Tachyon respuesta 2018-05-16 21:02
First, you have a missing opening curly brace for
result[0]in this lineprint(f"result[0]} {result[1]} {result[3]}")but your main problem is that in your while loop,first_valueandfifth_valuekeep getting incremented while the while loop runs for the first id, but then they are never reset, so for each subsequent id you never enter the while loop.- R_J 2018-05-16 21:06 Yeah missed the { when I copied and pasted the code in. How would you recommend that I reset the loop then
- Tané Tachyon 2018-05-16 21:10 If I understand how your code is supposed to work, just move your initialization of
first_valueandfifth_valueto right above and at the same level of indentation as your while statement. - R_J 2018-05-16 21:18 Thanks that now works for the first two IDs i.e. prints out the 14 sets of data for the first two IDs. But after two entries for the third ID i get ValueError: max() arg is an empty sequence
- Tané Tachyon 2018-05-16 21:22 If your list comprehension is returning an empty list because nothing in
num_listmatchesi[1] <= fifth_value and i[1] >= first_value and i[0] == id, then you'll get an error trying to find the max of an empty list. - R_J 2018-05-16 21:31 So why do you think it's returning an empty list but only after a number of outputs have been printed to the console
- Tané Tachyon 2018-05-16 21:40 Presumably because as you're looping through
idvalues and incrementingfirst_valueandfifth_valuefor some combinations nothing innum_listwill matchi[1] <= fifth_value and i[1] >= first_value and i[0] == id. For debugging purposes, have your code print the values ofidandfirst_valueandfifth_valueat the beginning of each iteration of the while loop, so that when it crashes you can see exactly which values are not there innum_list, and think about how you want to handle that.