将字符串排序成字典,其中初始字符为键,值为所有以该字符开头的行的列表

2024-05-16 18:10:35 发布

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

情况:AHLTA,一种电子病历,将GUI模板导出为文本。我正在建立一个模板编辑器,需要导入文本文件。每行表示一个GUI元素,并以一个数字开始,该数字标识GUI中的父选项卡。行的顺序不重要。我用的是python3。你知道吗

示例(文件):

1,550,57,730,77,0,32770," |||||||0|0||0|0|||0|||0|0|1|0|0|0|||","F=TimesNewRoman|C=8421504|T=T","Last updated: 2017-05-18"
0,743,4,823,48,0,16384," |||||||0|0||0|0|||0|||0|0|0|0|0|0|||","F=Arial|O=5|B=T","TSWF Navigator:<formLinkInfo><version>1.1</version><templateName>TSWF-Navigator</templateName><templateId>2238487</templateId><templateOwnerName>Department of Defense</templateOwnerName><templateOwnerNcid>33962</templateOwnerNcid></formLinkInfo>"
0,828,4,907,24,0,16384," |||||||0|0||0|0|||0|||0|0|0|0|0|0|||","O=5","CORE:<formLinkInfo><version>1.1</version><templateName>TSWF-CORE</templateName><templateId>1995726</templateId><templateOwnerName>Department of Defense</templateOwnerName><templateOwnerNcid>33962</templateOwnerNcid></formLinkInfo>"
2,25,791,370,811,297285,8961," | || ||||19|80|YCN|0|0|Y|N|0|||0|0|5|0|0|0|||","F=Arial|T=T","Responds to affection~ (by 4 months)"
2,25,871,370,891,297287,8961," | || ||||19|80|YCN|0|0|Y|N|0|||0|0|5|0|0|0|||","F=Arial|T=T","Indicates pleasure and displeasure~ (by 4 months)"

我的目标:我想要一个列表字典,其中键对应于GUI选项卡编号,列表包含以该编号开始的所有行。你知道吗

示例:

0: 
0,743,4,823,48,0,16384," |||||||0|0||0|0|||0|||0|0|0|0|0|0|||","F=Arial|O=5|B=T","TSWF Navigator:<formLinkInfo><version>1.1</version><templateName>TSWF-Navigator</templateName><templateId>2238487</templateId><templateOwnerName>Department of Defense</templateOwnerName><templateOwnerNcid>33962</templateOwnerNcid></formLinkInfo>"
0,828,4,907,24,0,16384," |||||||0|0||0|0|||0|||0|0|0|0|0|0|||","O=5","CORE:<formLinkInfo><version>1.1</version><templateName>TSWF-CORE</templateName><templateId>1995726</templateId><templateOwnerName>Department of Defense</templateOwnerName><templateOwnerNcid>33962</templateOwnerNcid></formLinkInfo>"

1:
1,550,57,730,77,0,32770," |||||||0|0||0|0|||0|||0|0|1|0|0|0|||","F=TimesNewRoman|C=8421504|T=T","Last updated: 2017-05-18"

2:
2,25,791,370,811,297285,8961," | || ||||19|80|YCN|0|0|Y|N|0|||0|0|5|0|0|0|||","F=Arial|T=T","Responds to affection~ (by 4 months)"
2,25,871,370,891,297287,8961," | || ||||19|80|YCN|0|0|Y|N|0|||0|0|5|0|0|0|||","F=Arial|T=T","Indicates pleasure and displeasure~ (by 4 months)"

问题:我无法提前创建列表,因为在读取文件之前我不知道有多少选项卡。我尝试在每个选项卡的文件中循环,在临时列表中收集该选项卡的项,然后在转到下一个选项卡之前将列表添加到字典中。为简单起见,简化了示例数据:

theFile = ['1,550,57,730,77', '0,743,4,823,48', '0,828,4,907,24', '2,25,791,370,811', '2,25,871,370,891']
tabCount = 3  # for this example; normally pulled from file header

sortedLines = dict()
for i in range(tabCount):
    tempList = []
    for line in theFile:
        tempList.append(line)
    sortedLines.update({tabCount: tempList})
    tempList.clear()

print('Dict: ', sortedLines)
for k, v in sortedLines.items():
    print('Pair: ' + str(k) + ': ' + '[%s]' % ', '.join(map(str, v)))

这似乎是适当的循环,但我最终得到一个空对:

{3: []}
3: []

摘要:只有在运行时才知道列表的数量,如何创建列表字典?你知道吗


Tags: ofnavigator列表versiongui选项卡departmentdefense
1条回答
网友
1楼 · 发布于 2024-05-16 18:10:35
def main():
    # I'm assuming you can get this far...
    lines = [
        '1,some stuff 1',
        '2,some stuff 2,more stuff',
        '2,some stuff 4,candy,bacon',
        '3,some stuff 3,this,is,horrible...'
    ]

    # Something to hold your parsed data
    data = {}

    # Iterate over each line of your file
    for line in lines:

        # Split the data apart on comma per your example data
        parts = line.split(',')

        # denote the key is the first part of the split data
        key = parts[0]
        if key not in data:
            # Since there could be multiple values per key we need to keep a
            # list of mapped values
            data[key] = []

        # put the "other data" into the list
        index_of_sep = line.find(',')
        data[key].append(line[index_of_sep+1:])

    # You probably want to return here. I'm printing so you can see the result
    print(data)


if __name__ == '__main__':
    main()

结果

C:\Python35\python.exe C:/Users/Frito/GitSource/sandbox/sample.py
{'3': ['some stuff 3,this,is,horrible...'], '1': ['some stuff 1'], '2': ['some stuff 2,more stuff', 'some stuff 4,candy,bacon']}

Process finished with exit code 0

相关问题 更多 >