How to assign dynamic variables in a for loop in python

EmJ

I have five models for five years as follows.

word2vec_files = ["word2vec_2011", "word2vec_2012", "word2vec_2013", "word2vec_2014", "word2vec_2015"]
years = [2011, 2012, 2013, 2014, 2015]

I want to open each model as model_2011, model_2012, model_2013, model_2014, model_2015.

Currently I am doing it one by one as follows.

model_2011 = word2vec.Word2Vec.load("word2vec_2011")
model_2012 = word2vec.Word2Vec.load("word2vec_2012")
model_2013 = word2vec.Word2Vec.load("word2vec_2013")
model_2014 = word2vec.Word2Vec.load("word2vec_2014")
model_2015 = word2vec.Word2Vec.load("word2vec_2015")

Now I want to imitate the same process using a for loop.

for i, word2vec_file in enumerate(word2vec_files):
    "model_"+str(years[i]) = word2vec.Word2Vec.load(word2vec_file)

However, I get the following error SyntaxError: can't assign to operator. I am wondering how to assign dynamic variables in a for loop in python.

I am happy to provide more details if needed.

OliverRadini

Unfortunately you can't create variable from strings like that; but you can use a dictionary, and add keys/values to it:

years = [2011, 2012, 2013, 2014, 2015]

models = {}

for year in years:
  models[year] = word2vec.Word2Vec.load("word2vec_%s" % year)

print(models)

That way, you can access the year on models to get what you need.

You could do the same thing with a dictionary comprehension:

years = [2011, 2012, 2013, 2014, 2015]

models = {
  year: "word2vec_%s" % year
  for year in years
}

print(models)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jmeter - How to assign multiple dynamic values to a variables inside a loop

python - Create variables in loop then assign

How to correctly assign local variables to a for-loop in python?

How to assign multiple variables in a loop for graphs in Python 3

How to assign variables using dynamic sql

How to assign EventHandler to dynamic object in a loop?

how to assign multiple localstorage variables inside a loop?

How to assign two variables in a single for loop?

How to assign multiple DataFrame variables using a for loop?

How to assign more than 26 variables with FOR Loop?

How to assign different variables in loop in ruby?

How to assign variables on a while loop in Java

How can we assign new variables after each for loop iteration in python?

How to build dynamic variables in python

How to assign dynamic variables to CSS content in Vue.js?

Python How To Assign 2 Variables in One Part(?)

Python - How to Automatically Assign Variables to Numerous Inputs

How to assign and call variables from a class in python

Python: Assign Flexible Variables That Change Based on Value in Loop

Python SyntaxError: cannot assign to operator - Defining variables as sum of strings in for loop

How to assign the result of a for loop to a variable in Python

How to assign different variables to a class object using for loop?

How to assign each variable in a vector of variables new value using loop?

How to create multiple variables in a loop and assign values in VBA

Dynamic Variables in loop

Python: How do I assign items to variables within a loop, where the variable and item names change based on a predetermined list?

Dynamically declare and assign variables in a loop

Loop rename variables in R with assign()

Python: How to pass dynamic variables into XML?