【问题标题】:python 3.5 range troublepython 3.5范围问题
【发布时间】:2018-05-24 02:53:41
【问题描述】:

位置:[['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")

我有一个带有整数的位置列表。但是当我执行它时它不起作用。知道为什么吗?

【问题讨论】:

  • 什么不起作用? locations的内容是什么,rangee的内容是什么?没有这些信息,我们只能猜测出了什么问题……
  • 欢迎来到 Stack Overflow。请阅读How do I ask a good question?What topics can I ask about here?研究、尝试、添加代码并指出问题。
  • 我编辑了列表
  • locations[1][0] + rangee[1][0] > locations[1][0] - rangee[1][1] 。如果 x > y,range(x,y) 不会返回结果

标签: list range python-3.5


【解决方案1】:

调试(_这不是调试,那只是打印出来并查看它_)你的数据 - 你的数据很神秘:

使用

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

这将根据其值向您的范围添加正确的步进。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 2018-12-06
    • 2016-12-18
    • 2010-10-05
    • 1970-01-01
    相关资源
    最近更新 更多