how to append items vertically python? - python

I have this list :
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
And I need to also 9 vertically list of items
My code :
vertical = []
x = []
n = 0
for List in List_of_all:
for num in List[n]:
x.append(num)
vertical.append(x)
x = []
n += 1
if n == 9:
break
My out put :
[['3'], ['2'], ['7'], ['0'], ['6'], ['0'], ['2'], ['7'], ['0']]
Why this iteration dont't work true ?!
How should I define this to get

Bold assumption: you are looking for the following transposition:
>>> list(map(list, zip(*List_of_all)))
[['3', '5', '0', '0', '9', '0', '1', '0', '0'],
['0', '2', '8', '0', '0', '5', '3', '0', '0'],
['6', '0', '7', '3', '0', '0', '0', '0', '5'],
['5', '0', '0', '0', '8', '0', '0', '0', '2'],
['0', '0', '0', '1', '6', '9', '0', '0', '0'],
['8', '0', '0', '0', '3', '0', '0', '0', '6'],
['4', '0', '0', '0', '0', '6', '2', '0', '3'],
['0', '0', '3', '8', '0', '0', '5', '7', '0'],
['0', '0', '1', '0', '5', '0', '0', '4', '0']]
The zip(*...) idiom is the simplest way to go about that in pure Python. Without the nested list casting, it will be an iterator over tuples.

Basically, you are taking the diagonal array, you can do:
import numpy as np
vertical = np.diag(List_of_all)
print(vertical)
array(['3', '2', '7', '0', '6', '0', '2', '7', '0'], dtype='<U1')
# to put each element in a list do this
vertical = [[x] for x in np.diag(List_of_all)]
print(vertical)
[['3'], ['2'], ['7'], ['0'], ['6'], ['0'], ['2'], ['7'], ['0']]

List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
vertical=[]
for i in range(9):
print([x[i] for x in List_of_all])#for visual appeal only
vertical.append([x[i] for x in List_of_all])
print()
for line_slicer in range(0,9,3):
count=0
for line in vertical:
count+=1
print(line[line_slicer:line_slicer+3])
if count%3==0:
print()
Output:
['3', '5', '0', '0', '9', '0', '1', '0', '0']
['0', '2', '8', '0', '0', '5', '3', '0', '0']
['6', '0', '7', '3', '0', '0', '0', '0', '5']
['5', '0', '0', '0', '8', '0', '0', '0', '2']
['0', '0', '0', '1', '6', '9', '0', '0', '0']
['8', '0', '0', '0', '3', '0', '0', '0', '6']
['4', '0', '0', '0', '0', '6', '2', '0', '3']
['0', '0', '3', '8', '0', '0', '5', '7', '0']
['0', '0', '1', '0', '5', '0', '0', '4', '0']
['3', '5', '0']
['0', '2', '8']
['6', '0', '7']
['5', '0', '0']
['0', '0', '0']
['8', '0', '0']
['4', '0', '0']
['0', '0', '3']
['0', '0', '1']
['0', '9', '0']
['0', '0', '5']
['3', '0', '0']
['0', '8', '0']
['1', '6', '9']
['0', '3', '0']
['0', '0', '6']
['8', '0', '0']
['0', '5', '0']
['1', '0', '0']
['3', '0', '0']
['0', '0', '5']
['0', '0', '2']
['0', '0', '0']
['0', '0', '6']
['2', '0', '3']
['5', '7', '0']
['0', '4', '0']

If you want a list of the first elements in all of your lists:
vertical=[i[0] for i in List_of_all]
this would be the same as
vertical= ['3','5','0','0','9','0','1','0','0']

You can get the columns as folows:
from collections import defaultdict
List_of_all = [
['3', '0', '6', '5', '0', '8', '4', '0', '0'],
['5', '2', '0', '0', '0', '0', '0', '0', '0'],
['0', '8', '7', '0', '0', '0', '0', '3', '1'],
['0', '0', '3', '0', '1', '0', '0', '8', '0'],
['9', '0', '0', '8', '6', '3', '0', '0', '5'],
['0', '5', '0', '0', '9', '0', '6', '0', '0'],
['1', '3', '0', '0', '0', '0', '2', '5', '0'],
['0', '0', '0', '0', '0', '0', '0', '7', '4'],
['0', '0', '5', '2', '0', '6', '3', '0', '0']
]
if __name__ == '__main__':
columns = defaultdict(list)
for row in List_of_all:
for j, item in enumerate(row):
columns[j].append(item)
print(columns[0]) # ['3', '5', '0', '0', '9', '0', '1', '0', '0']

It is unclear what you expect of output. If you want to get all the columns in the 2D list, one option is to use numpy:
>>> import numpy as np
>>> items = np.array([[1,2,3], [4,5,6], [7,8,9]])
>>> items
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
>>> columns = items.T
>>> columns
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
If you transpose the numpy array, the columns will be rows and vice versa.
Another way to do this is with numpy indexing:
>>> columns = [items[:, i] for i in range(len(items[0]))]
>>> columns
[array([1, 4, 7]), array([2, 5, 8]), array([3, 6, 9])]
>>>
If you simply want to use loops, you should iterate column-wise and not row-wise:
>>> columns = []
>>> for j in range(len(items[0])):
... column = []
... for i in range(len(items)):
... column.append(items[i, j])
... columns.append(column)
...
>>> columns
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
A more pythonic way for the latter solution would be with list comprehensions:
>>> [[items[i][j] for i in range(len(items))] for j in range(len(items[0]))]
[[1, 4, 7], [2, 5, 8], [3, 6, 9]]
That said, the solution provided by #schwobaseggl is a good way to do it too, very pythonic.

One-liner stolen from GeeksforGeeks:
t = [[m[j][i] for j in range(len(m))] for i in range(len(m[0]))]

Related

How to get the height for each column in Python?

This is a nested list:
matrix = [['0', '0', '0', '0', '0', '1', '1', '0', '0', '0'],
['0', '1', '1', '1', '1', '1', '0', '0', '0', '0'],
['0', '0', '1', '1', '0', '1', '0', '1', '1', '0'],
['0', '1', '0', '0', '0', '0', '1', '0', '0', '1'],
['0', '0', '0', '1', '0', '0', '1', '0', '1', '0'],
['0', '0', '0', '0', '1', '0', '1', '1', '1', '0'],
['0', '1', '1', '1', '1', '0', '1', '1', '1', '1'],
['1', '1', '1', '1', '1', '1', '1', '1', '1', '1']]
#output should be [1,2,2,2,3,1,5,3,4,2]
Height = 8
Width = 10
I want to know how I can get the height of each column. And then each height, I want to put it in a list.
We counting 1's and they only count for the height if they are adjoint 1's.
We start with counting below and then go up.
Output should be [1,2,2,2,3,1,5,3,4,1]
I only want to use build in Python functions.
I tried with a for loop and if, else statements.
For loop iterate through the list.
like if i == '1' add 1 to counter.
if i == '0' reset counter and add the last value from counter to counter1, but only if counter is greater then counter1.
You can use itertools.takewhile on the reversed, zipped matrix:
from itertools import takewhile
out = [len(list(takewhile(lambda x: x=='1', reversed(l)))) for l in zip(*matrix)]
output:
[1, 2, 2, 2, 3, 1, 5, 3, 4, 2]
If you don't want to import takewhile, use the recipe:
def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
for x in iterable:
if predicate(x):
yield x
else:
break
How it works:
zip rotates the matrix:
>>> list(zip(*matrix))
[('0', '0', '0', '0', '0', '0', '0', '1'),
('0', '1', '0', '1', '0', '0', '1', '1'),
('0', '1', '1', '0', '0', '0', '1', '1'),
('0', '1', '1', '0', '1', '0', '1', '1'),
('0', '1', '0', '0', '0', '1', '1', '1'),
('1', '1', '1', '0', '0', '0', '0', '1'),
('1', '0', '0', '1', '1', '1', '1', '1'),
('0', '0', '1', '0', '0', '1', '1', '1'),
('0', '0', '1', '0', '1', '1', '1', '1'),
('0', '0', '0', '1', '0', '0', '1', '1')]
The list comprehension with reversed rotates the other way around (actually inverses each row):
>>> [list(reversed(l)) for l in zip(*matrix)]
[['1', '0', '0', '0', '0', '0', '0', '0'],
['1', '1', '0', '0', '1', '0', '1', '0'],
['1', '1', '0', '0', '0', '1', '1', '0'],
['1', '1', '0', '1', '0', '1', '1', '0'],
['1', '1', '1', '0', '0', '0', '1', '0'],
['1', '0', '0', '0', '0', '1', '1', '1'],
['1', '1', '1', '1', '1', '0', '0', '1'],
['1', '1', '1', '0', '0', '1', '0', '0'],
['1', '1', '1', '1', '0', '1', '0', '0'],
['1', '1', '0', '0', '1', '0', '0', '0']]
takewhile keepd the elements while the condition is True, here while the items are '1' (lambda x: x=='1'), and len gets the length of the output:
>>> l = ['1', '1', '1', '0', '0', '0', '1', '0']
>>> list(takewhile(lambda x: x=='1', l))
['1', '1', '1']
>>> len(list(takewhile(lambda x: x=='1', l)))
3
NB. functions like zip, reversed, takewhile are generators, they don't produce output unless something consumes it, that's why I used list(generator(…)) in the exammples
solution with classical python loops:
out = []
for l in zip(*matrix):
counter = 0
for elem in reversed(l):
if elem == '1':
counter +=1
else:
break
out.append(counter)

append key if matches specific index and keyword?

titleValues = {'Movie 1 (1998)': ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], 'Movie 2 (1994)': ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']}
categories={'unknown': '0', 'Action': '1', 'Adventure': '2', 'Animation': '3', "Children's": '4', 'Comedy': '5', 'Crime': '6', 'Documentary': '7', 'Drama': '8', 'Fantasy': '9', 'Film-Noir': '10', 'Horror': '11', 'Musical': '12', 'Mystery': '13', 'Romance': '14', 'Sci-Fi': '15', 'Thriller': '16', 'War': '17', 'Western': '18'}
selectedCol = 1
titles=[]
for key, value in titleValues.items():
for num in value:
if num == '1':
valIdx = value.index(num)
if valIdx == selectedCol:
titles.append(key)
else:
continue
print(titles)
output:
['Movie 1 (1998)', 'Movie 1 (1998)', 'Movie 2 (1994)', 'Movie 2 (1994)', 'Movie 2 (1994)', 'Movie 2 (1994)']
I think it appears 6 times because of the six '1' occurrences. However, how can I only obtain two names as for both lists '1' appears at index 1.
['Movie 1 (1998)', 'Movie 2 (1994)']
only when titleValues contains a one, put key once in a list:
titles = [k for k,v in zip(titleValues.keys(),titleValues.values()) if '1' in v]
result will be
print(titles)
# ['Movie 1 (1998)', 'Movie 2 (1994)']
Explanation
Create two iterables (keys, values)
print(titleValues.keys())
#dict_keys(['Movie 1 (1998)', 'Movie 2 (1994)'])
print(titleValues.values())
#dict_values([['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0'], ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0']])
zip, is a function to iterate element-wise and simultanouesly over two iterables
print(list(zip(titleValues.keys(),titleValues.values())))
# [('Movie 1 (1998)', ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0']), ('Movie 2 (1994)', ['0', '1', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '1', '1', '0', '0'])]
in a for-comprehension you can access elements of both iterables (specified variable name: k for element in first iterable and v for element in second)

Display the list of dates in a proper format

I am running this code to get a date
from datetime import timedelta, date
def daterange(start_date, end_date):
for n in range(int(start_date.day), int((end_date - start_date).days), 90):
yield start_date + timedelta(n)
start_date = date(2016, 1, 1)
end_date = date.today()
for single_date in daterange(start_date, end_date):
x = list(single_date.strftime("%Y-%m-%d"))
print(x)
and its giving me this output
['2', '0', '1', '6', '-', '0', '1', '-', '0', '2']
['2', '0', '1', '6', '-', '0', '4', '-', '0', '1']
['2', '0', '1', '6', '-', '0', '6', '-', '3', '0']
['2', '0', '1', '6', '-', '0', '9', '-', '2', '8']
['2', '0', '1', '6', '-', '1', '2', '-', '2', '7']
['2', '0', '1', '7', '-', '0', '3', '-', '2', '7']
['2', '0', '1', '7', '-', '0', '6', '-', '2', '5']
['2', '0', '1', '7', '-', '0', '9', '-', '2', '3']
['2', '0', '1', '7', '-', '1', '2', '-', '2', '2']
['2', '0', '1', '8', '-', '0', '3', '-', '2', '2']
['2', '0', '1', '8', '-', '0', '6', '-', '2', '0']
['2', '0', '1', '8', '-', '0', '9', '-', '1', '8']
['2', '0', '1', '8', '-', '1', '2', '-', '1', '7']
['2', '0', '1', '9', '-', '0', '3', '-', '1', '7']
['2', '0', '1', '9', '-', '0', '6', '-', '1', '5']
['2', '0', '1', '9', '-', '0', '9', '-', '1', '3']
['2', '0', '1', '9', '-', '1', '2', '-', '1', '2']
Process finished with exit code 0
However, I want the list to display in date in a proper date format, something like this [2019-12-12, 2019-09-13, ...]
My goal is to create a list of dates, which I will be using in Gmail to search for emails. So it needs to be in a format that Gmail understands. My current level of coding/python is at beginners so it might be a very simple solution that I am currently missing. Any help appreciated.
Why did you make a list of the characters? All you need is the formatting you already did correctly:
print(single_date.strftime("%Y-%m-%d"))
you can use a list comprehension:
x = [sd.strftime("%Y-%m-%d") for sd in daterange(start_date, end_date)]
print(x)
output:
['2016-01-02', '2016-04-01', '2016-06-30', '2016-09-28', '2016-12-27', '2017-03-27', '2017-06-25', '2017-09-23', '2017-12-22', '2018-03-22', '2018-06-20', '2018-09-18', '2018-12-17', '2019-03-17', '2019-06-15', '2019-09-13', '2019-12-12']

Having troubles with list

Okey, I'm currently writing Conway's Game of Life and I have reached a point that I don't know how to solve.
Edit here are the different lists I'm having trouble with:
for repeticiones in range(0,5):
nuevo_mapa = mapax
print(mapax)
if '#' in mapax:
print('Jell')
if mapax is mapa_juego:
print('God')
for y in range(0,10):
for x in range(0,10):
if mapa_juego[y][x] == '#':
viva_muerta = True
elif mapa_juego[y][x] == '0':
viva_muerta = False
for i in range(0,8):
try:
if mapa_juego[y + check_listy[i]][x + check_listx[i]] == '#':
sum += 1
except IndexError:
pass
if viva_muerta == True and sum in [0,1]:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum >= 4:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum in [2,3]:
nuevo_mapa[y][x] = '#'
elif viva_muerta == False and sum == 3:
nuevo_mapa[y][x] = '#'
sum = 0
mapa_juego = nuevo_mapa
print('\n\n')
mapax is a list of lists full of 0.
I want to do this:
nuevo_mapa = mapax
nuevo_mapa is modified
mapa_juego = nuevo_mapa
And start over and over again.
But the problem comes here, after the first iteration mapax is no more a full list of 0.
The output is something like this(I want publish it all because it's so messy and to avoid confusions)
>>> [0,0,0,0,0,0,0] First print of mapax
>>> [0,0,#,0,0,0,0] Second print of mapax and I want to avoid this.
If you see anything in the code let me know, thanks.
Edit: here are the different lists I'm having trouble with:
mapa_juego = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','#','0','0','0','0','0','0','0',],
['0','0','0','#','0','0','0','0','0','0',],
['0','#','#','#','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
mapax = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
2 Edit: Here is full code:
mapa_juego = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','#','0','0','0','0','0','0','0',],
['0','0','0','#','0','0','0','0','0','0',],
['0','#','#','#','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
mapax = [
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',],
['0','0','0','0','0','0','0','0','0','0',]
]
viva_muerta = None
check_listy = [0,0,1,-1,-1,1,-1,1]
check_listx = [1,-1,0,0,1,1,-1,-1]
sum = 0
#Reglas
# Una célula muerta con exactamente 3 células vecinas vivas "nace" (es decir, al turno siguiente estará viva).
# Una célula viva con 2 ó 3 células vecinas vivas sigue viva, en otro caso muere o permanece muerta (por "soledad" o "superpoblación").
# Célula viva = True muerta = False
# for line in mapa_juego:
# print(line)
for repeticiones in range(0,5):
nuevo_mapa = mapax
print(mapax)
if '#' in mapax:
print('Jell')
if mapax is mapa_juego:
print('God')
for y in range(0,10):
for x in range(0,10):
if mapa_juego[y][x] == '#':
viva_muerta = True
elif mapa_juego[y][x] == '0':
viva_muerta = False
for i in range(0,8):
try:
if mapa_juego[y + check_listy[i]][x + check_listx[i]] == '#':
sum += 1
except IndexError:
pass
if viva_muerta == True and sum in [0,1]:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum >= 4:
nuevo_mapa[y][x] = '0'
elif viva_muerta == True and sum in [2,3]:
nuevo_mapa[y][x] = '#'
elif viva_muerta == False and sum == 3:
nuevo_mapa[y][x] = '#'
sum = 0
mapa_juego = nuevo_mapa
print('\n\n')
Edit 3: Real output
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '#', '0', '0', '0', '0', '0', '0']
['0', '#', '0', '#', '0', '0', '0', '0', '0', '0']
['0', '0', '#', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']
God

How to set a new variable to another variable's actual value rather then its memory location?

I have a large nested loop called classarray full of information about different classes. I'm trying to combine the classes that are the same class, given that the data in the first 2 nest loop indexes are the same.
[['AAS', '100', 'Intro Asian American Studies', '0', '12', '8', '5', '1', '3', '0', '0', '0', '0', '0', '0', '0', 'S-15']
['AAS', '100', 'Intro Asian American Studies', '1', '10', '4', '3', '6', '0', '1', '2', '0', '0', '0', '0', '0', 'S-15']
['AAS', '100', 'Intro Asian American Studies', '1', '7', '6', '7', '4', '1', '0', '1', '0', '0', '0', '0', '0', 'S-15']
['AAS', '120', 'Intro to Asian Am Pop Culture', '6', '7', '5', '2', '0', '3', '3', '0', '1', '0', '0', '0', '1', 'S-15']
['AAS', '215', 'US Citizenship Comparatively', '1', '3', '5', '4', '1', '6', '1', '3', '2', '1', '1', '1', '0', 'F-15']
['AAS', '258', 'Muslims in America', '0', '19', '2', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', 'S-16']
['AAS', '365', 'Asian American Media and Film', '1', '4', '6', '4', '5', '1', '3', '2', '2', '0', '0', '1', '0', 'S-16']
['AAS', '365', 'Asian American Media and Film', '6', '15', '0', '0', '1', '0', '3', '1', '1', '0', '0', '0', '2', 'S-15']]
I've written this code below to try to combine the rows and produce a new array called new_array:
itemx = ['','','',0,0,0,0,0,0,0,0,0,0,0,0,0,'']
newarray=[]
for course,courseplus1 in zip(classarray,classarray[1:]):
if (course[0]==courseplus1[0] and course[1]==courseplus1[1]):
itemx[0]=course[0]
itemx[1]=course[1]
itemx[2]=course[2]
for number in range(3,16):
itemx[number]=itemx[number]+(int(course[number])+int(courseplus1[number]))
itemx[16]=course[16]
else:
newarray.append(itemx)
print(itemx)
itemx[0]=courseplus1[0]
itemx[1]=courseplus1[1]
itemx[2]=courseplus1[2]
for number in range(3,16):
itemx[number]=int(courseplus1[number])
for item in newarray:
print(item)
However, the output is this:
['AAS', '365', 'Asian American Media and Film', '7', '19', '6', '4', '6', '1', '6', '3', '3', '0', '0', '1', '2', 'S-16']
5 times.
From what I understood by looking through stack overflow, the reason is because:
newarray.append(itemx)
appends itemx to the list; itemx is one singular memory location that, at the end, has AAS 365's information in it. So, new array, as a list of itemx's, has a bunch of itemx's in it.
My question is: how do I deal with or mitigate this problem? To create classarray, I was doing the same thing, except i was declaring the itemx inside of the for loop, which i understand to mean itemx is a new variable with a new location.
>>> from copy import copy, deepcopy
help(copy)
help(deepcopy)
>>> a = [1]
>>> b = copy(a)
>>> b.append(1)
>>> b
[1, 1]
>>> a
[1]
After consulting with some experienced friends, I was able to solve my problem. All I need to do is "redeclare" itemX in my else statement after appending itemx to newarray. This points it to a new location in memory (I think)!
else:
newarray.append(itemx)
itemx = ['', '', '', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '']
itemx[0]=courseplus1[0]
itemx[1]=courseplus1[1]
itemx[2]=courseplus1[2]
for number in range(3,16):
itemx[number]=int(courseplus1[number])

Categories

Resources