To understand what I'm trying to do, first look at the code I've included below:
a = []
b = []
c = []
x = [a, b, c]
x[0] = [5, 10, 15]
x[1] = [30, 60, 90]
x[2] = [100, 200, 300]
Now, what I wish I could do is find a technique such that those last three lines would update the elements I had initially stored in x -- In other words, I wish that assigning a value to x[0] would directly update a, and assigning a value to x[1] would update b, and assigning a value to x[2] would update c. If this was the case, we'd be able to see the following outputs:
print(a)
>> [5, 10, 15]
print(b)
>> [30, 60, 90]
print(c)
>> [100, 200, 300]
I know this won't happen because assigning a value to x[i] just replaces the ith element in x, rather than "updating" the value of the ith element. And frankly, that makes much more sense and is obviously the way it should be. But, is there an alternate way to "update" variables in a list/array like this?
Here's a more detailed example of why I'm looking to do this:
dataset = 'directory\data.csv'
writer = []
director = []
runtime = []
language = []
country = []
genre = []
budget = []
year = []
features = [writer, director, runtime, language, country, genre, budget, year]
cnt = 0
for cnt in range(0,len(features)):
with open(dataset,'r',encoding="latin1") as f:
reader = csv.reader(f,delimiter=",",quotechar='"')
for row in reader:
features[cnt].append(row[cnt])
features[cnt] = np.array(features[cnt]) # <<- trying to convert each element from list to numpy array
In other words, I'm trying to read data from a .csv file and copy each column to one of several different lists, which I initialized between dataset and features. Then, I need to convert these lists to numpy arrays so that I can use the np.where() function, since .index() won't do what I need for this task.
Ideally, after the most recent snippet of code, I'd be able to see all the elements in features had been updated. For example, the following outputs would ideally be what we see after running the above code:
print(country)
>> ['USA', 'USA', 'UK', 'Germany', 'USA', 'Italy', ... , 'Germany']
print(language)
>> ['English', 'English', 'English', 'German', 'English', 'Italian', ... ,'German']
# etc. etc.
Problem is, in my actual code, I have many more lists (ie. many more .csv columns) I need to copy, so that's why I put them all in one features list/array. I was hoping to be able to iteratively copy a different column into each list, and then convert each list to a numpy array, all in one compact loop. This would eliminate the need for a separate loop for each column/list I want to copy. But unfortunately, I can't figure out how to "update" each features element rather than write something entirely different in its place.
Is there a way to accomplish this? Apologies for poor syntax/efficiency anywhere, I'm not very experienced with Python.