在使用“和"时如何使用多个if else条件.使用python的嵌套for循环中的逻辑 [英] How to use multiple if else conditions when using "and" logic in a nested for-loop using python

查看:101
本文介绍了在使用“和"时如何使用多个if else条件.使用python的嵌套for循环中的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  • 我有一个清单清单清单
  • 最外面的列表是成员的整个集合,其中的每个列表都是单个成员,其中的原始文本文件的每一行都被分割成各个元素.
  • 每个成员的记录都有一个名称行,由"NM1"标签指示
  • 但并非每个成员都有一个结束日期"字段,由'DTP''349'标签
  • 指示
  • 同样,并非每个成员都有一个"Prior ID".字段,由'REF''0F'标签
  • 指示
  • 我想遍历每条记录,如果我需要的字段在那里,请提取我需要的列表元素,然后追加到新列表中.如果不存在,请附加一个None值作为占位符.我需要每个列表具有相同数量的值,因此当我将它们作为pandas系列放入DataFrame时,每个系列都具有相同的长度.
  • I have a list of lists of lists
  • The outer most list is the entire collection of members, each list within that is the individual members, and within that is each line of the raw text file I got split up into its individual elements.
  • Each member's record has a Name line, indicated by the "NM1" label
  • But not every member has an "End Date" field, indicated by the 'DTP' and '349' labels
  • Likewise not every member has an "Prior ID" field, indicated by the 'REF' and '0F' labels
  • I want to go through each record and if the field I need is there, extract the element of the list I need and append to a new list. If it isnt there, append a None value as a placeholder. I need each list to have the same number of values so when I put them as a pandas Series into a DataFrame, each series has the same length.

作为一个简单的例子,我将数据解析为我想要的格式.

I got the data parsed into the format I want like this, as a simple example.

Groups = [[['NM1', 'IL', '1', 'SMITH', 'JOHN', 'PAUL', 'MR', 'JR', ''],
  ['REF', '1L', '690553677', ''],
  ['DTP', '348', 'D8', '20200601', ''],
  ['DTP', '349', 'D8', '20200630', '']],
 [['NM1', 'IL', '1', 'IMA', 'MEAN', 'TURD', 'MR', 'SR', ''],
  ['REF', '1L', '690545645', ''],
  ['REF', '0F', '001938383',''],
  ['DTP', '348', 'D8', '20200601', '']]]

我尝试使用for循环来遍历每条记录,并且如果这些特殊的标签"的组合出现了,组中存在的项目,将其添加到仅包含我想要的最后一个元素(日期或ID号)的新列表中. 当我尝试对每个元素使用多个if-else条件时,我只会得到None值.

I try using a for loop to go through each record and if the combination of those special "labels" exist in the group, append it to a new list with just the last element I want (the date, or the ID #). when I try to use multiple if- else conditions for each element I only get None values.

current_id = []
prior_id = []
start_date = []
end_date = []


for group in Groups:
    if ((line[0] == 'REF') and (line[1] == 'IL')) in (line for line in group):
        current_id.append(line[2])
    else:
        current_id.append(None)
    if ((line[0] == 'REF') and (line[1] == '0F')) in (line for line in group):
        prior_id.append(line[2])
    else:
        prior_id.append(None)
    if ((line[0] == 'DTP') and (line[1] == '348')) in (line for line in group):
        start_date.append(line[2])
    else:
        start_date.append(None)
    if ((line[0] == 'DTP') and (line[1] == '349')) in (line for line in group):
        end_date.append(line[2])
    else:
        end_date.append(None)

print(current_id)
print(prior_id)
print(start_date)
print(end_date)

[None, None]
[None, None]
[None, None]
[None, None]

应该是:

['690553677','690545645']
[None, '001938383']
['20200601', '20200601']
['20200630', None]

我知道我的逻辑必定是错误的,但是如何做到这一点的最佳方法呢?

I know my logic must be off but how is the best way to do this?

推荐答案

您可以使用forelse语句,我定义了一个名为ids的函数,该函数将检索ID:

You can use for and else statements, I defined a function called ids that will retrieve the ids:

Groups = [[['NM1', 'IL', '1', 'SMITH', 'JOHN', 'PAUL', 'MR', 'JR', ''],
           ['REF', '1L', '690553677', ''],
           ['DTP', '348', 'D8', '20200601', ''],
           ['DTP', '349', 'D8', '20200630', '']],
          [['NM1', 'IL', '1', 'IMA', 'MEAN', 'TURD', 'MR', 'SR', ''],
           ['REF', '1L', '690545645', ''],
           ['REF', '0F', '001938383',''],
           ['DTP', '348', 'D8', '20200601', '']]]

def ids(a, b):
    l = []
    for group in Groups:
        for lst in group:
            if lst[:2] == [a, b]:
                if lst[2] == 'D8':
                    l.append(lst[3])
                else:
                    l.append(lst[2])
                break
        else:
            l.append(None)
    return l
        
current_id = ids('REF', '1L')
prior_id = ids('REF', '0F')
start_date = ids('DTP', '348')
end_date = ids('DTP', '349')
        
print(current_id)
print(prior_id)
print(start_date)
print(end_date)

输出:

['690553677', '690545645']
[None, '001938383']
['20200601', '20200601']
['20200630', None]

请注意我使用的if语句:if lst[2] == 'D8':.我之所以使用它,是因为我看到并不是列表的所有ID号都在索引2处,有些不是在索引3处.

Note the if statements I used: if lst[2] == 'D8':. I used that because I saw that not all of the list's id numbers are at index 2, some are at index 3.

这篇关于在使用“和"时如何使用多个if else条件.使用python的嵌套for循环中的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆