映射和迭代嵌套字典

2024-05-19 07:06:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我不太熟悉python,但对基础知识有很好的理解。我相信我需要字典,但我目前所做的是不工作,可能是非常无效的时间明智的。你知道吗

我正在尝试创建一个交叉矩阵,将用户之间的评论链接起来给定:评论人列表、他们的个人评论、与评论相关的元数据。你知道吗

注意:这是用Python2.7.10编写的-我不能使用Python3,因为它将在过时的系统上运行,yada yada。你知道吗

对于初始化,我有以下内容:

print '\nCompiling Review Maps... ';
LbidMap = {};
TbidMap = {};
for user in reviewer_idx :
    for review in data['Reviewer Reviews'][user] :
        reviewInfo = data['Review Information'][review];
        stars = float(reviewInfo['stars']);
        bid = reviewInfo['business_id'];

        # Initialize lists where necessary
        # !!!! I know this is probably not effective, but am unsure of
        #  a better method. Open to suggestions !!!!!
        if bid not in LbidMap:
            LbidMap[bid] = {};
            TbidMap[bid] = {};
        if stars not in LbidMap[bid] :
            LbidMap[bid][stars] = {};
        if user not in TbidMap[bid] :
            TbidMap[bid][user] = {};

        # Track information on ratings to each business
        LbidMap[bid][stars][user] = review;
        TbidMap[bid][user][review] = stars;

(其中“bid”是“Business ID”的缩写,pos\ u list是用户在运行时提供的输入)

然后,我继续尝试创建一个映射,将给予“积极”评价的用户映射到同时给予business L X评级的business T(例如,5个人给business L评级为4/5星,其中有多少人也给予business T“积极”评价?)你知道吗

对于映射,我有以下内容:

# Determine and map all users who rated business L as rL
#  and gave business T a positive rating
print '\nCross matching ratings across businesses';
cross_TrL = [];
for Tbid in TbidMap :
    for Lbid in LbidMap :
        # Ensure T and L aren't the same business
        if Tbid != Lbid :
            for stars in LbidMap[Lbid] :
                starSum = len(LbidMap[Lbid][stars]);
                posTbid = 0;
                for user in LbidMap[Lbid][stars] :
                    if user in TbidMap[Tbid] :
                        rid = LbidMap[Lbid][stars][user];
                        print 'Tbid:%s  Lbid:%s  user:%s  rid:%s'%(Tbid, Lbid, user, rid);
                        reviewRate = TbidMap[Tbid][user][rid];
                        # If true, then we have pos review for T from L
                        if reviewRate in pos_list :
                            posTbid += 1;
                numerator = posTbid + 1;
                denominator = starSum + 1;
                probability = float(numerator) / denominator;

我当前收到以下错误(还提供打印当前变量):

Tbid:OlpyplEJ_c_hFxyand_Wxw  Lbid:W0eocyGliMbg8NScqERaiA  user:Neal_1EVupQKZKv3NsC2DA  rid:TAIDnnpBMR16BwZsap9uwA
Traceback (most recent call last):
  File "run_edge_testAdvProb.py", line 90, in <module>
    reviewRate = TbidMap[Tbid][user][rid];
KeyError: u'TAIDnnpBMR16BwZsap9uwA'

因此,我知道KeyError在TbidMap中的某个特定时刻应该是rid(review ID),但是在我看来,这个键不知何故没有包含在初始化的第一个代码块中。你知道吗

我做错什么了?此外,建议如何改善时钟周期的第二个代码块是欢迎的。你知道吗


编辑:我意识到我试图使用Lbid中的rid来定位Tbid的rid,但是rid对于每个评论都是唯一的,所以您不会有Tbid.rid=Lbid.rid。你知道吗

更新了第二个代码块,如下所示:

cross_TrL = [];
for Tbid in TbidMap :
    for Lbid in LbidMap :
        # Ensure T and L aren't the same business
        if Tbid != Lbid :
            # Get numer of reviews at EACH STAR rate for L
            for stars in LbidMap[Lbid] :
                starSum = len(LbidMap[Lbid][stars]);
                posTbid = 0;
                # For each review check if user rated the Tbid
                for Lreview in LbidMap[Lbid][stars] :
                    user = LbidMap[Lbid][stars][Lreview];
                    if user in TbidMap[Tbid] :
                        # user rev'd Tbid, get their Trid 
                        #  and see if they gave Tbid a pos rev
                        for Trid in TbidMap[Tbid][user] :
                            # Currently this does not account for multiple reviews
                            #  given by the same person. Just want to get this 
                            #  working and then I'll minimize this
                            Tstar = TbidMap[Tbid][user][Trid];
                            print 'Tbid:%s  Lbid:%s  user:%s  Trid:%s'%(Tbid, Lbid, user, Trid);
                            if Tstar in pos_list :
                                posTbid += 1;
                numerator = posTbid + 1;
                denominator = starSum + 1;
                probability = float(numerator) / denominator;
                evaluation = {'Tbid':Tbid, 'Lbid':Lbid, 'star':stars, 'prob':probability}
                cross_TrL.append(evaluation);

仍然很慢,但我不再收到错误。你知道吗


Tags: andinforifbusinessreviewstarsuser

热门问题