0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonの配列をjsonデータに加えるだけ

Last updated at Posted at 2021-01-06

pythonの配列をjsonデータに加えるだけです

コード

test.py
#!/env/python
import json

def addJson(list):
    # state data to json
    json = {
        "parameter": list,
    }
    return json

l = list(range(0))

for i in range(25):
    l.append(i)
    data = addJson(l)
    print(data)

実行結果

$ python test.py
{'parameter': [0]}
{'parameter': [0, 1]}
{'parameter': [0, 1, 2]}
{'parameter': [0, 1, 2, 3]}
{'parameter': [0, 1, 2, 3, 4]}
{'parameter': [0, 1, 2, 3, 4, 5]}
{'parameter': [0, 1, 2, 3, 4, 5, 6]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]}
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]}

以上です

追記

最新の値のみ取り出す

test.py
#!/env/python
import json

def addJson(list):
    # state data to json
    json = {
        "parameter": list,
    }
    return json

l = list(range(0))
print(len(l))

for i in range(25):
    l.append(i)
    data = addJson(l)
    print(data)

    data_length = len(l)
    if data_length <= 0:
        Latest_Value = "none"
    else:
        Latest_Value = str(l[data_length - 1])
    print("Latest_Value : " + Latest_Value)

実行結果

$ python test4.py
{'parameter': [0]}
Latest_Value : 0
{'parameter': [0, 1]}
Latest_Value : 1
{'parameter': [0, 1, 2]}
Latest_Value : 2
{'parameter': [0, 1, 2, 3]}
Latest_Value : 3
{'parameter': [0, 1, 2, 3, 4]}
Latest_Value : 4
{'parameter': [0, 1, 2, 3, 4, 5]}
Latest_Value : 5
{'parameter': [0, 1, 2, 3, 4, 5, 6]}
Latest_Value : 6
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7]}
Latest_Value : 7
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8]}
Latest_Value : 8
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]}
Latest_Value : 9
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]}
Latest_Value : 10
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]}
Latest_Value : 11
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]}
Latest_Value : 12
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]}
Latest_Value : 13
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]}
Latest_Value : 14
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]}
Latest_Value : 15
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]}
Latest_Value : 16
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17]}
Latest_Value : 17
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18]}
Latest_Value : 18
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]}
Latest_Value : 19
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]}
Latest_Value : 20
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21]}
Latest_Value : 21
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]}
Latest_Value : 22
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]}
Latest_Value : 23
{'parameter': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24]}
Latest_Value : 24
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?