【问题标题】:list position replacement with if statement用 if 语句替换列表位置
【发布时间】:2018-04-10 06:08:24
【问题描述】:

我们可以对 python 列表执行替换吗?

我有从 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']]

上面列表中的第一个索引(标记为粗体)将成为客户 ID,后跟他们购买的商品,从上面的列表中,我们可以在第 12 列中看到客户 1 购买的商品,以斜体标记。

希望输出的是:

顾客 1 购买了商品 12 和商品 22,所以它是为顾客 2 和 3

请注意,我已经尝试使用 panda 并且无法正常工作,并且我不确定如何在 for 循环中使用 if 语句。

另外,我使用了 rappid minner,它们逐列替换,它们包括要替换的 [0][0]。除了python还有其他解决方案吗?

这是我的代码:

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

csv 写入器更新:

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

【问题讨论】:

  • 我很难理解你的英语。也许您可以编辑您的帖子以包含示例所需的输出?
  • @JohnGordon 编辑先生,希望比以前更好。

标签: python list pentaho data-cleaning


【解决方案1】:

这里是一个“起点”,客户1会有一个额外的索引,这是你可以玩弄的东西。这可能并不重要。您可以添加一个 if 子句来处理它。

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

【讨论】:

  • Muchas gracias,效果很好,显示欲望输出。我将尝试理解每一行的含义,因为我从未使用过 dict 和 keys 方法。并了解如何使用您之前提到的 if 子句删除第一个索引。再次Muchas gracias先生。
  • 这只是一个想法,但如果您将{words[0]:words[0:] for words in csv} 更改为{words[0]:words[1:] for words in csv},您将摆脱额外的索引产品,但您的列表会被推回。
  • 是的,先生,正如您所说,您可以使用它,所以我尝试像您在上面的推荐中所说的那样更改值。在这里他们说它可以像这样增加列表值: >>> mylist = [1,2,3] >>> [x+1 for x in mylist] [2, 3, 4] 但它​​会改变里面的值列出不是列表地址,这可能会导致其他问题,例如 print ("costummer", x, "buy", B +1) TypeError: can only concatenate list (not "int") to list the simple sollution just remove first column会好的
  • 非常感谢您,先生,并且知道我遇到了其他问题,我尝试查看如何将其打印值打印到 csv 文件中,就像他们在 linklink 中所做的那样sollutin 不适用于 bold'_io.TextIOWrapper' 对象没有属性 'writer' 和 boldc.write(r) ValueError: I/O operation on closed file they only打印第一个字符串是“costumer”
  • 我们不是在这里为您解答所有问题,尽管我回答了。下次显示某种形式的工作代码,然后我们可以修复任何错误。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-29
  • 2014-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多