我想将此数据从我的 spark rdd 转换为字典

I want to convert this data from my spark rdd to a dictonary

数据就像一本字典,但在方括号内,这使它成为一个列表。 名单如下:

a = [{'sI': ['17046', '17043'], 'sQ': ['15800', '15789'], 'rid': 572, 'pid': 511, 'uid': 411, 'st': 1594892854.513586, 'et': '16

如果你的原始数据是一个单元素列表,那么你应该只使用零索引,你会得到字典。

代码:

raw_data = [
    {
        "sI": ["17046", "17043"],
        "sQ": ["15800", "15789"],
        "rid": 572,
        "pid": 511,
        "uid": 411,
        "st": 1594892854.513586,
        "et": "16",
    }
]

print("Raw type: {}".format(type(raw_data)))

converted_data = raw_data[0]  # Get the first element of list.

print("Converted type: {}".format(type(converted_data)))

输出:

>>> python3 test.py
Raw type: <class 'list'>
Converted type: <class 'dict'>

如果您的列表包含更多的指令,那么您可以在 for 循环中一个一个地获取指令。我给你写了一个例子。

代码:

raw_data = [
    {
        "sI": ["17046", "17043"],
        "sQ": ["15800", "15789"],
    },
    {
        "sI": ["2", "3"],
        "sQ": ["4", "5"],
    }
]  # This list contains 2 dicts

print("Raw type: {}".format(type(raw_data)))

for item in raw_data:
    print("Type inside loop: {}".format(type(item)))  # Getting dicts one-by-one ("item" variable contains it)

输出:

>>> python3 test.py
Raw type: <class 'list'>
Type inside loop: <class 'dict'>
Type inside loop: <class 'dict'>