Python:合并字典,同时合并一些键,但不合并其他键?

时间:2018-11-28 18:27:43

标签: python

我正在尝试格式化许多电影院的电影列表。大多数影片放映许多相同的电影,但时间不同。我正在使用.csv。

以下是导入并创建列表作为字典后的示例:

def my_decision_stump(X, D, y):
    Fs = np.inf

    optimal_j = None
    optimal_b = None
    optimal_theta = None

    m, d = X.shape
    for j in range(d):
        record = np.hstack((X[:, j].reshape(-1, 1), D.reshape(-1, 1), y.reshape(-1, 1)))
        record_sorted = record[record[:, 0].argsort()]
        x = record_sorted[:, 0]; D = record_sorted[:, 1]; y = record_sorted[:, 2]
        x = np.hstack((x, x[-1]+1))

        F_pos = np.sum(D[y == 1])
        F_neg = np.sum(D[y == -1])

        if F_pos < Fs or F_neg < Fs:
            optimal_theta = x[0] - 1; optimal_j = j
            if F_pos < F_neg:
                Fs = F_pos; optimal_b = 1
            else:
                Fs = F_neg; optimal_b = -1

        for i in range(m):
            F_pos -= y[i] * D[i]
            F_neg += y[i] * D[i]
            if (F_pos < Fs or F_neg < Fs) and x[i] != x[i+1]:
                optimal_theta = 0.5 * (x[i] + x[i+1]); optimal_j = j
                if F_pos < F_neg:
                    Fs = F_pos; optimal_b = 1              
                else:
                    Fs = F_neg; optimal_b = -1       
    return (optimal_j, optimal_b, optimal_theta)

def decision_stump(X, D, y):
    Fs = np.inf

    optimal_j = None
    optimal_b = None
    optimal_theta = None

    m, d = X.shape
    for j in range(d):
        index = np.argsort(X[:, j])
        x = np.zeros(m+1)
        x[:-1] = X[index, j]
        x[-1] = x[-2] + 1
        F_pos = np.sum(D[y == 1])
        F_neg = np.sum(D[y == -1])
        if F_pos < Fs or F_neg < Fs:
            optimal_theta = x[0] - 1; optimal_j = j
            if F_pos < F_neg:
                Fs = F_pos; optimal_b = 1                   
            else:
                Fs = F_neg; optimal_b = -1

        for i in range(m):
            curr_idx = index[i]
            F_pos -= y[curr_idx] * D[curr_idx]
            F_neg += y[curr_idx] * D[curr_idx]
            if (F_pos < Fs or F_neg < Fs) and x[i] != x[i+1]:
                optimal_theta = 0.5 * (x[i] + x[i+1]); optimal_j = j
                if F_pos < F_neg:
                    Fs = F_pos; optimal_b = 1              
                else:
                    Fs = F_neg; optimal_b = -1       
    return (optimal_j, optimal_b, optimal_theta)

并且我正在寻找这样的输出:

holygrail : {
    rating : pg,
    cinema : regal,
    times : 1, 3, 5
    }

holygrail : {
    rating : pg,
    cinema : amc,
    times : 2, 4, 6
    }

holygrail : {
    rating: pg,
    cinema1 : {cinema : regal, times : 1, 3, 5},
    cinema2 : {cinema : amc, times : 2, 4, 6}
    }

在我看来,我可以制作电影类的实例,但不确定如何将其整合到字典中。

0 个答案:

没有答案