-2

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
Tanabata
  • 31
  • 7

1 Answers1

0

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])
Gumboy
  • 467
  • 4
  • 8
  • Muchas gracias, works great and display desire output. and i will try to understand the meaning from each line since i never used dict and keys methon. and find out how to remove first index with if clause like you mention before. once again Muchas gracias sir. – Tanabata Oct 29 '17 at 07:44
  • This just a thought but if you change `{words[0]:words[0:] for words in csv}` to `{words[0]:words[1:] for words in csv}` you get rid of the extra index product but your list is pushed back one. – Gumboy Oct 29 '17 at 07:47
  • yeah sir, as you said you can play with it so i try to change the value like you said in the commend above. here they said it can increase list value like this : >>> mylist = [1,2,3] >>> [x+1 for x in mylist] [2, 3, 4] but it is change the value inside the list not the list address and that might cause an other problem like print ("costummer", x, "buy", B +1) TypeError: can only concatenate list (not "int") to list the simple sollution just remove first column and it will be fine – Tanabata Oct 29 '17 at 07:57
  • thank you very much sir, and know i run out into other problem i have try to look how to print it print value into csv file like they did in [link](https://stackoverflow.com/questions/2084069/create-a-csv-file-with-values-from-a-python-list) and [link](https://stackoverflow.com/questions/6916542/writing-list-of-strings-to-excel-csv-file-in-python) most of their sollutin not work from **bold**'_io.TextIOWrapper' object has no attribute 'writer' and **bold**c.write(r) ValueError: I/O operation on closed file they only printed the first string wich is "costumer" – Tanabata Oct 29 '17 at 08:35
  • We are not here to answer everything for you, although I did. Next time show some form of working code, then we can fix any errors. – Gumboy Oct 29 '17 at 16:12
  • sry sir, i did mean to do that, here some progress, i find out the list cannot written directly into scv writer so that I try to used https://stackoverflow.com/questions/5618878/how-to-convert-list-to-string that i found from error message. the problem with this is, thet print the whole thing but they just print the first and third for. I know there must be wrong with the loop and i still tried it. here is your code that I edited so that i can print it into csv file. i put it right bellow the product list. the question is updated with modified code. – Tanabata Oct 29 '17 at 16:23
  • I answered your other question [link](https://stackoverflow.com/questions/47002129/only-the-first-and-third-element-printed-from-python-list-to-csv-file) – Gumboy Oct 29 '17 at 16:33
  • Remember to upvote, if you found a user's answer helpful. – Gumboy Oct 29 '17 at 16:57
  • done sir but they said "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score." – Tanabata Oct 29 '17 at 17:06