python - 问题int /字符串和散列/数组

时间:2009-12-29 12:38:53

标签: python string

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的数组:第一个索引是withdrawl num,第二个是ammount 我定义了数组并散列好吗? 另外我想总结一下......我怎么能这样做?

2 个答案:

答案 0 :(得分:2)

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

Amount       = line[36:45]

应该是

Amount       = int(line[36:45])

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

应该是

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

答案 1 :(得分:0)

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

TransType    = line[22:2]

应该是

TransType = line[22:24]

如果设置

,则会覆盖值
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