Python 我想将这些数据从spark rdd转换为听写

Python 我想将这些数据从spark rdd转换为听写,python,arrays,list,dictionary,rdd,Python,Arrays,List,Dictionary,Rdd,数据就像一本字典,但放在方括号内,这使它成为一个列表。 名单如下: a=[{'sI':['17046','17043'],'sQ':['15800','15789'],'rid':572,'pid':511,'uid':411,'st':1594892854.513586,'et':'16如果您的原始数据是一个元素列表,那么您应该只使用零索引,您将获得字典 代码: raw_data = [ { "sI": ["17046", &qu

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


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'>
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'>
输出:

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'>
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'>
输出:

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'>
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'>
>python3 test.py
原始类型:
在循环内键入:
在循环内键入:

如果您提供一个有效的Python列表就好了。其他人无法确定您的列表是否只包含一个或多个元素。