Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
List 在词汇表中添加2个列表_List_Dictionary_Accounting - Fatal编程技术网

List 在词汇表中添加2个列表

List 在词汇表中添加2个列表,list,dictionary,accounting,List,Dictionary,Accounting,我一直在努力在一个词汇表中添加2的数字。问题是,我需要验证所选行和列中的值是否已经在词典中,如果是这样,我想将复式输入列表添加到词典中已经存在的值(另一个复式输入列表)。我正在使用excel电子表格+xlrd,这样我就可以把它读出来。我对这个很陌生 例如,代码正在检查指定行和列中的帐户(一个数字),假设值为10,然后如果它不在字典中,则将与此计数对应的2个值相加,假设[100,0]作为此键的值。这是工作的预期 现在,最困难的部分是当账号已经出现在字典里的时候。假设这是帐号10的第二个条目。它是[

我一直在努力在一个词汇表中添加2的数字。问题是,我需要验证所选行和列中的值是否已经在词典中,如果是这样,我想将复式输入列表添加到词典中已经存在的值(另一个复式输入列表)。我正在使用excel电子表格+xlrd,这样我就可以把它读出来。我对这个很陌生

例如,代码正在检查指定行和列中的帐户(一个数字),假设值为10,然后如果它不在字典中,则将与此计数对应的2个值相加,假设[100,0]作为此键的值。这是工作的预期

现在,最困难的部分是当账号已经出现在字典里的时候。假设这是帐号10的第二个条目。它是[50,20]。我希望与键“10”关联的值为[150,20]

我尝试过zip方法,但它似乎返回了radomn结果,有时它加起来了,有时它没有

import xlrd
book = xlrd.open_workbook("Entry.xls")
print ("The number of worksheets is", book.nsheets)
print ("Worksheet name(s):", book.sheet_names())
sh = book.sheet_by_index(0)
print (sh.name,"Number of rows", sh.nrows,"Number of cols", sh.ncols)
liste_compte = {}
for rx in range(4, 10):
    if (sh.cell_value(rowx=rx, colx=4)) not in liste_compte:
        liste_compte[((sh.cell_value(rowx=rx, colx=4)))] = [sh.cell_value(rowx=rx, colx=6), sh.cell_value(rowx=rx, colx=7)]
    elif (sh.cell_value(rowx=rx, colx=4)) in liste_compte:
        three = [x + y for x, y in zip(liste_compte[sh.cell_value(rowx=rx, colx=4)],[sh.cell_value(rowx=rx, colx=6), sh.cell_value(rowx=rx, colx=7)])]
        liste_compte[(sh.cell_value(rowx=rx, colx=4))] = three
print (liste_compte)

我不打算直接解开你的代码,只想用一个通用的例子来帮助你:

 def update_balance(existing_balance, new_balance):
     for column in range(len(existing_balance)):
         existing_balance[column] += new_balance[column]


 def update_account(accounts, account_number, new_balance):
     if account_number in accounts:
        update_balance(existing_balance = accounts[account_number], new_balance = new_balance)
     else:
        accounts[account_number] = new_balance
最后,您可以这样做(假设您的xls看起来像
[账号,余额1,余额2]

  accounts = dict()
  for row in xls:
      update_account(accounts       = accounts, 
                     account_number = row[0],
                     new_balance    = row[1:2])

哇,你让它更干净了。谢谢!如果它对你有效,请接受答案,让其他人知道它解决了问题:)(单击答案旁边的复选标记)回答其他人的问题