将行与行之间的字添加到数组

2024-07-03 06:11:04 发布

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

这是我的文件的内容:

david    C001 C002 C004 C005 C006 C007

*    C008 C009 C010 C011 C016 C017 C018

*    C019 C020 C021 C022 C023 C024 C025

anna C500 C521 C523 C547 C555 C556

*    C557 C559 C562 C563 C566 C567 C568

*    C569 C571 C572 C573 C574 C575 C576

*    C578

charlie    C701 C702 C704 C706 C707 C708

*    C709 C712 C715 C716 C717 C718

我希望我的输出是:

david=[C001,C002,C004,C005,C006,C007,C008,C009,C010,C011,C016,C017,C018,C019,C020,C021,C022,C023,C024,C025]

anna=[C500,C521,C523,C547,C555,C556,C557,C559,C562,C563,C566,C567,C568,C569,C571,C572,C573,C574,C575,C576,C578]

charlie=[C701,C702,C704,C706,C707,C708,C709,C712,C715,C716,C717,C718]

我能够创造:

david=[C001,C002,C004,C005,C006,C007]
anna=[C500,C521,C523,C547,C555,C556]
charlie=[C701,C702,C704,C706,C707,C708]

计算一行中的字数,并使用第[0]行作为数组名称,然后将剩余的字数添加到数组中。 但是,我不知道如何将以“*”开头的下一行中的单词的连续字符带到数组中

有人能帮忙吗


Tags: davidcharlieannac001c002c500c521c547
3条回答

我自己想出了一个办法。感谢那些给出自己解决方案的人。它给了我新的视角

下面是我的代码:

persons_library={}
persons=['david','anna','charlie']
for i,person in enumerate(persons,start=0):
    persons_library[person]=[]

with open('data.txt','r') as f:
    for line in f:
        line=line.replace('*',"")
        line=line.split()
        for i,val in enumerate(line,start=0):
            if val in persons_library:
                key=val
            else:
                persons_library[key].append(val)
    print(persons_library)

您可以使用正则表达式将列表读入词典:

import re
with open('file_name') as file:
    contents = file.read()
res_list = re.findall(r"[a-z]+\s+[^a-z]+",contents)
res_dict = {}
for p in res_list:
    elt = p.split()
    res_dict[elt[0]] = [e for e in elt[1:] if e != '*']

print(res_dict)

注意:此解决方案依赖于对defaultdict进行排序,这是在Python 3.6上引入的

有些幼稚的做法:

from collections import defaultdict

# Create a dictionary of people
people = defaultdict(list)

# Open up your file in read-only mode
with open('your_file.txt', 'r') as f:
    # Iterate over all lines, stripping them and splitting them into words
    for line in filter(bool, map(str.split, map(str.strip, f))):
        # Retrieve the name of the person
        # either from the current line or use the name of the last person processed
        name, words = list(people)[-1] if line[0] == '*' else line[0], line[1:]
        # Add all remaining words to that person's record
        people[name].extend(words)


print(people['anna'])
# ['C500', 'C521', 'C523', 'C547', 'C555', 'C556', 'C557', 'C559', 'C562', 'C563', 'C566', 'C567', 'C568', 'C569', 'C571', 'C572', 'C573', 'C574', 'C575', 'C576', 'C578']

它还有另外一个好处,即为未知名称返回空的list

print(people['matt'])
# []

相关问题 更多 >