我有此代码:
compareList[productName] = productID + ',' + productHref;
console.log(productName + ' ' + productID + ' ' + productHref + ' ' + compareList.length);
登录到哪个帐户(我已删除链接):
Acer Iconia B1-790 [NT.LDFEE.002] 112576 为保密起见,链接已删除 0
如您所见,所有三个变量都是有效的字符串,但是json对象仍然无法分配(compareList.length日志为0)。我一直在思考,但我根本无法弄清楚。任何帮助表示赞赏。
答案 0 :(得分:0)
也许这种添加和检查数组长度的版本对您有用吗?
from math import floor
def mergeSort(a): # mergesort function
if len(a)<2: # if length of a < 2, return a
return a
mid=int(len(a)/2) # mid point
x=mergeSort(a[:mid]) # recursivly call mergesort for 0 to mid
y=mergeSort(a[mid:]) # recursivly call mergesort from mid to end
result=[] # empty list
i=j=0 # initialize i and j
while j<len(x) and i<len(y):
if x[j]<y[i]: # if x[j] < y[i], then append result for x[j]
result.append(x[j])
j+=1 # increment j by 1
else:
result.append(y[i]) # append result for y[i]
i+=1 # increament i by 1
result+=x[j:] # add x[j:] --> result
result+=y[i:] # add y[i:] --> result
return result # return the result
def findMedian(a): # find the median
return mergeSort(a)[floor(len(a)/2)] # call mergesort
def choosePivot(a): # choose pivot
medians=[] # empty list
j=0 # initialize j
if len(a)==1: # if the len = 1, print the element
print (a[0])
return a[0]
if 5<len(a):
medians.append(findMedian(a[0:5])) # call findMedian for 0 to 5
else:
medians.append(findMedian(a)) # call findMedian for a
for i in range(1,floor(len(a)/5)): # divide the input array into 5 groups
if i*5<len(a):
medians.append(findMedian(a[j*5:i*5])) # call findMedian
else:
medians.append(findMedian(a[j*5:len(a)]))
return choosePivot(medians) # return choosePivot medians
def partition(array,pivot): # partition
array[0],array[pivot]=array[pivot],array[0] #swap
j=1 # intiatalize
for i in range(1,len(array)):
if array[i]<array[0]:
array[i],array[j-1]=array[j-1],array[i] #Swap the number less than the pivot
j+=1
array[1],array[j]=array[j],array[1] #swap the pivot to its rightful place
return j-1,array #return index of pivot and the partitioned array
def Selection(array,k): # selection function
p=choosePivot(array)
if k>len(array): # if k > length of array, then return -1
print ("Out of array index")
return -1
if len(array)>1: # if len(array) >1, then
x,b=partition(array,p) # call partition func
if x>k:
c = Selection(b[:x],k) # search the left half for the statistic
return c # return c
elif x<k:
d= Selection(b[x+1:],k-x-1) # search the right half for the statistic
return d # return d
else:
return array[k] # return the element if statistic is found
else:
return array[0] #Only one element. Only solution, return as it is.
print (Selection([5,1,48,6,2,4,8,7,5,63,2,1,4,8,99],13))