This is probably a beginners question but I don't know how to search for an answer (because I cannot "name" the question)
I have 2 lists or a tuple of 2 lists
xxx = ["time1", "time2", "time3"]
yyy = ["value1", "value2", "value3"]
zzz=(xxx,yyy)
now I would like to create a list/tuple for every entry result should be
[['time1', 'value1'], ['time2', 'value2'], ['time3', 'value3']]
I'm able to do this with a for loop (and zip) but is there no "nicer" solution? Here is a similar question but I'm not able to use the resolution given there for my probelms
Use the builtin zip
function:
zzz = zip(xxx, yyy)
Of course, this creates a list of tuples (or an iterable of tuples in python3.x). If you really want a list of lists:
#list (python2.x) or iterable(python3.x) of lists
zzz = map(list,zip(xxx,yyy))
or
#list of lists, not list of tuples
#python 2.x and python 3.x
zzz = [ list(x) for x in zip(xxx,yyy) ]
If you're using python3.x and you want to make sure that zzz
is a list, the list comprehsion solution will work, or you can construct a list from the iterable that zip
produces:
#list of tuples in python3.x.
zzz = list(zip(xxx,yyy)) #equivalent to zip(xxx,yyy) in python2.x
#will work in python2.x, but will make an extra copy.
# which will be available for garbage collection
# immediately
I notice that your data includes time stamps and numbers. If you're doing number-intensive calculations, then numpy arrays might be worth a look. They offer better performance, and transposing is very simple. ( arrayname.transpose()
or even arrayname.T
)
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.