Reputation: 535
I have a sh**y list of strings with weather info by hours, here it is:
bad_list =['00:00', '-2°C', '-6°C', '320°13 Km/h', 'N/A', '74%', '-6°C', '1025,0mb', '',
'01:00', '-1°C', '-3°C', '320°6 Km/h', 'N/A', '75%', '-5°C', '1024,0mb', '',
'02:00', '-3°C', '-5°C', '270°6 Km/h', 'N/A', '86%', '-5°C', '1023,0mb', '',
.....(skipped hours from 03 till 09)
'09:04', '9°C', '5°C', '290°35 Km/h', 'N/A', '66%', '3°C', '1022,0mb', '',
'10:00', '9°C', '5°C', '290°37 Km/h', 'N/A', '62%', '2°C', '1022,0mb', '',
'10:27', '10°C', '6°C', '280°39 Km/h', 'N/A', '58%', '2°C', '1023,0mb', '',
'11:02', '11°C', '11°C', '290°35 Km/h', 'N/A', '54%', '2°C', '1022,0mb', '',
'11:10', '12°C', '12°C', '290°37 Km/h', 'N/A', '47%', '1°C', '1022,0mb', '',
.....(skipped)
'23:00', '3°C', '3°C', 'N/A', '52%', '-6°C', '1020,0mb', '',
]
The problem is that in the list, hour strings are messy like the example, there is '10:00' and '10:27'. What I'm trying is to collect temp (next index string after an hour) for each hour (from 00:00 till 23:00). There are more than 24 strings for an hour (and the corresponding weather info!) in that list. So I was thinking to find the first occurrence for an hour and then to get the next index from the list as the corresponding temperature:
unique_time = ['00:','01:','02:','03:','04:','05:','06:','07:','08:','09:','10:','11:','12:','13:','14:','15:','16:',
'17:','18:','19:','20:','21:','22:','23:']
sorted_time_list = next(x for x in unique_time if x in bad_list) #not working
And to get +1 index string corresponding to sorted_time_list in bad_list. I know it sounds ugly but I want to get temp string (second element after hour string in bad_list) for only 24 hours.
I know it sounds little unclear. If need more details please shoot me :)
Upvotes: 2
Views: 75
Reputation: 34
I am a green hand in python, I am try to use own method to solve this question. Followed are my code.
final_result={}
for x, y in enumerate(unique_time):
if (y in bad_list):
final_result[y]=bad_list[x+1]
print(final_result)
Upvotes: 0
Reputation: 1125058
Group your items into 9 items (using a grouper iterator), then group by the hour of each group:
from itertools import groupby, zip_longest
def grouper(iterable, n, fillvalue=None):
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)
by_hour = groupby(grouper(bad_list, 9), lambda g: g[0][:2])
temp_list = [(hour, next(entries)[1]) for hour, entries in by_hour]
This produces a list of (hour, temp)
tuples, where the first temperature for the given hour is produced.
Demo on your given data:
>>> from itertools import groupby, zip_longest
>>> def grouper(iterable, n, fillvalue=None):
... args = [iter(iterable)] * n
... return zip_longest(*args, fillvalue=fillvalue)
...
>>> by_hour = groupby(grouper(bad_list, 9), lambda g: g[0][:2])
>>> [(hour, next(entries)[1]) for hour, entries in by_hour]
[('00', '-2°C'), ('01', '-1°C'), ('02', '-3°C'), ('09', '9°C'), ('10', '9°C'), ('11', '11°C'), ('23', '3°C')]
Upvotes: 4