首页 > 解决方案 > 即使列表 x 比 y 长,如何使 str x 列表完全匹配列表 y 中的列表?我想要与 None 配对的额外 x 值

问题描述

我正在尝试设置一种将电子邮件列表和名称列表匹配为元组的方法。但是,我发现当它到达姓氏时,那些没有名字配对的电子邮件不包含在我的元组中,我怎样才能让这些额外的电子邮件简单地配对一个空字符串(“”)?

本质上,我有格式的 excel 行,我将其设置为 pandas 数据框:

cust_ID 买家姓名 买家电子邮件
1234 名称 1;名称 2; 名称 3 电子邮件1;电子邮件2;电子邮件3;电子邮件 4
...... ...... ……

我试过这个:

# Set regular expression to catch emails
regex = r"[a-zA-Z0-9_.+-]*@[a-zA-Z0-9-]+.[a-zA-Z\.]*"

# Initialise empty list to add query ready emails
emails_query_format = []

# Iterate over retailer_id / emails template rows and append formatted emails to list
for i, row in df.iterrows():
    # Put all emails in the row into a list
    emails = re.findall(regex, df['additional_emails'][i])
    emails = [email.strip() for email in emails]
    
    # Put all additional buyers into a list
    buyer_names = row['additional_buyers']
    buyers = re.split(r";", buyer_names)
    buyers = [buyer.strip() for buyer in buyers]
    
    buyer_email_tuple = [*zip(emails, buyers)]

最终,在遍历这个元组并将它们放入查询格式之后,如下所示:

  # For each pair I want to create a row with the formated 
  for email, buyer in buyer_email_tuple:

      # Here I am just putting it into a specific format to copy paste to query template
      query_format = "(" + str(row['retailer_id']) + "," + "'" + buyer + "'" + "," + "'" + \ 
      email + "'" + ")" + ","
      
      emails_query_format.append(query_format)

# New DataFrame to input query ready emails
query_df = pd.DataFrame(emails_query_format, columns=['query_ready'])

这样,元组不包括额外的“email4”。我想到了 collections 模块中的容器,但我并没有真正看到为此使用 defaultdict 的明确方法。

如何使元组包含 email4 与简单的“”值作为名称配对?

提前致谢。

标签: python-3.xcollectionstuplesiterationdefaultdict

解决方案


Solved the issue:

for idx in range(len(emails)):
    if idx <= len(buyers) -1:
        buyer_emails_tuple_list.append((buyers[idx], emails[idx]))
    elif idx > len(buyers) -1:
        buyer_emails_tuple_list.append(("", emails[idx]))

Now I can make sure that for those emails that have no corresponding buyer names I get them paired with an empty string as:

("", email4)

推荐阅读