Quantcast

list 求助

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

list 求助

luxmj
This post has NOT been accepted by the mailing list yet.
初学Python 
花了一天的时间考虑如何循环,写出代码总是缺少数值,请帮助,谢谢! 
已知一下两个list,len(Alist)=len(Blist),求Alist[i]中相同的Blist[i]的和: 
Alist=['10A','10A','10A','11A','11A','12A','12A','12B','12B','12B','12B','12B','12B','12B','13C'] 
Blist=[15,25,30,40,50,12,20,35,42,15,15,13,32,14,56] 
如何得到AVlist和BVlist: 
AVlist=['10A','11A','12A','12B','13C'] 
BVlist=[60,90,32,151,56] 
请问如何根据Alist和Blist得到AVlist和BVlist,谢谢!

--
Now found Gmail was so powerful


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: list 求助

katetang
This post has NOT been accepted by the mailing list yet.
Alist=['10A','10A','10A','11A','11A','12A','12A','12B','12B','12B','12B','12B','12B','12B','13C']
Blist=[15,25,30,40,50,12,20,35,42,15,15,13,32,14,56]
SUM = {}
for i in range(len(Alist)):
   item = Alist[i]
   if item in SUM.keys():
      SUM[item] += Blist[i]
   else:
      SUM[item] = Blist[i]
A= [item for item in SUM.keys()]
B= [item for item in SUM.values()]
print A
print B
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: list 求助

leiddx
This post has NOT been accepted by the mailing list yet.
In reply to this post by luxmj
luxmj wrote
初学Python
花了一天的时间考虑如何循环,写出代码总是缺少数值,请帮助,谢谢!
已知一下两个list,len(Alist)=len(Blist),求Alist[i]中相同的Blist[i]的和:
Alist=['10A','10A','10A','11A','11A','12A','12A','12B','12B','12B','12B','12B','12B','12B','13C']
Blist=[15,25,30,40,50,12,20,35,42,15,15,13,32,14,56]
如何得到AVlist和BVlist:
AVlist=['10A','11A','12A','12B','13C']
BVlist=[60,90,32,151,56]
请问如何根据Alist和Blist得到AVlist和BVlist,谢谢!

--
Now found Gmail was so powerful
Alist=['10A','10A','10A','11A','11A','12A','12A','12B','12B','12B','12B','12B','12B','12B','13C']
Blist=[15,25,30,40,50,12,20,35,42,15,15,13,32,14,56]

_Alist = list(set(Alist)) #去重
_Alist.sort() #排序

_Blist = [0] *len(_Alist)

for i in xrange(len(Alist)):
    _Blist[_Alist.index(Alist[i])] += Blist[i]

print _Alist
print _Blist

Loading...