使用列表/字典的Python3.x长到宽的数据集?

2024-09-29 23:30:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我在这些论坛上潜伏了很多年,总是能够找到我的答案,但我被困在一个我似乎无法用Python解决的问题上。我有一个类似下面的数据集(显然是虚构的):

输入:

wound_list =[
['1065', '', 'Drew, DonnaLee', '8/22/2018 8:23:00 AM','Wound 1 Length', '2.5']
['1065', '', 'Drew, DonnaLee', '8/22/2018 8:23:00 AM', 'Wound 1 depth', '.8']
['1065', '', 'Drew, DonnaLee', '8/22/2018 8:23:00 AM', 'Wound 1- wound care performed', 'yes']
['1065', '', 'Drew, DonnaLee', '8/22/2018 8:23:00 AM', 'Wound 1 color', 'red']

['1065', '', 'Drew, DonnaLee', '8/8/2018 11:34:00 AM', 'Wound 1 Length', '1.8']
['1065', '', 'Drew, DonnaLee', '8/8/2018 11:34:00 AM', 'Wound 1- wound care performed', 'yes']
['1065', '', 'Drew, DonnaLee', '8/8/2018 11:34:00 AM', 'Wound 1 color', 'red']

['1065', '', 'Drew, DonnaLee', '8/8/2018 11:34:00 AM', 'Wound 2 Length', '3.5']
['1065', '', 'Drew, DonnaLee', '8/8/2018 11:34:00 AM', 'Wound 2- wound care performed', 'no']
['1065', '', 'Drew, DonnaLee', '8/8/2018 11:34:00 AM', 'Wound 2 color', 'yellow']
['1065', '', 'Drew, DonnaLee', '8/8/2018 11:34:00 AM', 'Wound 2 depth', '.7']]

我想把它放到下面的东西中,这样我就可以把它上传到SQLite数据库中:
输出:

Last_Name, First_Name, Date_Time, Wound_Number, Length, depth, width, color, wound_care_performed
'Drew', 'DonnaLee', 8/22/2018 8:23:00 AM, 1, 2.5, .8, '', 'red', 'yes'
'Drew', 'DonnaLee', 8/8/2018 11:34:00 AM, 2, 1.8, '', '', 'red', 'yes'
'Drew', 'DonnaLee', 8/8/2018 11:34:00 AM, 1, 3.5, .7, '', 'yellow', 'no'

我用这段代码来解析变量,并试图保持它们的顺序,但它遇到了问题,因为伤口编号不同,日期保持不变

ppt = ''
date = ''
woundnumber = ''
length = ''
width = ''
depth = ''
color = ''
woundcare = ''
wound_list_improved=[]
woundnumbercheck = ''
for i in range(len(wound_list)):
    for j, d in enumerate(wound_list[i][4]):
        if d.isdigit():
            woundnumbercheck = wound_list[i][4][j]
    if date != wound_list[i][3]:
        wound_list_improved.append([ppt, date, woundnumber, length, width, depth, color, woundcare])
        ppt = ''
        date = ''
        woundnumber = ''
        length = ''
        width = ''
        depth = ''
        color = ''
        woundcare = ''
        for j, d in enumerate(wound_list[i][4]):
            if d.isdigit():
                woundnumber = wound_list[i][4][j]
        if "Length" in wound_list[i][4]:
            length = wound_list[i][5]
    if woundnumbercheck != woundnumber:
        wound_list_improved.append([ppt, date, woundnumber, length, width, depth, color, woundcare])
        ppt = ''
        date = ''
        woundnumber = ''
        length = ''
        width = ''
        depth = ''
        color = ''
        woundcare = ''
        for j, d in enumerate(wound_list[i][4]):
            if d.isdigit():
                woundnumber = wound_list[i][4][j]
        if "Length" in wound_list[i][4]:
            length = wound_list[i][5]
    else:
        if "Length" in wound_list[i][4]:
            length = wound_list[i][5]

我缩短了代码,这样就不会占用太多的空间。但我觉得必须有更好的方法来处理这类问题。也许是带字典的

提前谢谢你的帮助


Tags: indateifamwidthlengthlistcolor

热门问题