3
['2016-01-01 00:00:00', '0',
 '2016-01-01 08:00:00', '268705.0',
 '2016-01-01 16:00:00', '0',
 '2016-01-02 00:00:00', '0',
 '2016-01-02 08:00:00', '0.0',
 '2016-01-02 16:00:00', '0.0',
 '2016-01-03 00:00:00' ...
 ... etc for 1 year]

I basically have a date and energy production as a int after it. I want to make it so that it looks like

['2016-01-01 00:00:00;0',
 '2016-01-01 08:00:00;268705.0',
 ... etc]

aka ['date;energy']

Any tips? I am new to this and need this to get through my course...

Christian König
  • 3,437
  • 16
  • 28

2 Answers2

3

Use zip() and list-slicing to zip every 2nd element starting at index 0 with every 2nd element starting at index 1:

data = ['2016-01-01 00:00:00', '0',
        '2016-01-01 08:00:00', '268705.0',
        '2016-01-01 16:00:00', '0',
        '2016-01-02 00:00:00', '0',
        '2016-01-02 08:00:00', '0.0',
        '2016-01-02 16:00:00', '0.0',
        '2016-01-03 00:00:00', "18.05",]

new_data = list(zip(data[0::2],data[1::2]))

print(new_data)

combined = ["{};{}".format(a,b) for a,b in new_data]

print(combined)

Output:

# new_data (I would vouch to use that further on)
[('2016-01-01 00:00:00', '0'), ('2016-01-01 08:00:00', '268705.0'), 
 ('2016-01-01 16:00:00', '0'), ('2016-01-02 00:00:00', '0'), 
 ('2016-01-02 08:00:00', '0.0'), ('2016-01-02 16:00:00', '0.0'), 
 ('2016-01-03 00:00:00', '18.05')]

# combined
['2016-01-01 00:00:00;0', '2016-01-01 08:00:00;268705.0', '2016-01-01 16:00:00;0',
 '2016-01-02 00:00:00;0', '2016-01-02 08:00:00;0.0', '2016-01-02 16:00:00;0.0', 
 '2016-01-03 00:00:00;18.05']

If I were you I would not str-combine them , but work further using the tuples. F.e. if you want to summ up energies per day:

new_data = sorted((zip(data[0::2],data[1::2])))

from itertools import groupby 

# x[0][0:10] uses the 1st element of each tuple (the datetime) and slices only the date
# from it. That is used to group all data.
k = groupby(new_data, lambda x:x[0][0:10])

summs = []
for date,tups in k:
    summs.append( (date,sum(float(x[1]) for x in tups)) )

print(summs)

Output:

[('2016-01-01', 268705.0), ('2016-01-02', 0.0), ('2016-01-03', 18.05)]
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Now I have another issue. Now that I have this list of '2016-01-01 00:00:00;0' etc.. I need to use timedelta somehow to add all energy production of a certain date, for example to the form of 1.1.2016;combined energy of that day. and then 2.1.2016;combined energy of that day. etc.. so one day has three dates and three energy sums and they need to be combined for one day and one energy sum. and it needs to be in the form of d.m.year as i wrote. – anonymous33243433 Nov 15 '18 at 12:58
  • ['01.01.2016;0', '01.01.2016;268705.0', '01.01.2016;0', '02.01.2016;0', '02.01.2016;0.0', '02.01.2016;0.0', '03.01.2016;0.0'] Okay now my list looks like this, I just now need it to be like 01.01.2016;sum of energy production of that day – anonymous33243433 Nov 15 '18 at 13:27
  • @anonymous33243433 this is alltogether a new problem and should be a new question. I added code to show you how to do it using the tuple approach. You can str-join the values yourself using the code above usign the grouped results. You need to sort the date-tuples, hence I changed `list` to `sorted` so `groupby` works. – Patrick Artner Nov 15 '18 at 13:30
  • and one more thing the date has to actually look like 1.1.2016 --- 2.1.2016 etc.. not 01.01.2016 gahhhh more things to do – anonymous33243433 Nov 15 '18 at 13:34
  • @anonymous33243433 your task - not mine. you can either use creative datestring slicing or convert your date into a datetimeobject and back to a string using usìng `datetime.strptime` and `datetime.strftime` - plenty on questions on this forum, f.e. here: https://stackoverflow.com/questions/311627/how-to-print-a-date-in-a-regular-format - your third question - but there are of plenty dupes around: https://stackoverflow.com/questions/466345/converting-string-into-datetime – Patrick Artner Nov 15 '18 at 13:37
1

use a zip to generate a zipped list of your needed values and then use join to join the list with a ;

>>> a=['2016-01-01 00:00:00', '0', '2016-01-01 08:00:00', '268705.0', '2016-01-01 16:00:00', '0']
>>> [';'.join(i) for i in zip(a[::2],a[1::2])]
['2016-01-01 00:00:00;0', '2016-01-01 08:00:00;268705.0', '2016-01-01 16:00:00;0']
>>> 
Albin Paul
  • 3,330
  • 2
  • 14
  • 30