Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将元组列表排序到字典中时的奇怪计数行为_Python_Loops_Dictionary_Tuples - Fatal编程技术网

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

Python 将元组列表排序到字典中时的奇怪计数行为,python,loops,dictionary,tuples,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,'

我已经研究这个问题好几个小时了,我想不出我的错误。这是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]
此处存在引用重复问题。当您将
条带
分配给许多不同的
基数[items[1]]
时,它们都会得到完全相同的条带字典;增加其中一条会增加所有其他条带字典。对于
计数
,也一样。您应该提供每个条带的副本,而不是原始条带

#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) #

您能简化您的问题吗?请附上示例数据(如5个元素左右)、您的输出和预期输出。
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 #
#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) #