将元组列表排序到字典时的奇怪计数行为

时间:2014-10-03 19:06:36

标签: python loops dictionary tuples

我一直在看这个问题几个小时,我无法弄清楚我的错误。这是Python代码(其中raw是一个元组列表,如[(2.0,'w'),(2.2,'WSW'),(4.5,'N')...]。第一个元组元素是一个浮点代表风速。第二个是风向的弦。

bands = {'<0.5':[],'0.5-2':[],'2-4':[],'4-6':[],'6-8':[],'8-10':[],'10+':[]}
counts = {'<0.5':0,'0.5-2':0,'2-4':0,'4-6':0,'6-8':0,'8-10':0,'10+':0}
cardinals = {}
cardinal_count = {}
for item in raw:
  if not cardinals.has_key(item[1]):#wdir e.g. 'W'
    cardinals[item[1]] = bands
for item in cardinals.keys():
  if not cardinal_count.has_key(item):
    cardinal_count[item] = counts #

for speed,wdir in raw[:10]: # raw is very long - use only first ten for illustration
  target = cardinals[wdir]
  if speed < 0.5:
    cardinal_count[wdir]['<0.5'] += 1
  elif ((speed >= 0.5) and (speed < 2.0)):
    cardinal_count[wdir]['0.5-2'] += 1
  elif ((speed >= 2.0) and (speed < 4.0)):
    cardinal_count[wdir]['2-4'] += 1
  elif ((speed >= 4.0) and (speed < 6.0)):
    cardinal_count[wdir]['4-6'] += 1
  elif ((speed >= 6.0) and (speed < 8.0)):
    cardinal_count[wdir]['6-8'] += 1
  elif ((speed >= 8.0) and (speed < 10.0)):
    cardinal_count[wdir]['8-10'] += 1
  else:
    cardinal_count[wdir]['10+'] += 1
print cardinal_count
print raw[:10]

我原本预计在字典cardinal_count('&lt; 0.5','0.5-2'等)中声明的所有波段中的所有整数的总和将给我不超过'raw'的切片'list(在这种情况下为raw[:10])。正如您从下面的输出中看到的(抱歉格式化),发生了太多增量。即使第一个条目('E')对于'2-4'的频段有7个增量,对'4-6'有3个增量 - 那就是那里的十个。我必须循环太多的东西,但我认为for x,y in [tuple...]语法正是这样做的?

cfarrell@x201:~> python test.py
{'E': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0,'0.5-2': 0},
 'ENE': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0}, 
'WSW': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0}, 
'S': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0}, 
'SSW': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0}, 
'SW': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0}, 
'NE': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0}, 
'ESE': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0}, 
'WNW': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0},
'W': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0},
'SSE': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0},
'SE': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0},
'NW': {'10+': 0, '2-4': 7, '4-6': 3, '<0.5': 0, '8-10': 0, '6-8': 0, '0.5-2': 0}}

[(4.45, 'SSW'), (4.45, 'SSW'), (2.51, 'S'), (2.99, 'SSE'), (3.16, 'SSW'), 
 (3.97, 'S'), (2.39, 'WSW'), (3.03, 'S'), (4.18, 'S'), (2.99, 'S')]

1 个答案:

答案 0 :(得分:0)

for item in raw:
  if not cardinals.has_key(item[1]):#wdir e.g. 'W'
    cardinals[item[1]] = bands
for item in cardinals.keys():
  if not cardinal_count.has_key(item):
    cardinal_count[item] = counts #

您在此处有参考重复问题。当您将bands分配给许多不同的cardinals[items[1]]时,它们都会获得完全相同的乐队字典;递增其中一个将增加所有其他。 counts也是如此。你应该提供每个字典的副本,而不是原始字典。

#at the top of your script:
from copy import deepcopy

#meanwhile, several lines down...
for item in raw:
  if not cardinals.has_key(item[1]):#wdir e.g. 'W'
    cardinals[item[1]] = deepcopy(bands)
for item in cardinals.keys():
  if not cardinal_count.has_key(item):
    cardinal_count[item] = deepcopy(counts) #