标识要在for循环中附加的列表中特定值的索引

背景上下文-我正在使用mailchimp3 python库,我试图检索一个人可能会对他们收到的电子邮件进行的一系列操作。通常,它会被弹开,打开或单击。一个人可以单击多次,将其打开多次,否则它只会反弹而已。

我想做的是确定 1.如果有点击与未打开,那么在他们执行的动作列表中,“点击”的索引值是多少? 2.用管道传输该点击的索引值以获取该点击的时间戳 3.如果没有点击,请执行他们最后一次执行的操作。

现在,我进行此设置只是为了执行某人所做的最后一项操作。我正在尝试使用循环(在下面的代码中间)循环显示其操作列表,确定他们是否单击了,优先考虑将其作为他们的操作的附加值,否则我将采取他们的最后一个操作,

def patient_performance(campaign_id):
    campaign_data = client.reports.email_activity.all(campaign_id=campaign_id,get_all=True)

    patient_campaign = []
    patient_email    = []
    patient_activity = []
    activity_time    = []
    action_list      = []

    for value in campaign_data['emails']:
        #For any unopened or untouched emails. The list will return as empty. Removes any empty email results.
        if len(value['activity']) == 0:
            pass

        else:
            try:
                patient_campaign.append(campaign_id)
                patient_email.append(value['email_address'])

                #Previous code that was working
                #patient_activity.append(value['activity'][-1].get('action'))
                #activity_time.append(value['activity'][-1].get('timestamp'))

                #HERE IS THE NEW CODE I AM TRYING TO INCORPORATE!!!!

                #Summarizes the list of various actions a patient has taken with emails.
                for actions in value['activity']:
                    action_list.append(actions.get('action'))
                    timestamp_list.append(actions.get('timestamp'))


                #Attempting to identify the if a patient clicked.
                for counter,value in enumerate(action_list):
                    if  value == 'click':
                        patient_activity.append(value)
                        activity_time.append(value['activity'][counter].get('timestamp'))
                    else:
                        patient_activity.append(value['activity'][-1].get('action'))
                        activity_time.append(value['activity'][-1].get('timestamp'))


            #Catches any errors.
            except AttributeError:
                pass

    #Appends all results captured for specific campaign. This is fed into a temporary DF which is then appended into the master DF. 
    temp_patient_performance = pd.DataFrame(list(zip(patient_campaign,patient_email,patient_activity,activity_time)),columns = ['campaign','email_address','activity','timestamp'])
    temp_patient_performance = temp_patient_performance.drop(temp_patient_performance[(temp_patient_performance['activity'] == 'bounce')].index)

    print("Retrieved {} patient email results for campaign {}".format(len(temp_patient_performance['email_address']),campaign_id))
    return  temp_patient_performance

这是结果的示例读取结果。每个列表代表一个人,该列表中的多个词典代表进行的多个操作。

[{'action': 'open','timestamp': '2019-11-02T18:24:19+00:00','ip': '1234'}]
[{'action': 'open','timestamp': '2019-11-02T13:43:45+00:00','timestamp': '2019-11-02T00:25:33+00:00','timestamp': '2019-11-01T20:19:00+00:00','ip': '1234'},{'action': 'open','timestamp': '2019-11-02T20:36:21+00:00','timestamp': '2019-11-04T03:07:53+00:00','timestamp': '2019-11-01T20:45:39+00:00','timestamp': '2019-11-02T23:39:32+00:00','timestamp': '2019-11-01T22:16:54+00:00','timestamp': '2019-11-01T20:14:48+00:00','timestamp': '2019-11-01T22:21:33+00:00','timestamp': '2019-11-02T00:40:15+00:00','timestamp': '2019-11-02T00:54:15+00:00','timestamp': '2019-11-04T01:51:53+00:00','timestamp': '2019-11-01T23:54:49+00:00','ip': '1234'}]
[{'action': 'bounce','type': 'hard','timestamp': '2019-11-01T00:00:00+00:00'}]
[{'action': 'bounce','timestamp': '2019-11-01T00:00:00+00:00'}]
[{'action': 'open','timestamp': '2019-11-01T20:28:11+00:00','timestamp': '2019-11-02T18:27:28+00:00','timestamp': '2019-11-03T22:35:34+00:00','timestamp': '2019-11-06T20:28:34+00:00','timestamp': '2019-11-10T02:32:38+00:00','timestamp': '2019-11-11T05:18:34+00:00','ip': '1234'}]
yl987987 回答:标识要在for循环中附加的列表中特定值的索引

如果我误解了您的要求,请原谅我,但我认为这应该有效吗?

# HERE IS THE NEW CODE I AM TRYING TO INCORPORATE!!!!

# Summarizes the list of various actions a patient has taken with emails.
for action in value['activity']:
    if action['action'] == 'click':
        patient_activity.append(action['action'])
        activity_time.append(action['timestamp'])

if not len(patient_activity):
    patient_activity.append(value['activity'][-1]['action'])
    activity_time.append(value['activity'][-1]['timestamp'])
,

我已经通过其他一些专门在词典列表中搜索值的帖子解决了这个问题。

这尤其是解决了我的问题的新代码:

try:
    patient_activity.append(next(item for item in value['activity'] if item['action'] == 'click').get('action'))
    activity_time.append(next(item for item in value['activity'] if item['action'] == 'click').get('timestamp'))

#StopIteration is returned by the code above where 'click' is not found because a patient has not clicked.

except StopIteration:
        patient_activity.append(value['activity'][-1].get('action'))
        activity_time.append(value['activity'][-1].get('timestamp'))

完整的代码块在下面

def patient_performance(campaign_id):
    campaign_data = client.reports.email_activity.all(campaign_id=campaign_id,get_all=True)

    patient_campaign = []
    patient_email    = []
    patient_activity = []
    activity_time    = []
    action_list      = []
    timestamp_list   = []
    total_opens      = []
    total_clicks     = []

    for value in campaign_data['emails']:
        #For any unopened or untouched emails. The list will return as empty. Removes any empty email results.
        if len(value['activity']) == 0:
            pass

        else:
            try:
                patient_campaign.append(campaign_id)
                patient_email.append(value['email_address'])

                #Looks to identify which patients have 'clicked',prioritising their click action vs. all else (open or bounce). Otherwise takes their last action.
                try:
                    patient_activity.append(next(item for item in value['activity'] if item['action'] == 'click').get('action'))
                    activity_time.append(next(item for item in value['activity'] if item['action'] == 'click').get('timestamp'))

                #StopIteration is returned by the code above where 'click' is not found because a patient has not clicked.
                except StopIteration:
                    patient_activity.append(value['activity'][-1].get('action'))
                    activity_time.append(value['activity'][-1].get('timestamp'))

            #Catches any errors returned by missing data within the 'activity' list retrieved from Mailchimp.
            except AttributeError:
                pass

    #Appends all results captured for specific campaign. This is fed into a temporary DF which is then appended into the master DF once it's returned to the function call. 
    temp_patient_performance = pd.DataFrame(list(zip(patient_campaign,patient_email,patient_activity,activity_time)),columns = ['campaign','email_address','activity','timestamp'])
    temp_patient_performance = temp_patient_performance.drop(temp_patient_performance[(temp_patient_performance['activity'] == 'bounce')].index)

    print("Retrieved {} patient email results for campaign {}".format(len(temp_patient_performance['email_address']),campaign_id))
    return  temp_patient_performance
本文链接:https://www.f2er.com/3108713.html

大家都在问