Originally i'd like to make a class that has a few string and float properties, one being a timestamp in float format. having a list of ~10'000 instances of that class i'd like use those 10'000 timestamps as if I had a numpy array for math operations of vectors:
import random
import time
class myclass:
def __init__(self):
# some properties
self.ID=random.random()
self.text1 = 'sometext'
self.text2 = 'some other text'
self.timestamp=time.time()
if __name__ == "__main__":
for i in range(10):
mylist.append(myclass())
# now apply math functions to the timestamps, for example
# max([all timestamps]) or sum([all timestamps])
I could just have a row in an array instead of a class instance, but then many advantages (not shown here) of using classes are lost.
Can you help me?
Use operator.attrgetter
to create a callable for the timestamp; use it with map
on the list of instances; consume the map object with sum
or max
.
from operator import attrgetter
tstamp = attrgetter('timestamp')
print(sum(map(tstamp, mylist)))
print(max(map(tstamp, mylist)))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.