python问题int/string和hash/array

2024-09-28 22:20:55 发布

您现在位置:Python中文网/ 问答频道 /正文

f = open('transaction.log','r')

ClerkHash = dict()
 arr = [0,0]

for line in f:
    Tdate        = line[0:12] 
    AccountKey   = line[12:50]
    TransType    = line[22:2]
    ClerkKey     = line[24:10]
    CurrencyCode = line[34:2]
    Amount       = line[36:45]
    print line
    print '\n'
    print AccountKey 
    print '\n'
    print Tdate         print '\n'

    if TransType=="04":
        ClerkHash[ClerkKey+AccountKey] = arr; // is this line corrent ? i don't want to corrupt the array every time ? how should i do it ?
        ClerkHash[ClerkKey+AccountKey][0]+=1 
        ClerkHash[ClerkKey+AccountKey][1]+= Amount


for Key in ClerkHash.keys():
     if ClerkHash[key][0] >= 3 and ClerkHash[key][1] > 1000:
        print Key

我想要一个哈希名ClerkHash[ClerkKey+AccountKey] 它由2 int数组组成:第一个索引是drawl num,第二个索引是amount 我定义好数组和散列了吗? 另外,我想把弹药加起来……我怎么能做到呢?在


Tags: keyinforifline数组amountprint
2条回答

检查你的切片间隔!第二个参数是另一个索引,而不是从第一个索引执行的步骤数。我想是吧

TransType    = line[22:2]

应该是的

^{pr2}$

如果设置了

ClerkHash[ClerkKey+AccountKey] = [0, 0]

每次遇到TransType == "04"。所以改变

if TransType=="04":
        ClerkHash[ClerkKey+AccountKey] = arr[0,0]
        ClerkHash[ClerkKey+AccountKey][0]+=1 
        ClerkHash[ClerkKey+AccountKey][1]+= Amount

if TransType=="04":
    if not ClerkHash.has_key(ClerkKey+AccountKey):
        ClerkHash[ClerkKey+AccountKey] = [1, Amount]
    else:
        ClerkHash[ClerkKey+AccountKey][0] += 1 
        ClerkHash[ClerkKey+AccountKey][1] += Amount

到目前为止,我看到的问题很少

Amount       = line[36:45]

应该是

^{pr2}$

以及

ClerkHash[ClerkKey+AccountKey] = arr[0,0]

应该是

ClerkHash[ClerkKey+AccountKey] = [0,0]

相关问题 更多 >