【问题标题】:Splitting a date string within a Nested List to group lists by the month - Python将嵌套列表中的日期字符串拆分为按月对列表进行分组 - Python
【发布时间】:2021-07-08 08:59:04
【问题描述】:

这是家庭作业。我不允许使用库等。目的是学习和理解 Python 中的嵌套列表、循环和条件过滤。

我有一个嵌套的数据列表,下面是一个简短的摘录。它有 200 行长。

patients = [ ['Milos', 'Jones', '15/01/20', 'male', 'smoker', '210'],
             ['Delia', 'Chan', '15/03/20', 'female', 'non-smoker', '170'],
             ['Denise', 'Ross', '13/02/20', 'female', 'non-smoker', '150'] ]

我需要能够按性别和月份过滤列表。 将每个列表中的 [5] 元素转换为准备好的整数。

为了按性别过滤,我写了这段代码:

female_patients = []
for solo_patient in patients:
    if solo_patient[3] == 'female':
        female_patients.append(solo_patient)

我已使用以下方法将元素转换为整数:

for solo_patient in patients:
    solo_patient[5] = int(solo_patient[5])
      

工作和输出我需要的东西。

但是,我正在尝试将数据拆分为字符串并转换为整数,以便按月过滤。我使用了与上面类似的逻辑,但我无法使其正常工作。

for solo_patient in patients:
patients[2] = [solo_patient[2].split('/')

这给了我一个错误“IndexError: list index out of range”

如果我使用代码:

for solo_patient in patients:
patients = [solo_patient[2].split('/')

它将日期拆分为“MM”、“DD”、“YY”,但我丢失了其他数据。

当我将它拆分时,我需要将日期字符串转换为整数,然后使用 range(1,13) 的循环遍历并按月份分组。然后我需要对男性/女性的数据进行基本统计。我想一旦我正确过滤了列表,我就知道该怎么做。如有任何建议、解释或建设性反馈,我将不胜感激。

【问题讨论】:

    标签: python date filter nested


    【解决方案1】:

    让我告诉你你做错了什么 -

    for solo_patient in patients:
        patients[2] = solo_patient[2].split('/')
    

    在每个 for 循环调用中,即 0,1,2。您正在尝试更新最后一行 -> 患者 [2]。

    患者[2] = ['Denise', 'Ross', '13/02/20', '女性', '非吸烟者', '150']

    如果你在 for 循环中执行类似的操作,那么patients[2] 的值将会改变。

    让我们改为这样做 -

    for solo_patient in patients:
        print((solo_patient[2]).split('/'))
    

    上面的语句会给你另一个列表-

    ['15', '01', '20']
    ['15', '03', '20']
    ['13', '02', '20']
    

    现在,如果您想将此值分配回特定索引处的原始列表,即每行的 2。你可以做这样的事情

    for index,solo_patient in enumerate(patients):
        patients[index][2] = tuple((solo_patient[2]).split('/'))
        
    print(patients)
    

    这将打印 -

    [['Milos', 'Jones', ('15', '01', '20'), 'male', 'smoker', '210'],
     ['Delia', 'Chan', ('15', '03', '20'), 'female', 'non-smoker', '170'],
     ['Denise', 'Ross', ('13', '02', '20'), 'female', 'non-smoker', '150']]
    

    提取月份。

    month_list= [] #initialize empty list
    for solo_patient in patients:
        date = solo_patient[2].split('/') # split and create list
        month_list.append(int(date[1])) # select the value at index 1 and convert it to int and then append it to month list.
        
    print(month_list)
    

    【讨论】:

    • 非常感谢您的解释。现在我可以看到我哪里出错了。在上面的代码中,如何访问元组中的月份字符串?
    【解决方案2】:

    以下是按月对患者分组的可能解决方案:

    patients_by_month = {month: [] for month in range(1, 13)}
    for patient in patients:
        mm = int(patient[2].split('/')[1])
        patients_by_month[mm].append(patient)
    

    如果您在示例中运行此代码,那么 patients_by_month 是:

    {1: [['Milos', 'Jones', '15/01/20', 'male', 'smoker', '210']],
     2: [['Denise', 'Ross', '13/02/20', 'female', 'non-smoker', '150']],
     3: [['Delia', 'Chan', '15/03/20', 'female', 'non-smoker', '170']],
     4: [], 5: [], 6: [], 7: [], 8: [], 9: [], 10: [], 11: [], 12: []}
    

    具体来说,patients_by_month 是一个将每个月映射到患者列表的字典。

    【讨论】:

    • 当我尝试运行您的代码时出现错误“IndexError: list index out of range”
    • @EmBass 我认为您的数据中有一些错误。例如,您确定您的所有患者都有 5 个字段,并且您的所有患者都有 DD、MM、YY 的日期吗?
    • @EmBass 请提供一些我的代码无法正常运行的数据,以便我可以帮助您
    • 对不起。似乎是我。它现在正在工作。
    • @EmBass 如果您认为我的回答有用,请将其标记为已接受 :)
    猜你喜欢
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多