简体   繁体   English

将字典的字符串转换为int

[英]Convert Strings of a dictionary to int

I have a dictionary loaded: 我已加载字典:

{'17.0x6.0x7.0 6.0': ['4.60x4.30x4.30 4.00, 4.60x4.30x4.30 1.00, 4.60x4.30x4.30 3.00'],
 '9.5x5.5x5.5 1.0': ['4.60x4.30x4.30 5.00'],
 '22.0x7.5x8.0 10.0': ['6.60x6.00x5.16 8.00, 9.00x6.00x5.60 6.00'],
 '17.0x6.0x7.0 6.0_1': ['8.75x6.60x5.60 7.00'],
 '9.5x5.5x5.5 2.0': ['4.60x4.30x4.30 2.00']}

I want to extract the float values after dimensions 我想提取尺寸后的浮点值

Desired output: 所需的输出:

{6:[4,1,3],
 1:[5],
 10:[8,6],
 6:[7]
 2:[2]}

I tried this to extract the dimensions 我尝试过提取尺寸

loaded_items = {int(float(k.split()[1])): [int(float(j.split()[1])) for i in v for j in i.split(",")] for k,v in loaded.items()}

but this gives me output as such: 但这给了我这样的输出:

{6: [7], 1: [5], 10: [8, 6], 2: [2]}

ie when it encounters the second similar object it replaces the value. 即,当遇到第二个相似的对象时,它将替换该值。

I tried one more method: 我尝试了另一种方法:

all_items_loaded = {}
for k,v in loaded.items():
    a = k.split()[1]
    print(a)
    for i in v:
        x = i.split()
        x = x[1::2]
        all_items_loaded[a] = x

and the output is: 输出为:

{'6.0': ['4.00,', '1.00,', '3.00'],
 '1.0': ['5.00'],
 '10.0': ['8.00,', '6.00'],
 '6.0_1': ['7.00'],
 '2.0': ['2.00']}

There are problems with your data and the required output. 您的数据和所需的输出存在问题。

The input dict contains a key '17.0x6.0x7.0 6.0_1 '. 输入字典包含键“ 17.0x6.0x7.0 6.0_1 ”。 While calling int(float()) on '6.0_1' works (it is cast to 6.01 and subsequently to 6) I would not recommend this and rather investigate the reason why this data is supplied in this way. 在'6.0_1'上调用int(float())时(将其强制转换为6.01,然后转换为6),我不建议这样做,而是调查以这种方式提供此数据的原因。

The desired output dict contains duplicate keys. 所需的输出字典包含重复的键。 This is not possible and causes the oerwriting you are describing. 这是不可能的,并且会导致您描述的书写。 Change your output data structure from dict to eg list of dicts. 将输出数据结构从字典更改为例如字典列表。

[{int(float(key.split()[1])): [int(float(val.split()[1])) for val in value[0].split(",")]} for (key,value) in a.items() ]
--> [{6: [4, 1, 3]}, {1: [5]}, {10: [8, 6]}, {6: [7]}, {2: [2]}]

You either need to decide on a different way of associating the data or choose a different data structure to use. 您或者需要确定关联数据的其他方式,或者选择要使用的其他数据结构。 One example, a defaultdict , is below. 下面是一个示例defaultdict This is not the only solution and getting a better input would be the first priority. 不是唯一的解决方案,获得更好的输入将是第一要务。

from collections import defaultdict

d = defaultdict(list)
d['6.0'].append([7])
d['6.0'].append([4,1,3])

print(d['6.0'])

# result [[7], [4, 1, 3]]

The only suggestion I can make (aside from getting a better input) is to create tuples. 我可以提出的唯一建议(除了获得更好的输入)是创建元组。 Create a simple function to extract values from strings: 创建一个简单的函数从字符串中提取值:

>>> def extract(v): return int(float(v.split()[1]))

And apply this function to dictionary key-values pairs and produce a list of pairs: 并将此函数应用于字典键值对并生成对列表:

>>> loaded_items = {
...     '17.0x6.0x7.0 6.0': ['4.60x4.30x4.30 4.00, 4.60x4.30x4.30 1.00, 4.60x4.30x4.30 3.00'],
...     '9.5x5.5x5.5 1.0': ['4.60x4.30x4.30 5.00'],
...     '22.0x7.5x8.0 10.0': ['6.60x6.00x5.16 8.00, 9.00x6.00x5.60 6.00'],
...     '17.0x6.0x7.0 6.0_1': ['8.75x6.60x5.60 7.00'],
...     '9.5x5.5x5.5 2.0': ['4.60x4.30x4.30 2.00']
... }
>>> data = [(extract(k), [extract(v) for v in vs[0].split(",")])  for k, vs in loaded_items.items()]
>>> data
[(6, [4, 1, 3]), (1, [5]), (10, [8, 6]), (6, [7]), (2, [2])]

You can then group the values if needed: 然后,可以根据需要对值进行分组:

>>> d1 = {}
>>> for k,v in data: d1.setdefault(k, []).extend(v)
>>> d1
{6: [4, 1, 3, 7], 1: [5], 10: [8, 6], 2: [2]}

Or: 要么:

>>> d2 = {}
>>> for k,v in data: d2.setdefault(k, []).append(v)
>>> d2
{6: [[4, 1, 3], [7]], 1: [[5]], 10: [[8, 6]], 2: [[2]]}

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

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