Create variables dynamically using a repeat statement

Asked 2 years ago, Updated 2 years ago, 136 views

I want to dynamically generate variables using repetitive statements in Python. Please suggest a creative way.

python variable

2022-09-22 21:24

1 Answers

If you don't need to give a variety of variable names, use a dictionary structure that dynamically assigns keys and links values to each key.

a = {}
k = 0
while k < 10:
    <dynamically create key> 
    key = ...
    <calculate value> 
    value = ...
    a[key] = value 
    k += 1

You can also use the 'collections' module to find some data structures to solve your questions :

http://docs.python.org/dev/library/collections.html


2022-09-22 21:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.