2012-11-17 57 views
1

我有两个表像below.i是从数据库的Python:列表添加

EmpID = Assign.objects.select_related().filter(pName=selProject).filter() 
    .order_by('laEmpNum').values_list('laEmpNum', flat=True) 

TotDur = Assign.objects.select_related().filter(pName=selProject).order_by('laEmpNum') 
    .values_list('duration', flat=True) 

EmpID = [u'1046', u'1046', u'1046', u'8008', u'8008', u'8011'] 

TotDur = [0.0, 2.0, 2.5, 0.0, 2.7, 1.2] 

得到这个如果EmpIDs在TotDur相同,则相应的值应该收集并添加(总和)。

ResOne = 0.0 + 2.0 + 2.5 i.e 4.5 
ResTwo = 0.0+2.7   i.e 2.7 
ResThr = 1.2    i.e 1.2 

如何在Python中执行此操作。

回答

2

顺序如果这些元素均使用一个OrderedDict在你在你的例子已经显示顺序,可以使用itertools.groupby

from itertools import groupby 
from operator import itemgetter 
[(k , sum(e for _,e in v)) for k,v in groupby(zip(EmpID, TotDur), itemgetter(0))] 
[(u'1046', 4.5), (u'8008', 2.7), (u'8011', 1.2)] 

INFACT你不需要创建两个单独的列表,后来压缩它

Emp_TotDur = Assign.objects.select_related().filter(pName=selProject).filter() 
    .order_by('laEmpNum').values_list('laEmpNum', 'duration') 

[(k , sum(e for _,e in v)) for k,v in groupby(Emp_TotDur, itemgetter(0))] 
2

您可以使用defaultdict

In [60]: from collections import * 

In [61]: EmpID = [u'1046', u'1046', u'1046', u'8008', u'8008', u'8011'] 

In [62]: TotDur = [0.0, 2.0, 2.5, 0.0, 2.7, 1.2] 

In [63]: d=defaultdict(int) 

In [64]: for x,y in zip(EmpID,TotDur): 
    d[x]+=y 
    ....:  

In [65]: d 
Out[65]: defaultdict(<type 'int'>, {u'8008': 2.7, u'1046': 4.5, u'8011': 1.2}) 

或者干脆dict

In [70]: d=dict() 

In [71]: for x,y in zip(EmpID,TotDur): 
    d[x]=d.get(x,0)+y 
    ....:  

In [72]: d 
Out[72]: {u'1046': 4.5, u'8008': 2.7, u'8011': 1.2} 
3

defaultdict是一个很好的数据结构来使用,为int领域它假定新的密钥值0,并允许方便收集桶桶:

from collections import defaultdict 
d = defaultdict(int) 
for i, j in zip(EmpID, TotDur): 
    d[i] += j 
print d # defaultdict(<type 'int'>, {u'8008': 2.7, u'1046': 4.5, u'8011': 1.2}) 
+0

如何从我的答案有何不同? –

+0

它不是,我没有注意到你的,直到我发布我的;) –

+0

@ AshwiniChaudhary:为什么它需要有所不同?它确实解释了“int”行为,我发现它很有用。 –

0

你能做到这样

l1 = [1,1,2,3,3] 
l2 = [1,2,3,4,5] 
last_val=l1[0] 
sum=0 
list=[] 
for pair in zip(l1,l2): 
    if pair[0]!=last_val: 
     print(sum) 
     list.append(sum) 
     sum=0 
    last_val=pair[0] 
    sum+=pair[1] 
list.append(sum) 
print(list) 
0

可以zip两个列表,其中对第一至第二的元素融入到元组的元素。然后遍历它们,并使用元组的第一部分作为键在词典:

EmpID = [u'1046', u'1046', u'1046', u'8008', u'8008', u'8011'] 
TotDur = [0.0, 2.0, 2.5, 0.0, 2.7, 1.2] 
Result = {} 

for (key, value) in zip(EmpID, TotDur): 
    if not key in Result: 
     Result[key] = value 
    else: 
     Result[key] += value 

# Print the result 
for (key, value) in Result.items(): 
    print key, value 

你可能想,如果你想保留的EMPS

0

试试这个,它将所有对应的id值的总和放在一个列表中,稍后可以用empId访问列表并查看它的总和。

finalList = [] 
lastEmpId 

for index, emp in enumarate(EmdIP): 
    if lastEmpId == emp: 
     finalList[lastEmpId] += TotDur[index] 
    else: 
     finalList[lastEmpId] = TotDur[index] 
    lastEmpId = emp; 

print finalList 
0
python 3.2 
    res=[] 
    for i in set(ID): 
     b=[] 
     for x,y in enumerate(ID): 
       if i==y: b.append(T[x]) 
    res.append(sum(b))