将嵌套列表中的日期字符串拆分为按月份分组的列表-Python
这是一个家庭作业。我不允许使用库等。目的是学习和理解Python中的嵌套列表、循环和条件过滤 我有一个嵌套的数据列表,下面是一个简短的摘录。它有200行长将嵌套列表中的日期字符串拆分为按月份分组的列表-Python,python,date,filter,nested,Python,Date,Filter,Nested,这是一个家庭作业。我不允许使用库等。目的是学习和理解Python中的嵌套列表、循环和条件过滤 我有一个嵌套的数据列表,下面是一个简短的摘录。它有200行长 patients = [ ['Milos', 'Jones', '15/01/20', 'male', 'smoker', '210'], ['Delia', 'Chan', '15/03/20', 'female', 'non-smoker', '170'], ['Denise', 'Ro
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('/')
这给了我一个错误“索引器:列表索引超出范围”
如果我使用代码:
for solo_patient in patients:
patients = [solo_patient[2].split('/')
它将日期拆分为“MM”、“DD”、“YY”,但我丢失了其他数据
当我将其拆分时,我需要将日期字符串转换为整数,然后使用范围(1,13)的循环遍历并按月份分组。然后我需要对男性/女性的数据进行基本统计。我想一旦我正确地过滤了列表,我就知道该怎么做了。如果您能提供任何建议、解释或建设性反馈,我将不胜感激。这里有一个可能的解决方案,可以按月对患者进行分组:
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)
我在您的示例上运行此代码,然后每个月患者是:
{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\u by\u month
是一本字典,它将每个月映射到一个患者列表。让我告诉你你在这里做错了什么-
for solo_patient in patients:
patients[2] = solo_patient[2].split('/')
下面是每个for循环调用中的值,即0,1,2。您正在尝试更新最后一行->患者[2]
患者[2]=“丹尼斯”、“罗斯”、“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)
当我试图运行你的代码时,我得到一个错误“IndexError:list index out-range”@EmBass我想你的数据中有一些错误。例如,您是否确定所有患者都有5个字段,并且所有患者的日期都是DD、MM、YY?@EmBass请提供一些我的代码无法正常运行的数据,以便我可以帮助您。对不起。好像是我。它现在正在工作。@EmBass如果您认为我的回答有用,请将其标记为已接受:)非常感谢您的解释。现在我知道我错在哪里了。在上面的代码中,如何访问元组中的月份字符串?我已经为您更新了答案