![](/img/trans.png)
[英]What is the best approach to matching/joining elements in two non-identical unsorted python lists of different lengths?
[英]Joining two arrays/lists of different lengths and setting the correct axis for joining
我要加入的列表是兩個日期列表,它們在一個日期上的價值大於另一個。
dates2008 = [2008-12-29 00:00:00 2008-12-30 00:00:00 2008-12-31 00:00:00]
dates2009 = [2009-01-01 00:00:00 2009-01-02 00:00:00]
我認為可以通過numpy append完成:
a = np.array(dates2008)
b = np.array(dates2009)
mergedlist = np.append(a, b)
我得到了按順序加入的新日期列表,但是這改變了我想要保持不變的尺寸,並且我不知道要在哪個軸上加入它們。 有人可以幫忙嗎。
如果我正確理解了您想要的內容,則可以使用numpy.hstack()
合並這兩個numpy.ndarray
:
import numpy as np
import datetime
dates2008 = [datetime.datetime(2008, 12, i) for i in range(29,32)]
dates2009 = [datetime.datetime(2009, 1, i) for i in range(1,3)]
a = np.array(dates2008)
b= np.array(dates2009)
merged = np.hstack((a,b))
## output ##
print "a = {0}".format(a)
print "b = {0}".format(b)
print "merged = {0}".format(merged)
將輸出:
a = [datetime.datetime(2008, 12, 29, 0, 0)
datetime.datetime(2008, 12, 30, 0, 0)
datetime.datetime(2008, 12, 31, 0, 0)]
b = [datetime.datetime(2009, 1, 1, 0, 0) datetime.datetime(2009, 1, 2, 0, 0)]
merged = [datetime.datetime(2008, 12, 29, 0, 0)
datetime.datetime(2008, 12, 30, 0, 0)
datetime.datetime(2008, 12, 31, 0, 0) datetime.datetime(2009, 1, 1, 0, 0)
datetime.datetime(2009, 1, 2, 0, 0)]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.