简体   繁体   English

从列表中划分字母数字词并将其存储为字典的键值对

[英]Divide alphanumeric word from a list and store as a key value pair of a dict

I have a list of alphanumeric data, 我有一个字母数字数据列表,

my_list = ["A1B2244", "B3H7654", "A1O6541", "J4777"]

I need to divide each word in dict form like 我需要按字典形式将每个单词分开

{"A1": ["B2244", "O6541"], "B3": ["H7654"], "J4": ["777"]}

Could you please let me know the easiest way to do this in python. 您能否让我知道在python中执行此操作的最简单方法。

my_list = ['A1B2244', 'B3H7654', 'A1O6541', 'J4777']
my_dict={i[:2]:i[2:] for i in my_list}

Edit: Sorry I didn't notice the replication in your output. 编辑:对不起,我没有在您的输出中注意到复制。 Others have short solutions, but a pure pythonic way is: 其他人有简短的解决方案,但纯Python方式是:

my_list = ['A1B2244', 'B3H7654', 'A1O6541', 'J4777']
my_dict={}
for i in my_list:
    if i[:2] in my_dict:
        my_dict[i[:2]].append(i[2:])
    else:
        my_dict[i[:2]]=[i[2:]]

You can use itertools.groupby to group the elements of your list based on your condition(first two characters). 您可以使用itertools.groupby根据您的条件(前两个字符)对列表中的元素进行分组。 Then supply the result to dict constructor 然后将结果提供给dict构造函数

>>> from itertools import groupby
>>> dict([(k,list(g)) for k,g in groupby(sorted(k),key=lambda x: x[:2])])
>>> {'J4': ['J4777'], 'A1': ['A1B2244', 'A1O6541'], 'B3': ['B3H7654']}
list = ['A1B2244', 'B3H7654', 'A1O6541', 'J4777']
#first initialize lists based 2 first elements
d= {i[:2]:[] for i in list}
#loop to add items by key
[d.get(i[:2]).append(i[2:]) for i in list]
print(d)

output: 输出:

{'A1': ['B2244', 'O6541'], 'J4': ['777'], 'B3': ['H7654']}

Just to add to the first answer by Willian Vieira, I thought it would be helpful to know the output print(d) right after d= {i[:2]:[] for i in list} , which is: 为了补充Willian Vieira的第一个答案,我认为知道d= {i[:2]:[] for i in list}之后的输出print(d)会有所帮助,这是:

{'A1': [], 'B3': [], 'J4': []}

just to clarify this line in the two-line solution. 只是为了在两行解决方案中阐明这一行。 Just to see how the keys are made from taking the first two characters in each element of the list (without duplication of these characters), and the values are initialized as empty lists. 只是看看如何通过使用列表中每个元素的前两个字符(不重复这些字符)来制作键,并将值初始化为空列表。

As per your comment to the question, rule to split is: split after the first digit. 根据您对问题的评论,拆分的规则是:在第一个数字后拆分。 So, you can search for the index of the first digit, split and add to the dict. 因此,您可以搜索第一位数的索引,进行拆分并添加到字典中。 I ignored an input with no digits. 我忽略了没有数字的输入。

def first_index_of_digit(st):
    for i in range(len(st)):
        if st[i].isdigit():
            return i
    return -1

my_list = ["A1B2244", "B3H7654", "A1O6541", "J4777"]

dd = dict()

for item in my_list:
    i = first_index_of_digit(item)
    if (i == -1):
        continue
    k, v = item[:i+1], item[i+1:]

    if (dd.get(k, 0) == 0):
        dd[k] = list()
    dd[k].append(v)

print(dd)
# {'A1': ['B2244', 'O6541'], 'B3': ['H7654'], 'J4': ['777']}

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

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