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)
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']
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
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])