Python 从字典中删除特定字符串

Python 从字典中删除特定字符串,python,python-3.x,dictionary,string-formatting,Python,Python 3.x,Dictionary,String Formatting,我有一本这种格式的字典 Open = {'22.0x7.5x8.0 12.0': ['4.60x4.30x4.30 13.00, 4.60x4.30x4.30 1.00, 4.60x4.30x4.30 2.00, 6.60x6.00x5.16 5.00'], '18.0x7.0x7.0 7.0': ['4.60x4.30x4.30 1.00, 8.75x6.60x5.60 4.00'], '2

我有一本这种格式的字典

Open = {'22.0x7.5x8.0 12.0': ['4.60x4.30x4.30 13.00, 4.60x4.30x4.30 1.00, 
                              4.60x4.30x4.30 2.00, 6.60x6.00x5.16 5.00'], 
         '18.0x7.0x7.0 7.0': ['4.60x4.30x4.30 1.00, 8.75x6.60x5.60 4.00'],
           '22.0x7.5x8.0 9.0': ['6.60x6.00x5.16 5.00, 6.60x6.00x5.16 9.00, 
                                6.60x6.00x5.16 5.00']}
我想从键和值中删除尺寸标注部分1x2x3,并将其余部分转换为整数。我该怎么做

这样我的输出是这样的

new = {12:[13,1,2,5],
          7:[1,4]...}
使用str.split

例:

输出:

使用str.split就足够了

Open = {'22.0x7.5x8.0 12.0': ['4.60x4.30x4.30 13.00, 4.60x4.30x4.30 1.00',
                              '4.60x4.30x4.30 2.00, 6.60x6.00x5.16 5.00'],
         '18.0x7.0x7.0 7.0': ['4.60x4.30x4.30 1.00, 8.75x6.60x5.60 4.00'],
           '22.0x7.5x8.0 9.0': ['6.60x6.00x5.16 5.00, 6.60x6.00x5.16 9.00',
                                '6.60x6.00x5.16 5.00']}

res = {}
for key,value in Open.items():

    #Split on space and convert to int for key
    k = int(float(key.split()[1]))
    li = []
    for v in value:
        #First split on comma
        for i in v.split(','):
            #Then split on space
            num = int(float(i.split()[1]))
            #Append the number to a list
            li.append(num)
    #Assign the list to the key
    res[k] = li

print(res)
输出将是

{12: [13, 1, 2, 5], 7: [1, 4], 9: [5, 9, 5]}

你有没有试过?您尝试过str.split吗?尝试过什么代码?编写一个与模式匹配的正则表达式,并使用它将字符串替换为空字符串。然后对键和值强制转换为int,将字符串拆分为,并将每个列表元素强制转换为int.print{k.split[1]:[j.split[1]对于i-in-v对于i.split中的j,]对于k,v对于Open.items}?虽然答案很好,但由于dict-comprehension中for循环的深度,在我看来不是很可读!我们也可以用int替换float吗?我试过了,它说我不能用int和float itemsDone,先生@RahulSharma只是在将来做intfloat'12.0'来获得12等
Open = {'22.0x7.5x8.0 12.0': ['4.60x4.30x4.30 13.00, 4.60x4.30x4.30 1.00',
                              '4.60x4.30x4.30 2.00, 6.60x6.00x5.16 5.00'],
         '18.0x7.0x7.0 7.0': ['4.60x4.30x4.30 1.00, 8.75x6.60x5.60 4.00'],
           '22.0x7.5x8.0 9.0': ['6.60x6.00x5.16 5.00, 6.60x6.00x5.16 9.00',
                                '6.60x6.00x5.16 5.00']}

res = {}
for key,value in Open.items():

    #Split on space and convert to int for key
    k = int(float(key.split()[1]))
    li = []
    for v in value:
        #First split on comma
        for i in v.split(','):
            #Then split on space
            num = int(float(i.split()[1]))
            #Append the number to a list
            li.append(num)
    #Assign the list to the key
    res[k] = li

print(res)
{12: [13, 1, 2, 5], 7: [1, 4], 9: [5, 9, 5]}