python3.5射程麻烦
python 3.5 range trouble
位置:[['120', '40', 'x1'], ['200', '50', 'x2'], ['150', '15', 'x3'], ['240', '60', 'x4'], ['260', '45', 'x5'], ['225', '15', 'x6'], ['273', '20', 'x7'], ['221', '5', 'x8'], ['345', '20', 'x9' ]]
范围:[['100', '0', '60', '40', 'x1'], ['80', '10', '30', '5', 'x2'], ['120', '0', '25', '10', 'x3'], ['100', '10', '0', '40', 'x4'], ['70', '0', '10', '25', 'x5'], ['75', '10', '5', '10', 'x6' ], ['75', '0', '0', '0', 'x7'], ['0', '0', '0', '0', 'x8'], ['0', '0', '0', '0', 'x9']]
for a in range(int(locations[1][0])+int(rangee[1][0]),int(locations[1][0])-int(rangee[1][1])):
print("x")
我有一个包含 integers.But 的位置列表,当我执行此操作时它不会 work.Any 知道为什么?
调试(_ 这不是调试,那只是打印出来并查看它_)你的数据 - 你的数据是无止境的:
使用
locations = [['120', '40', 'x1'], ['200', '50', 'x2'], ['150', '15', 'x3'], ['240', '60', 'x4'], ['260', '45', 'x5'], ['225', '15', 'x6'], ['273', '20', 'x7'], ['221', '5', 'x8'], ['345', '20', 'x9']]
rangee = [['100', '0', '60', '40', 'x1'], ['80', '10', '30', '5', 'x2'], ['120', '0', '25', '10', 'x3'], ['100', '10', '0', '40', 'x4'], ['70', '0', '10', '25', 'x5'], ['75', '10', '5', '10', 'x6'], ['75', '0', '0', '0', 'x7'], ['0', '0', '0', '0', 'x8'], ['0', '0', '0', '0', 'x9']]
from_loc = locations[1][0]
from_ran = rangee[1][0]
to_loc = locations[1][0]
to_ran = rangee[1][1]
print ( "from " , from_loc, " + " , from_ran, " to: ", to_loc, " - " , to_ran )
输出:
('from ', '200', ' + ', '80', ' to: ', '200', ' - ', '10')
测试:
print ( range(280,190))
输出:
[]
==> 空列表 - 对于下降范围,您需要为步骤添加 ,-1:
print ( range(280,190,-1))
输出:
[280, 279, 278, 277, 276, ... , 192, 191]
这取决于你的用例,你可能想做这样的事情:
one = 280
two = 190
print (range( min(one,two), max(one,two))) # will sort them correctly
输出:[190, ..., 279]
或
print(range(one,two, 1 if one < two else -1))
这将根据其值将正确的步进添加到您的范围。
位置:[['120', '40', 'x1'], ['200', '50', 'x2'], ['150', '15', 'x3'], ['240', '60', 'x4'], ['260', '45', 'x5'], ['225', '15', 'x6'], ['273', '20', 'x7'], ['221', '5', 'x8'], ['345', '20', 'x9' ]]
范围:[['100', '0', '60', '40', 'x1'], ['80', '10', '30', '5', 'x2'], ['120', '0', '25', '10', 'x3'], ['100', '10', '0', '40', 'x4'], ['70', '0', '10', '25', 'x5'], ['75', '10', '5', '10', 'x6' ], ['75', '0', '0', '0', 'x7'], ['0', '0', '0', '0', 'x8'], ['0', '0', '0', '0', 'x9']]
for a in range(int(locations[1][0])+int(rangee[1][0]),int(locations[1][0])-int(rangee[1][1])):
print("x")
我有一个包含 integers.But 的位置列表,当我执行此操作时它不会 work.Any 知道为什么?
调试(_ 这不是调试,那只是打印出来并查看它_)你的数据 - 你的数据是无止境的:
使用
locations = [['120', '40', 'x1'], ['200', '50', 'x2'], ['150', '15', 'x3'], ['240', '60', 'x4'], ['260', '45', 'x5'], ['225', '15', 'x6'], ['273', '20', 'x7'], ['221', '5', 'x8'], ['345', '20', 'x9']]
rangee = [['100', '0', '60', '40', 'x1'], ['80', '10', '30', '5', 'x2'], ['120', '0', '25', '10', 'x3'], ['100', '10', '0', '40', 'x4'], ['70', '0', '10', '25', 'x5'], ['75', '10', '5', '10', 'x6'], ['75', '0', '0', '0', 'x7'], ['0', '0', '0', '0', 'x8'], ['0', '0', '0', '0', 'x9']]
from_loc = locations[1][0]
from_ran = rangee[1][0]
to_loc = locations[1][0]
to_ran = rangee[1][1]
print ( "from " , from_loc, " + " , from_ran, " to: ", to_loc, " - " , to_ran )
输出:
('from ', '200', ' + ', '80', ' to: ', '200', ' - ', '10')
测试:
print ( range(280,190))
输出:
[]
==> 空列表 - 对于下降范围,您需要为步骤添加 ,-1:
print ( range(280,190,-1))
输出:
[280, 279, 278, 277, 276, ... , 192, 191]
这取决于你的用例,你可能想做这样的事情:
one = 280
two = 190
print (range( min(one,two), max(one,two))) # will sort them correctly
输出:[190, ..., 279]
或
print(range(one,two, 1 if one < two else -1))
这将根据其值将正确的步进添加到您的范围。