如何在 Python 中将 OrderedDict 仅转换为值列表?
How to convert OrderedDict to a list of values only, in Python?
我有一个 OrderedDict
对象,我想制作一个仅包含值的列表,同时保持它们在字典中的顺序相同。有开箱即用的方法吗?我的 OrderedDict
对象看起来像这样:
OrderedDict(
[
(
datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
{
"time": datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
"angle": 74.628235,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
),
(
datetime.datetime(2022, 5, 12, 6, 7, 8, 9),
{
"time": datetime.datetime(2022, 3, 12, 6, 17, 8, 90980),
"angle": 8.054147,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
)
]
)
我的预期输出是这样的:
[[datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),74.628235,[15.0, 65.0, 10.0, 130.0]],
[datetime.datetime(2022, 3, 12, 6, 17, 8, 90980),8.054147,[15.0, 65.0, 10.0, 130.0]]
仅供参考。
import datetime
OrderedDict = [
(
datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
{
"time": datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
"angle": 74.628235,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
),
(
datetime.datetime(2022, 5, 12, 6, 7, 8, 9),
{
"time": datetime.datetime(2022, 3, 12, 6, 17, 8, 90980),
"angle": 8.054147,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
)
]
lst = [list(a[1].values()) for a in OrderedDict]
print(lst)
输出
[[datetime.datetime(2022, 5, 11, 7, 43, 0, 90913), 74.628235, [15.0, 65.0, 10.0, 130.0]],
[datetime.datetime(2022, 3, 12, 6, 17, 8, 90980), 8.054147, [15.0, 65.0, 10.0, 130.0]]]
用于字典。
import datetime
OrderedDict = [
(
datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
{
"time": datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
"angle": 74.628235,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
),
(
datetime.datetime(2022, 5, 12, 6, 7, 8, 9),
{
"time": datetime.datetime(2022, 3, 12, 6, 17, 8, 90980),
"angle": 8.054147,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
)
]
lst = [a[1] for a in OrderedDict]
print(lst)
输出
[{'time': datetime.datetime(2022, 5, 11, 7, 43, 0, 90913), 'angle': 74.628235, 'sizes': [15.0, 65.0, 10.0, 130.0]},
{'time': datetime.datetime(2022, 3, 12, 6, 17, 8, 90980), 'angle': 8.054147, 'sizes': [15.0, 65.0, 10.0, 130.0]}]
我有一个 OrderedDict
对象,我想制作一个仅包含值的列表,同时保持它们在字典中的顺序相同。有开箱即用的方法吗?我的 OrderedDict
对象看起来像这样:
OrderedDict(
[
(
datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
{
"time": datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
"angle": 74.628235,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
),
(
datetime.datetime(2022, 5, 12, 6, 7, 8, 9),
{
"time": datetime.datetime(2022, 3, 12, 6, 17, 8, 90980),
"angle": 8.054147,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
)
]
)
我的预期输出是这样的:
[[datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),74.628235,[15.0, 65.0, 10.0, 130.0]],
[datetime.datetime(2022, 3, 12, 6, 17, 8, 90980),8.054147,[15.0, 65.0, 10.0, 130.0]]
仅供参考。
import datetime
OrderedDict = [
(
datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
{
"time": datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
"angle": 74.628235,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
),
(
datetime.datetime(2022, 5, 12, 6, 7, 8, 9),
{
"time": datetime.datetime(2022, 3, 12, 6, 17, 8, 90980),
"angle": 8.054147,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
)
]
lst = [list(a[1].values()) for a in OrderedDict]
print(lst)
输出
[[datetime.datetime(2022, 5, 11, 7, 43, 0, 90913), 74.628235, [15.0, 65.0, 10.0, 130.0]],
[datetime.datetime(2022, 3, 12, 6, 17, 8, 90980), 8.054147, [15.0, 65.0, 10.0, 130.0]]]
用于字典。
import datetime
OrderedDict = [
(
datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
{
"time": datetime.datetime(2022, 5, 11, 7, 43, 0, 90913),
"angle": 74.628235,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
),
(
datetime.datetime(2022, 5, 12, 6, 7, 8, 9),
{
"time": datetime.datetime(2022, 3, 12, 6, 17, 8, 90980),
"angle": 8.054147,
"sizes": [15.0, 65.0, 10.0, 130.0],
},
)
]
lst = [a[1] for a in OrderedDict]
print(lst)
输出
[{'time': datetime.datetime(2022, 5, 11, 7, 43, 0, 90913), 'angle': 74.628235, 'sizes': [15.0, 65.0, 10.0, 130.0]},
{'time': datetime.datetime(2022, 3, 12, 6, 17, 8, 90980), 'angle': 8.054147, 'sizes': [15.0, 65.0, 10.0, 130.0]}]