簡體   English   中英

Python:將字符串列表解析為字典

[英]Python: Parse a list of strings into a dictionnary

這有點復雜。 我有一個看起來像這樣的列表:

['19841018 ID1\n', ' Plunging oil... \n', 'cut in the price \n', '\n', '19841018 ID2\n', ' The U.S. dollar...  \n', 'the foreign-exchange markets \n', 'late New York trading \n', '\n']

在我的清單中, '\\n'是分隔故事的內容。 我想做的是從上面的列表創建一個字典,就像這樣:

dict = {ID1: [19841018, 'Plunging oil... cut in the price'], ID2: [19841018, 'The U.S. dollar... the foreign-exchange markets']}

您會看到我字典的KEYID ,項目是year和故事的組合。 那可行嗎?

  • 我的ID的格式為J00100394J00384932 因此,它們都以J00開頭。

棘手的部分是按任何值分割列表,因此我從這里開始進行討論
然后我解析了列表部分以構建res dict

>>> import itertools
>>> def isplit(iterable,splitters):
...     return [list(g) for k,g in itertools.groupby(iterable,lambda x:x in splitters) if not k]
... 
>>> l = ['19841018 ID1\n', ' Plunging oil... \n', 'cut in the price \n', '\n', '19841018 ID2\n', ' The U.S. dollar...  \n', 'the foreign-exchange markets \n', 'late New York trading \n', '\n']
>>> res = {}
>>> for sublist in isplit(l,('\n',)):
...     id_parts = sublist[0].split()
...     story    = ' '.join (sentence.strip() for sentence in sublist[1:])
...     res[id_parts[1].strip()] = [id_parts[0].strip(), story]
... 
>>> res
{'ID2': ['19841018', 'The U.S. dollar... the foreign-exchange markets late New York trading'], 'ID1': ['19841018', 'Plunging oil... cut in the price']}

我編寫一個使用生成器的答案。 這個想法是,每次啟動id令牌時,生成器都會返回計算出的最后一個密鑰。 您可以通過更改check_fun()以及如何混合描述的一部分來進行check_fun()

def trailing_carriage(s):
    if s.endswith('\n'):
        return s[:-1]
    return s

def check_fun(s):
    """
    :param s:Take a string s
    :return: None if s dosn't match the ID rules. Otherwise return the
    name,value of the token
    """
    if ' ' in s:
        id_candidate,name = s.split(" ",1)
        try:
            return trailing_carriage(name),int(id_candidate)
        except ValueError:
            pass


def parser_list(list, check_id_prefix=check_fun):
    name = None #key dict
    id_candidate = None
    desc = "" #description string
    for token in list:
        check = check_id_prefix(token)
        if check is not None:
            if name is not None:
                """Return the previous coputed entry"""
                yield name,id_val,desc
            name,id_val = check
        else:
            """Append the description"""
            desc += trailing_carriage(token)
    if name is not None:
        """Flush the last entry"""
        yield  name,id_val,desc


>>> list = ['19841018 ID1\n', ' Plunging oil... \n', 'cut in the price \n', '\n', '19841018 ID2\n', ' The U.S. dollar...  \n', 'the foreign-exchange markets \n', 'late New York trading \n', '\n']
>>> print {k:[i,d] for k,i,d in parser_list(list)}
{'ID2': [19841018, ' Plunging oil... cut in the price  The U.S. dollar...  the foreign-exchange markets late New York trading '], 'ID1': [19841018, ' Plunging oil... cut in the price ']}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM