我怎样才能得到类型(c1) - >列表,而不是键入str

时间:2018-05-22 06:52:05

标签: python

对不起,我是noob T.T

import numpy as np

import json

import ast

import itertools

data1 = b'[{"id":"1","array1_1":"0","array1_2":"1","array2_1":"2","array2_2":"3","array3_1":"44","array3_2":"55"},{"id":"asd","array1_1":"234","array1_2":"[[0, 26, 60, 45], [0, -45, 45]]","array2_1":"[[23, 345], [543, 321]]","array2_2":"jytj","array3_1":"5","array3_2":"6"},{"id":"bb","array1_1":"bbb","array1_2":"ad","array2_1":"[[23, 345], [543, 321]]","array2_2":"[[0, 45], [45]]","array3_1":"[[34, 45], [66, 88]]","array3_2":"sfw"},{"id":"aa","array1_1":"aa","array1_2":"[[0, 26, 60, 45], [0, -45, 45]]","array2_1":"gfd","array2_2":"[[0, 45], [45]]","array3_1":"sdf","array3_2":"[[26, 60], [0], [56, 34, 45]]"},{"id":"ss","array1_1":"00","array1_2":"[[0, 45], [0, -45, 45], [0, -45, 45, -26]]","array2_1":"[[31, 233], [152, 151], [190, 204], [205, 87], [231, 48], [255, 194]]","array2_2":"[[0, 45], [45], [45], [26, 45, 63], [45], [0, 26, 45, 63]]","array3_1":"[[29, 194], [32, 234], [165, 229], [232, 46], [258, 190]]","array3_2":"[[45], [0, 45], [0, 26, 45], [0, -63, 45, 63], [0, 26, 45]]"}]'

data2 = data1.decode()

data3 = {"key" : [data2]}

s = json.dumps(data3)

data4 = str.encode(s)

data5 = json.loads(data4)

data6 = data5['key']

data7 = data6[0]

data8 = ast.literal_eval(data7)



d0 = [d['id']for d in data8]

d1 = [d['array1_1']for d in data8]

d2 = [d['array1_2']for d in data8]

d3 = [d['array2_1']for d in data8]

d4 = [d['array2_2']for d in data8]

d5 = [d['array3_1']for d in data8]

d6 = [d['array3_2']for d in data8]

da = [d1,d2,d3,d4,d5,d6]

for x in zip(*da):
    y = list(x)

a1 = y[0]

a2 = y[1]

a3 = y[2]

a4 = y[3]

a5 = y[4]

a6 = y[5]

b1 = np.array(a1)

b2 = np.array(a2)

b3 = np.array(a3)

b4 = np.array(a4)

b5 = np.array(a5)

b6 = np.array(a6)

c1 = b2.tolist()

c2 = b4.tolist()

c4 = b6.tolist()

print(type(c1))

当我使用print(list(c1))

shell告诉我

['[', '[', '0', ',', ' ', '4', '5', ']', ',', ' ', '[', '0', ',', ' ', '-', '4', '5', ',', ' ', '4', '5', ']', ',', ' ', '[', '0', ',', ' ', '-', '4', '5', ',', ' ', '4', '5', ',', ' ', '-', '2', '6', ']', ']']

我只想要[[0, 45], [0, -45, 45], [0, -45, 45, -26]]这样,类型必须是列表

2 个答案:

答案 0 :(得分:1)

  

我只想这样[[0, 45], [0, -45, 45], [0, -45, 45, -26]]   类型必须是列表

我对你的代码看起来有多复杂感到有点震惊,所以我做的第一件事就是查看输入数据,如下所示:

[
    {
        "id":"1",
        "array1_1":"0",
        "array1_2":"1",
        "array2_1":"2",
        "array2_2":"3",
        "array3_1":"44",
        "array3_2":"55"
    },

    {
        "id":"asd",
        "array1_1":"234",
        "array1_2":"[[0, 26, 60, 45], [0, -45, 45]]",
        "array2_1":"[[23, 345], [543, 321]]",
        "array2_2":"jytj",
        "array3_1":"5",
        "array3_2":"6"
    },

    {
        "id":"bb",
        "array1_1":"bbb",
        "array1_2":"ad",
        "array2_1":"[[23, 345], [543, 321]]",
        "array2_2":"[[0, 45], [45]]",
        "array3_1":"[[34, 45], [66, 88]]",
        "array3_2":"sfw"
    },

    {
        "id":"aa",
        "array1_1":"aa",
        "array1_2":"[[0, 26, 60, 45], [0, -45, 45]]",
        "array2_1":"gfd",
        "array2_2":"[[0, 45], [45]]",
        "array3_1":"sdf",
        "array3_2":"[[26, 60], [0], [56, 34, 45]]"
    },

    {
        "id":"ss",
        "array1_1":"00",
        "array1_2":"[[0, 45], [0, -45, 45], [0, -45, 45, -26]]",
        "array2_1":"[[31, 233], [152, 151], [190, 204], [205, 87], [231, 48], [255, 194]]",
        "array2_2":"[[0, 45], [45], [45], [26, 45, 63], [45], [0, 26, 45, 63]]",
        "array3_1":"[[29, 194], [32, 234], [165, 229], [232, 46], [258, 190]]",
        "array3_2":"[[45], [0, 45], [0, 26, 45], [0, -63, 45, 63], [0, 26, 45]]"
    }
]

以下观察:

  • 您的输入数据虽然是bytes b'',却是string list dictionaries的完美“id:'ss'
  • 在我看来,您需要array1_2eval()

使用decode(),您可以在# no imports needed def get_my_values(data: bytes, key: str, key_v: str, req: str, codec: str = 'utf-8') -> str: ''' data = bytes representing a string representing a list of dictionaries key = key to use to select the element/dictionary of the list key_v = value to match for the key to select the element/dictionary req = required key of the dictionary to return its values codec = codec to use for decode(), defaults to 'utf-8' when omitted ''' l = eval(data.decode(codec)) ## decode and make a list for d in l: ## enumerate through list to get dictionary if d[key] == key_v: ## only if key is the required, matching value return d[req] ## return the values required if __name__ == '__main__': # input data data = b'[{"id":"1","array1_1":"0","array1_2":"1","array2_1":"2","array2_2":"3","array3_1":"44","array3_2":"55"},{"id":"asd","array1_1":"234","array1_2":"[[0, 26, 60, 45], [0, -45, 45]]","array2_1":"[[23, 345], [543, 321]]","array2_2":"jytj","array3_1":"5","array3_2":"6"},{"id":"bb","array1_1":"bbb","array1_2":"ad","array2_1":"[[23, 345], [543, 321]]","array2_2":"[[0, 45], [45]]","array3_1":"[[34, 45], [66, 88]]","array3_2":"sfw"},{"id":"aa","array1_1":"aa","array1_2":"[[0, 26, 60, 45], [0, -45, 45]]","array2_1":"gfd","array2_2":"[[0, 45], [45]]","array3_1":"sdf","array3_2":"[[26, 60], [0], [56, 34, 45]]"},{"id":"ss","array1_1":"00","array1_2":"[[0, 45], [0, -45, 45], [0, -45, 45, -26]]","array2_1":"[[31, 233], [152, 151], [190, 204], [205, 87], [231, 48], [255, 194]]","array2_2":"[[0, 45], [45], [45], [26, 45, 63], [45], [0, 26, 45, 63]]","array3_1":"[[29, 194], [32, 234], [165, 229], [232, 46], [258, 190]]","array3_2":"[[45], [0, 45], [0, 26, 45], [0, -63, 45, 63], [0, 26, 45]]"}]' # selection criteria selection_key = 'id' ## this is the key to use for selection of the list element selection_value = 'ss' ## this is the value of the selection key required_key = 'array1_2' ## this is the key you want values returned # get required values from data result = get_my_values(data, selection_key, selection_value, required_key) ## get values # make a list if possible try: result = eval(result) ## make it a list if possible except NameError: pass ## cannot be made a list, and is a single value # print results print(result) print(type(result))

之后将输入数据(表示列表的字符串)放入列表中

这是一个例子:

[[0, 45], [0, -45, 45], [0, -45, 45, -26]]
<class 'list'>

示例输出:

selectizeInput

答案 1 :(得分:0)

改为使用print(c1)

c1已经是一个列表,因此print(list(c1))会重新列出它。