简体   繁体   English

如何从python中的字典中获取后续键?

[英]How to get subsequent keys from a dictionary in python?

so I'm working on this referencing problem in which I need to get a key from a dictionary and its child keys(if any) for example, I have a list of dictionary keys as such:所以我正在解决这个引用问题,例如,我需要从字典及其子键(如果有)中获取一个键,我有一个字典键列表,如下所示:

dict_keys(['1.', '1.1', '1.2', '1.2.1', '1.2.2', '1.2.2(a)', '1.2.2(b)', '1.2.2(c)', '1.2.2(d)', '1.2.3', '1.2.4', '1.2.5', '1.2.6', '1.2.7', '1.2.8', '1.2.9', '1.2.10', '1.2.11', '2.', '2.1', '3.', '3.1', '3.1.1', '3.1.2', '3.2', '3.3', '3.4', '3.5', '3.5.1', '3.5.2', '3.5.2(a)', '3.5.2(b)', '3.5.2(c)', '3.6', '3.7', '3.8', '3.9', '3.9.1', '3.9.2', '3.9.3', '3.10', '3.11', '3.11.1', '3.11.2', '4.', '4.1', '4.1.1', '4.1.2', '4.1.3', '4.1.4', '4.1.5', '4.1.6', '4.1.7', '4.1.8', '4.1.9', '4.1.10', '4.2', '5.', '5.1', '5.1.1', '5.1.2', '5.1.3', '5.1.4', '5.1.4(a)', '5.1.4(b)', '5.1.4(c)', '5.1.4(d)', '5.1.4(e)', '5.1.4(f)', '5.1.4(g)', '5.1.4(h)', '5.1.4(i)', '5.1.4(j)', '5.1.4(k)', '5.2', '5.3', '5.4', '6.', '6.1', '6.2', '7.', '7.1', '7.2', '7.3', '7.3.1', '7.3.2', '7.3.3', '7.4', '7.5', '7.6', '7.6.1', '7.6.2', '7.6.3', '7.6.4', '8.', '8.1', '8.2', '8.2.1', '8.2.2', '8.2.3', '8.2.4', '8.2.5', '8.2.6', '8.2.7', '8.3', '8.3.1', '8.3.2', '8.3.3', '8.3.4', '8.3.5', '8.3.6', '8.4', '9.', '9.1', '9.1.1', '9.1.2', '9.2', '9.3', '9.4', '9.5', '10.', '10.1', '10.2', '10.3', '10.4', '11.', '11.1', '11.2', '11.2.1', '11.2.2', '11.2.3', '11.3', '11.4', '11.5', '11.6', '11.7', '11.8', '11.9', '11.10', '12.', '12.1', '12.2', '12.2.1', '12.2.2', '12.3', '12.4', '12.4.1', '12.4.2', '12.4.3', '12.5', '12.6', '12.7', '12.8', '12.9', '12.10', '12.11', '13.', '13.1', '13.2', '13.3', '13.3.1', '13.3.2', '13.3.3', '13.3.4', '13.4', '13.4.1', '13.4.2', '13.4.3', '14.', '14.1', '14.2', '14.3', '15.', '15.1', '15.2', '15.2.1', '15.2.2', '15.2.3', '15.2.4', '16.', '16.1', '16.2', '16.3', '16.4', '16.5', '16.6', '16.7', '16.7.1', '16.7.2', '17.', '17.1', '17.1.1', '17.1.2', '17.1.3', '17.1.4', '17.2', '17.2.1', '17.2.2', '17.2.3', '17.2.4', '17.3', '17.4', '17.4.1', '17.4.2', '17.5', '17.5.1', '17.5.2', '17.5.3', '18.', '18.1', '18.2', '18.2.1', '18.2.1(a)', '18.2.1(b)', '18.2.1(c)', '18.2.2', '18.2.3', '18.2.4', '18.2.5', '18.2.5(a)', '18.2.5(b)', '18.2.6', '18.2.7', '19.', '19.1', '19.2', '19.3', '19.4', '20.', '21.', '21.1', '21.2', '21.3', '21.4', '22.', '22.1', '22.1.1', '22.1.2', '22.1.3', '22.1.4', '22.2', '22.2.1', '22.2.2', '22.3', '22.4', '22.5', '22.6', '22.6.1', '22.6.2', '22.7', '23.', '23.1', '23.1.1', '23.1.1(a)', '23.1.1(b)', '23.1.1(c)', '23.1.2', '23.1.3', '23.1.3(a)', '23.1.3(b)', '23.1.4', '23.2', '23.2.1', '23.2.2', '23.3', '23.4', '23.5', '24.', '24.1', '24.1.1', '24.1.2', '24.1.3', '24.1.4', '24.1.5', '24.1.6', '24.2', '24.3', '25.', '25.1', '25.1.1', '25.1.2', '25.1.3', '25.1.4', '25.1.5', '25.1.6', '25.2', '25.2.1', '25.2.2', '25.2.3', '25.2.4', '25.2.5', '25.3', '26.', '27.', '27.1', '27.2', '27.2.1', '27.2.2', '27.2.3', '27.3', '28.', '28.1', '28.2', '28.2.1', '28.2.2', '28.2.3', '28.2.4', '28.2.5', '28.3', '29.', '29.1', '29.1.1', '29.1.2', '29.1.3', '29.2', '30.', '30.1', '30.2', '30.2.1', '30.2.2', '30.2.3', '30.3', '30.4', '31.', '31.1', '31.2', '31.3', '31.4', '31.5', '31.5.1', '31.5.2', '31.6', '31.7', '31.8', '31.8.1', '31.8.2', '31.8.3', '31.9', '31.10', '31.11'])

in this, if I have something referred in key 3.1, I also want to extract the data for keys 3.1.1 and 3.1.2(child keys).在此,如果我在键 3.1 中引用了某些内容,我还想提取键 3.1.1 和 3.1.2(子键)的数据。 so for this I used str.startwith() method which fails in cases as such.所以为此我使用了 str.startwith() 方法,该方法在这种情况下会失败。 It will not only return me the child clauses but also other clauses like 3.10, 3.11... which also start with 3.1, so this will result in some false positives and leakages.它不仅会返回子子句,还会返回其他子句,例如 3.10、3.11... 也以 3.1 开头,因此这将导致一些误报和泄漏。

The caveat however being that I don't know for sure that what numbering system will the user be using in the agreement.但是需要注意的是,我不确定用户将在协议中使用什么编号系统。 for example: it could be anything like:例如:它可能是这样的:

['(1.)', '(1.)a.', '(2.)'] or ['1.', '1.1.', '1.1.1.', '1.1.1.a',] or ['1.', '1.1', '1.1.1', '1.1.1a']

so I'm trying to figure out a function using which I can cover all these ways.所以我试图找出一个可以涵盖所有这些方式的函数。 How can I do that?我怎样才能做到这一点? Thanks in advance!提前致谢! :) :)

You're much more familiar with your dataset and how robust you need to be, but I set that set of dict_keys to keys and used the following:您更熟悉您的数据集以及您需要有多健壮,但我将该组dict_keys设置为keys并使用以下内容:

if key.count('.') < 2:
    child_keys = [k for k in keys if k.startswith(key + '.')]
else:
    child_keys = [k for k in keys if k.startswith(key + '(')]

If key = '1.2.2' , then child_keys = ['1.2.2(a)', '1.2.2(b)', '1.2.2(c)', '1.2.2(d)'] .如果key = '1.2.2' ,则child_keys = ['1.2.2(a)', '1.2.2(b)', '1.2.2(c)', '1.2.2(d)'] If key = '3.1' , then child_keys = ['3.1.1', '3.1.2'] .如果key = '3.1' ,则child_keys = ['3.1.1', '3.1.2']

def get_subsequent_keys(key):
    subsequent_keys = []
    total_keys = structure.keys()
    try:
        if key[-1] == '.' or key[-1] == ')':
            if key in total_keys:
                pass
            elif key[:-1] in total_keys:
                subsequent_keys.append(key[:-1])
            subsequent_keys += [x for x in total_keys if x.startswith(key)]
        else:
            key_pat = fr"({key}[a-z]+)"
            subsequent_keys.append(key)
            subsequent_keys += [x for x in total_keys if x.startswith(key + '.')]
            subsequent_keys += [x for x in total_keys if x.startswith(key + '(')]
            subsequent_keys += re.findall(key_pat, '   '.join(total_keys))
    except Exception as exception:
        logger.debug(exception)
    finally:
        return subsequent_keys

So I used this way to cover all the possible cases.所以我用这种方式覆盖了所有可能的情况。 The problem with my use case is that I don't really have a definite way by which the user numbers the clauses in the agreement.我的用例的问题是我真的没有明确的方式让用户对协议中的条款进行编号。 Hence I needed a generic function to cover all ground.因此,我需要一个通用函数来覆盖所有领域。 Also, thanks to Ava and Hayley for their help!另外,感谢 Ava 和 Hayley 的帮助! :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM