I have a question. I have this array but I don't know how to eliminate single quotation marks. I have this, can you help me with it?
array([['0.3', '0.3', '0.3', '0', '0', '0', '0', '0', '0', '0'],
['0.0', '0.0', '0.0', '0', '0', '0', '0', '0', '0', '0'],
['1.0', '1.0', '1.0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0.7', '0.7', '0.7', '0', '0', '0', '0', '0', '0'],
['0', '0.0', '0.0', '0.0', '0', '0', '0', '0', '0', '0'],
['0', '0.3', '0.3', '0.3', '0', '0', '0', '0', '0', '0'],
['0', '0', '0.7', '0.7', '0.7', '0', '0', '0', '0', '0'],
['0', '0', '0.0', '0.0', '0.0', '0', '0', '0', '0', '0'],
['0', '0', '0.0', '0.0', '0.0', '0', '0', '0', '0', '0'],
['0', '0', '0', '0.3', '0.3', '0.3', '0', '0', '0', '0'],
['0', '0', '0', '0.0', '0.0', '0.0', '0', '0', '0', '0'],
['0', '0', '0', '0.0', '0.0', '0.0', '0', '0', '0', '0'],
['0', '0', '0', '0', '0.3', '0.3', '0.3', '0', '0', '0'],
['0', '0', '0', '0', '0.3', '0.3', '0.3', '0', '0', '0'],
['0', '0', '0', '0', '0.0', '0.0', '0.0', '0', '0', '0'],
['0', '0', '0', '0', '0', '0.0', '0.0', '0.0', '0', '0'],
['0', '0', '0', '0', '0', '0.0', '0.0', '0.0', '0', '0'],
['0', '0', '0', '0', '0', '0.7', '0.7', '0.7', '0', '0'],
['0', '0', '0', '0', '0', '0', '0.3', '0.3', '0.3', '0'],
['0', '0', '0', '0', '0', '0', '0.3', '0.3', '0.3', '0'],
['0', '0', '0', '0', '0', '0', '0.0', '0.0', '0.0', '0'],
['0', '0', '0', '0', '0', '0', '0', '0.3', '0.3', '0.3'],
['0', '0', '0', '0', '0', '0', '0', '0.0', '0.0', '0.0'],
['0', '0', '0', '0', '0', '0', '0', '0.0', '0.0', '0.0']],
dtype=object)
I tried this :
dfff1 = dfff.replace("'", " ") but I got this error:
AttributeError: 'numpy.ndarray' object has no attribute 'replace'
Should I use "for" to gedd rid of it? something like: for mar1 in dfff:
for mar2 in mar1:
mar2.replace("''", "")
the problem with this is that I don't know how to make them an array again ( with the same order)
Can we perform replace on python list?
I have that list that is imported from csv file:
[['1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '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', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0'], ['3', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0']]
fist index(mark as bold) from list above will became costumer id followed by the item that they buy, from list above we can see costumer 1 purchased item in column 12 that marked by italic.
desire out put is :
costumer 1 purchased item 12 and item 22 and so it's for costumer 2 and
3
Note that I have try used panda and not work, and I not sure how to use if statement inside the for loop.
Also, I have used rappid minner and they replaced column by column and they included the [0][0] to be replaced. Is there any other solution beside python?
Here is my code:
import csv
csvfile = open("tran.csv", 'r')
reader = csv.reader(csvfile, delimiter=',')
my_list = list(reader)
a = len(my_list[1])
b = (my_list)
x=0
y=0
for name in my_list:
print ("costummer", my_list[0][0], "buy", my_list[n][g])
update for csv writer:
csvdict = {words[0]:words[1:] for words in csv}
for x in csvdict.keys(): # iterate over the keys '1', '2', ....
products = [index+1 for index,v in enumerate(csvdict[x]) if v == '1' ] # create list of purchases where the '1's are indexed
f = open("trans.csv", 'w')
result = ("costummer", x, "buy", products)
resultstr = (','.join([str(x) for x in hasil]))#I try to convert it to str.
print (resultstr) #to check whether the conversion from list into str is working or not.
f.write(resultstr) #try to write to csv file
Here is a "starting point", there will be an extra index for the customer 1, that is something you can play around with. It might not be important. You can add a if clause to take care of it.
csv = [['1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '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', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0'], ['3', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0', '0', '1', '0', '0', '0', '0', '0', '0', '0']]
csvdict = {words[0]:words[1:] for words in csv}
for x in sorted(csvdict.keys()): # iterate over the keys '1', '2', ....
products = [index+1 for index,v in enumerate(csvdict[x]) if v == '1' ] # create list of purchases where the '1's are indexed
print "costummer", x, "purchased", ' '.join(['"item '+str(i)+'"' for i in B])
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