How to add int numbers to json data? [duplicate] - python

This question already has answers here:
What does enumerate() mean?
(7 answers)
Closed 2 years ago.
I have a small script that create JSON. I want to add field track_ID and this field wll be int. Idea is to add some loop in which it starts from 1 and finish when objects gone.
Any ideas?
for list in obj['frames']['objects']:
data = {
'track_ID':
'object_id': obj['info']['doc']
}

Just add an i in it, something like this
i = 0
for list in obj['frames']['objects']:
data = {
'track_ID': i
'object_id': obj['info']['doc']
}
i = i + 1

enumerate() will do that beautifully

You can use enumerate.
Note that your loop overrides the value of data in every iteration, list is a saved word in python and you don't actually use the value of list in your code example.
for idx, list in enumerate(obj['frames']['objects']):
data = {
'track_ID': idx+1
'object_id': obj['info']['doc']
}

Related

Using a for loop to combine lists with similar names [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 16 days ago.
I have some lists with common names and I'd like to use a for loop to concatenate them.
Bitcoin_2014 = [457.00, 639.80, 386.94, 320.19]
Bitcoin_2015 = [244.22, 263.07, 236.06, 430.57]
Bitcoin_2016 = [416.73, 673.34, 609.74, 963.74]
I'd like the for loop to be able to print all lists (since the only thing that changes are the last two digits) with the logic of the following but I know the syntax isn't correct.
for i in range(14, 17):
print(Bitcoin_20,i)
Are you able to do this in Python?
You can put all the lists in a tuple and loop over that.
for b in (Bitcoin_2014, Bitcoin_2015, Bitcoin_2016):
print(b)
Consider using a dict instead if you have many such lists.
bitcoin = {
2014 : [457.00, 639.80, 386.94, 320.19],
2015 : [244.22, 263.07, 236.06, 430.57],
2016 : [416.73, 673.34, 609.74, 963.74]
}
for year, val in bitcoin.items():
print(year, val)

Dictionary where values are dictionaries not updating [duplicate]

This question already has answers here:
How to copy a dictionary and only edit the copy
(23 answers)
Closed 1 year ago.
I want to create a dictionary where each value is a dictionary. Here's what I have:
start = 1
for i in range(3):
updated_payload = payload
updated_payload["params"]["dsoJsonData"]["ReadMap"]["Some"]["StartIndex"] = start
x[i] = updated_payload
start = start + 1
Where payload is also a dictionary with all the needed attributes. I am just changing the StartIndex attribute to whatever the loop is at that time. However, when I run this piece of code all my dictionaries' keys have the same exact values. StartIndex all equal to 3. Why are they all getting the same value and how can I fix it so that it gets its respective iteration number?
This is because you're referencing those dicts rather than copying them. So all of your x[i]'s are pointing the same dict. You should be copying them as the following:
x[i] = updated_payload.copy()
I think you need to make a copy of the payload dictionary. You are directly assigning the value of payload to updated_payload so it's passed by reference instead of being copied.
updated_payload = payload.copy()

Find the biggest value in a given list [duplicate]

This question already has answers here:
How to find the maximum number in a list using a loop?
(11 answers)
Closed 2 years ago.
by using only a temporary variable, a for loop, and an if to compare the values-hackinscience.org
Find the biggest value in a given list.
the_list = [
143266561,
1738152473,
312377936,
1027708881,
1495785517,
1858250798,
1693786723,
1871655963,
374455497,
430158267,
]
max_in = 0
for val in the_list:
if val > max_in:
max_in = val
There is the for, there is the if, max_in is somehow a temp var cause it changes over the loop. You get it.
No need for either of that. Use max(the_list).

How do I make different variables in for loops In Python [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 3 years ago.
I am making a program that has a for loop and every time the loop is ran I want it to make a new variable like
for item in range(0, size)
(Make new variable bit1, bit2, bit3, bit4, etc with value of 0)
Is this possible?
Create a List of variables and append to it like this:
bits = []
for item in range(0, size)
bits.append(0)
# now you have bits[0], bits[1], bits[2], etc, all set to 0
We can do this by appending to the vars() dictionary in the program.
for index, item in enumerate(range(size)):
vars()[f'bit{index+1}'] = 0
Try calling the names now:
>>> bit1
0
And while this works, I'd recommend using a list or a dict instead.

Making (theoreticly) infinite dictionaries in python? [duplicate]

This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 8 years ago.
I recently looked up this topic but dint find a satisfying answer. Everybody said using dictionaries is the best way , but I don’t know how to apply that in this case:
I want a function that generates me a list of objects generated by a class.
So in pseudo-code something like :
for a in range(100):
tmp = "object" + str(a)
var(tmp) = someclass()
list add tmp
Edit , because of being marked as an duplicate.
I don’t want an answer how dictionary work , I know that. I want to know , how I can make entries which consists of an entry-name and the generated objects for that name.
I need a dictionary which looks something like:
{"1" : obj1 , "2" : obj2, "3" : obj3 ... }
Edit two:
I ended up using a list instead of a dictionary which is massively easier:
for tmp in range(100):
tmpobj = objclass()
list.append(tmpobj)
thanks for help ;)
You can use a dictionary comprehension :
{ var("object" + str(a)) : someclass() for a in range(100) }

Categories