使用子字符串将字典转换为列表

发布于 2025-01-21 02:38:21 字数 530 浏览 2 评论 0原文

我有以下内容:

dict = {
    "HVAC_flex[1,1]": 8.0,
    "HVAC_flex[1,2]": 15.0,
    "HVAC_flex[2,1]": 0.0,
    "HVAC_flex[2,2]": 0.0,
    "DHW_flex[1,1]": 0.0,
    "DHW_flex[1,2]": 2.0,
    "DHW_flex[2,1]": 4.0,
    "DHW_flex[2,2]": 17.0
}

我想产生的是两个列表,如下所示:

HVAC_flex = [ 8.0, 15.0, 0.0, 0.0]
DHW_flex = [ 0.0, 2.0, 4.0, 17.0]

事实是,尺寸是动态定义的,因此dict可能包括其他值。

我的想法是搜索substring,例如hvac_flex被绑定在密钥中,然后将值放入此列表等,但是我可以找到一种巧妙的方法来实现此目标。有办法这样做吗?

I have the following dict:

dict = {
    "HVAC_flex[1,1]": 8.0,
    "HVAC_flex[1,2]": 15.0,
    "HVAC_flex[2,1]": 0.0,
    "HVAC_flex[2,2]": 0.0,
    "DHW_flex[1,1]": 0.0,
    "DHW_flex[1,2]": 2.0,
    "DHW_flex[2,1]": 4.0,
    "DHW_flex[2,2]": 17.0
}

And what I would like to produce is two lists as follows:

HVAC_flex = [ 8.0, 15.0, 0.0, 0.0]
DHW_flex = [ 0.0, 2.0, 4.0, 17.0]

The thing is that the dimensions are dynamically defined, so the dict may include additional values.

My idea is to search for substrings, like if HVAC_flex is inluded in the key, then put the value into this list, etc, but I could find a clever way to implement this. Is there a way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野心澎湃 2025-01-28 02:38:21

您无法创建这些“命名”列表 - 但是您可以使用列表理解来创建值列表的列表:

d = {
    "HVAC_flex[1,1]": 8.0,
    "HVAC_flex[1,2]": 15.0,
    "HVAC_flex[2,1]": 0.0,
    "HVAC_flex[2,2]": 0.0,
    "DHW_flex[1,1]": 0.0,
    "DHW_flex[1,2]": 2.0,
    "DHW_flex[2,1]": 4.0,
    "DHW_flex[2,2]": 17.0
}

prefix = ["HVAC", "DHW"]

values = [ [v for key,v in d.items() if key.startswith(b)] for b in prefix ]

print(values)

输出:

[[8.0, 15.0, 0.0, 0.0], [0.0, 2.0, 4.0, 17.0]]

使用字典理解来创建另一个字典可能更有意义:

values = {b: [v for key,v in d.items() if key.startswith(b)] for b in prefix }

print(values)

保留有关前缀的信息:

{'HVAC': [8.0, 15.0, 0.0, 0.0], 'DHW': [0.0, 2.0, 4.0, 17.0]}

You cannot create those "named" lists - you can however use a list comprehension to create a list of list of values:

d = {
    "HVAC_flex[1,1]": 8.0,
    "HVAC_flex[1,2]": 15.0,
    "HVAC_flex[2,1]": 0.0,
    "HVAC_flex[2,2]": 0.0,
    "DHW_flex[1,1]": 0.0,
    "DHW_flex[1,2]": 2.0,
    "DHW_flex[2,1]": 4.0,
    "DHW_flex[2,2]": 17.0
}

prefix = ["HVAC", "DHW"]

values = [ [v for key,v in d.items() if key.startswith(b)] for b in prefix ]

print(values)

Output:

[[8.0, 15.0, 0.0, 0.0], [0.0, 2.0, 4.0, 17.0]]

It would probably make more sense to use a dict comprehension to create another dict:

values = {b: [v for key,v in d.items() if key.startswith(b)] for b in prefix }

print(values)

to keep the information about the prefix:

{'HVAC': [8.0, 15.0, 0.0, 0.0], 'DHW': [0.0, 2.0, 4.0, 17.0]}
乱世争霸 2025-01-28 02:38:21

您可以检查关键名称是否包含HVACDHW。创建一个列表并在其各自列表中附加值。

d = {
    "HVAC_flex[1,1]": 8.0,
    "HVAC_flex[1,2]": 15.0,
    "HVAC_flex[2,1]": 0.0,
    "HVAC_flex[2,2]": 0.0,
    "DHW_flex[1,1]": 0.0,
    "DHW_flex[1,2]": 2.0,
    "DHW_flex[2,1]": 4.0,
    "DHW_flex[2,2]": 17.0
}

HVAC_flex, DHW_flex = [], []
for k,v in d.items():
    if "HVAC" in k:
        HVAC_flex.append(v)
    else: 
        DHW_flex.append(v)
print(HVAC_flex)
print(DHW_flex)

我正在使用else,因为您的字典中只有两个子字符串,但是如果它们包含更多的子字符串,请尝试在Square Brackets之前获取文本:

prefix = []
for k,v in d.items():
    if k.split('[')[0] not in prefix: # this splits the string into part containing '[' by using [0] we get the string part. 
        prefix.append(k.split('[')[0])
    else:
        continue

输出:

[8.0, 15.0, 0.0, 0.0]
[0.0, 2.0, 4.0, 17.0]

You can check if the key name contains HVAC or DHW. Create a list and append the values in their respective list.

d = {
    "HVAC_flex[1,1]": 8.0,
    "HVAC_flex[1,2]": 15.0,
    "HVAC_flex[2,1]": 0.0,
    "HVAC_flex[2,2]": 0.0,
    "DHW_flex[1,1]": 0.0,
    "DHW_flex[1,2]": 2.0,
    "DHW_flex[2,1]": 4.0,
    "DHW_flex[2,2]": 17.0
}

HVAC_flex, DHW_flex = [], []
for k,v in d.items():
    if "HVAC" in k:
        HVAC_flex.append(v)
    else: 
        DHW_flex.append(v)
print(HVAC_flex)
print(DHW_flex)

I'm using else because there are only two substrings in your dictionary but if they contain more try getting the text before square brackets:

prefix = []
for k,v in d.items():
    if k.split('[')[0] not in prefix: # this splits the string into part containing '[' by using [0] we get the string part. 
        prefix.append(k.split('[')[0])
    else:
        continue

Output:

[8.0, 15.0, 0.0, 0.0]
[0.0, 2.0, 4.0, 17.0]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文