如何将字典转换为 Python 中的对象数组

huangapple go评论74阅读模式
英文:

How to convert dict into array of objects in python

问题

I've got it!

英文:

I have a dict list dict(streams_list) which gives values example- {'Generic': ['_96_fkGECEeyqHrLeykGtFQ'], 'SF1B': ['_9lG7AJ8PEey0L88pC_veNw']}

How can I convert this to array of objects ? example -

[{'Generic': '_96_fkGECEeyqHrLeykGtFQ'}, {'SF1B': '_9lG7AJ8PEey0L88pC_veNw'},....]

答案1

得分: 3

{'Generic': '_96_fkGECEeyqHrLeykGtFQ'}, {'SF1B': '_9lG7AJ8PEey0L88pC_veNw'}

英文:

Just do this:

my_dict = {'Generic': ['_96_fkGECEeyqHrLeykGtFQ'], 'SF1B': ['_9lG7AJ8PEey0L88pC_veNw']}
my_list = [{k:v[0]} for k,v in my_dict.items()]

Result:

{'Generic': '_96_fkGECEeyqHrLeykGtFQ'}, {'SF1B': '_9lG7AJ8PEey0L88pC_veNw'}]

答案2

得分: 2

dict = {'Generic': ['_96_fkGECEeyqHrLeykGtFQ'], 'SF1B': ['_9lG7AJ8PEey0L88pC_veNw']}

result = [{key: values[0]} for key, values in dict.items()]

print(result)
英文:
dict = {'Generic': ['_96_fkGECEeyqHrLeykGtFQ'], 'SF1B': ['_9lG7AJ8PEey0L88pC_veNw']}

result = [{key: values[0]} for key, values in dict.items()]

print(result)

答案3

得分: 2

代替使用索引,您可以使用一些变量解包的魔法:

data = {'Generic': ['_96_fkGECEeyqHrLeykGtFQ'], 'SF1B': ['_9lG7AJ8PEey0L88pC_veNw']}
result = [{k, v} for k, (v,) in data.items()]

[{'Generic', '_96_fkGECEeyqHrLeykGtFQ'}, {'SF1B', '_9lG7AJ8PEey0L88pC_veNw'}]
英文:

Instead of using indexing you could use some variable unpacking magic:

data = {'Generic': ['_96_fkGECEeyqHrLeykGtFQ'], 'SF1B': ['_9lG7AJ8PEey0L88pC_veNw']}    
result = [{k, v} for k, (v,) in data.items()]

[{'Generic', '_96_fkGECEeyqHrLeykGtFQ'}, {'SF1B', '_9lG7AJ8PEey0L88pC_veNw'}]

答案4

得分: 2

streams_list: 列表[元组[字符串, 列表[字符串]]] = [
    ('Generic', ['_96_fkGECEeyqHrLeykGtFQ']),
    ('SF1B', ['_9lG7AJ8PEey0L88pC_veNw']),
]

测试定义您请求的行为:

def test_transform():
expected_output: 列表[字典[字符串, 字符串]] = [
{'Generic': '_96_fkGECEeyqHrLeykGtFQ'},
{'SF1B': '_9lG7AJ8PEey0L88pC_veNw'},
]
actual_output = transform(input=streams_list)
assert all(
expected_dict in actual_output
for expected_dict in expected_output
)


通过测试的一种实现:

def transform(input: 列表[元组[字符串, 列表[字符串]]]) -> 列表[字典[字符串, 字符串]]:
output: 列表[字典[字符串, 字符串]] = []
for pair in input:
key: 字符串
val: 列表[字符串]
key, val = pair
output.append({key: val[0]})
return output


<details>
<summary>英文:</summary>

Inferred input that you are using:

streams_list: list[tuple[str, list[str]]] = [
('Generic', ['_96_fkGECEeyqHrLeykGtFQ']),
('SF1B', ['_9lG7AJ8PEey0L88pC_veNw']),
]


Test defining your requested behavior:

def test_transform():
expected_output: list[dict[str, str]] = [
{'Generic': '_96_fkGECEeyqHrLeykGtFQ'},
{'SF1B': '_9lG7AJ8PEey0L88pC_veNw'},
]
actual_output = transform(input=streams_list)
assert all(
expected_dict in actual_output
for expected_dict in expected_output
)


One implementation that passes said test:

def transform(input: list[tuple[str, list[str]]]) -> list[dict[str, str]]:
output: list[dict[str, str]] = []
for pair in input:
key: str
val: list[str]
key, val = pair
output.append({key: val[0]})
return output



</details>



# 答案5
**得分**: 1

为解决你的问题,请尝试以下代码:
```python
your_dictionary = {'Generic': ['_96_fkGECEeyqHrLeykGtFQ'], 'SF1B': ['_9lG7AJ8PEey0L88pC_veNw']}
your_list = [{your_object: object_key[0]} for your_object, object_key in your_dictionary.items()]
print(your_list)

输出:

{'Generic': '_96_fkGECEeyqHrLeykGtFQ'}, {'SF1B': '_9lG7AJ8PEey0L88pC_veNw'}]
英文:

To solve your question, try the following code:

your_dictionary = {&#39;Generic&#39;: [&#39;_96_fkGECEeyqHrLeykGtFQ&#39;], &#39;SF1B&#39;: [&#39;_9lG7AJ8PEey0L88pC_veNw&#39;]}
your_list = [{your_object:object_key[0]} for your_object, object_key in your_dictionary.items()]
print(your_list)

Output:

{&#39;Generic&#39;: &#39;_96_fkGECEeyqHrLeykGtFQ&#39;}, {&#39;SF1B&#39;: &#39;_9lG7AJ8PEey0L88pC_veNw&#39;}]

huangapple
  • 本文由 发表于 2023年4月6日 21:14:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定