错误:UnboundLocalError: local variable 'xxx' referenced before assignment
来源:互联网 发布:安卓壁纸软件 编辑:程序博客网 时间:2024/05/22 10:52
# -*- coding:utf-8 -*-def addr_to_lenth(jsonlist): t = len(jsonlist) i = 0 typelist = list() while i < t : for j in range(i+1,t): if jsonlist[i]['起始地址'] + jsonlist[i]['长度'] == jsonlist[j]['起始地址'] and j-i-1 < 9: jsonlist[i]['长度'] += jsonlist[j]['长度'] jsonlist[i]['采集数据类型'] = jsonlist[i]['采集数据类型'] + ',' + jsonlist[j]['采集数据类型'] jsonlist[i]['id'] = jsonlist[i]['id'] + ','+ jsonlist[j]['id'] else: break typelist.append(jsonlist[i]) if i + 1 < j: i = j else: i += 1 return typelistdef sort(jsonlist):#冒泡排序,按地址大小重新排序 for i in range(len(jsonlist)-1): for j in range(len(jsonlist)-1-i): if jsonlist[j]['起始地址'] > jsonlist[j+1]['起始地址']: addrlist[j],addrlist[j+1] = addrlist[j+1],addrlist[j] return jsonlistdef add_lenth(jsonlist): for i in range(len(jsonlist)): if jsonlist[i]['采集数据类型'] not in ('int16', 'uint16'): jsonlist[i]['长度'] = 2 else: jsonlist[i]['长度'] = 1 return jsonlistdef tear_down(jsonlist): for item in jsonlist: for i in range(len(item['采集数据类型'])): passdef run(jsonlist): addrlist_sort = sort(jsonlist) addlenth = add_lenth(addrlist_sort) t = addr_to_lenth(addlenth) return tif __name__=="__main__": addrlist = [ {"采集数据类型": "float", "起始地址": 400012, "串口网口": "192.168.0.106:502", "id": "MID10001"}, {"采集数据类型": "float", "起始地址": 400114, "串口网口": "192.168.0.106:502", "id": "MID20002"}, {"采集数据类型": "float", "起始地址": 400014, "串口网口": "192.168.0.106:502", "id": "MID10002"}, {"采集数据类型": "int16", "起始地址": 400016, "串口网口": "192.168.0.106:502", "id": "MID10003"}, {"采集数据类型": "float", "起始地址": 400017, "串口网口": "192.168.0.106:502", "id": "MID10004"}, {"采集数据类型": "int16", "起始地址": 400019, "串口网口": "192.168.0.106:502", "id": "MID10005"}, {"采集数据类型": "float", "起始地址": 400020, "串口网口": "192.168.0.106:502", "id": "MID10006"}, {"采集数据类型": "int16", "起始地址": 400022, "串口网口": "192.168.0.106:502", "id": "MID10006"}, {"采集数据类型": "int16", "起始地址": 400023, "串口网口": "192.168.0.106:502", "id": "MID10006"}, {"采集数据类型": "int16", "起始地址": 400024, "串口网口": "192.168.0.106:502", "id": "MID10006"}, {"采集数据类型": "float", "起始地址": 400025, "串口网口": "192.168.0.106:502", "id": "MID10007"}, {"采集数据类型": "float", "起始地址": 400027, "串口网口": "192.168.0.106:502", "id": "MID10008"}, {"采集数据类型": "float", "起始地址": 400029, "串口网口": "192.168.0.106:502", "id": "MID10009"}, {"采集数据类型": "float", "起始地址": 400031, "串口网口": "192.168.0.106:502", "id": "MID10009"}, {"采集数据类型": "float", "起始地址": 400033, "串口网口": "192.168.0.106:502", "id": "MID10009"}, {"采集数据类型": "float", "起始地址": 400035, "串口网口": "192.168.0.106:502", "id": "MID10009"}, {"采集数据类型": "float", "起始地址": 400037, "串口网口": "192.168.0.106:502", "id": "MID10009"}, {"采集数据类型": "int16", "起始地址": 400039, "串口网口": "192.168.0.106:502", "id": "MID10009"}, {"采集数据类型": "float", "起始地址": 400040, "串口网口": "192.168.0.106:502", "id": "MID10010"} ] t = run(addrlist) print t
程序手动运行该程序是不会报错的,但当其他程序调用时就会出现错误:line15:UnboundLocalError: local variable 'j' referenced before assignment
修改:
def addr_to_lenth(jsonlist): t = len(jsonlist) i = 0 typelist = list() while i < t :s = 0 for j in range(i+1,t): if jsonlist[i]['起始地址'] + jsonlist[i]['长度'] == jsonlist[j]['起始地址'] and j-i-1 < 9: jsonlist[i]['长度'] += jsonlist[j]['长度'] jsonlist[i]['采集数据类型'] = jsonlist[i]['采集数据类型'] + ',' + jsonlist[j]['采集数据类型'] jsonlist[i]['id'] = jsonlist[i]['id'] + ','+ jsonlist[j]['id']s = j else:
s = j break typelist.append(jsonlist[i]) if i + 1 < s: i = s else: i += 1 return typelist
阅读全文
0 0
- UnboundLocalError: local variable 'XXX' referenced before assignment
- 错误:UnboundLocalError: local variable 'xxx' referenced before assignment
- python的UnboundLocalError: local variable 'xxx' referenced before assignment
- python的UnboundLocalError: local variable 'xxx' referenced before assignment
- Python的UnboundLocalError: local variable 'xxx' referenced before assignment
- Python的UnboundLocalError: local variable 'xxx' referenced before assignment
- Python - UnboundLocalError: local variable referenced before assignment
- UnboundLocalError: local variable 'l' referenced before assignment
- UnboundLocalError: local variable 'l' referenced before assignment
- UnboundLocalError: local variable ' ' referenced before assignment
- UnboundLocalError: local variable 'sum1' referenced before assignment
- UnboundLocalError: local variable 'c' referenced before assignment
- UnboundLocalError: local variable 'epoch_logs' referenced before assignment
- local variable 'xxx' referenced before assignment
- local variable 'xxx' referenced before assignment
- local variable 'xxx' referenced before assignment
- local variable 'xxx' referenced before assignment
- local variable 'xxx' referenced before assignment
- JS与JQ图片的预加载与懒加载
- java眼中的C++
- Ubuntu16.04 安装字体教程
- 我们也说说Android.mk(2)-函数进阶教程
- effective python
- 错误:UnboundLocalError: local variable 'xxx' referenced before assignment
- 启动Service的时候报错unable to install breakpoint in
- 欢迎使用CSDN-markdown编辑器
- HTML和XHTML的区别
- 微信分享朋友,朋友圈,qq
- 自学OPENCV+PYTHON+MACOS-X(1)
- 关于ajax跨域问题
- LeetCode||56. Merge Intervals
- Unity 2D塔防