英文:
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 = {'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)
Output:
{'Generic': '_96_fkGECEeyqHrLeykGtFQ'}, {'SF1B': '_9lG7AJ8PEey0L88pC_veNw'}]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论