Python,负值不超出列表范围

标签 python list negative-number

class world:
    def __init__(self, screen_size):
        self.map = [[0 for col in range(500)] for row in range(500)]
        self.generate()

    def generate(self):
        for x in range(0, len(self.map[0])):
            for y in range(0, len(self.map)):
                kind = random.randint(0, 100)
                if kind <= 80:
                    self.map[x][y] = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255))
                else:
                    self.map[x][y] = (random.randint(0, 255),random.randint(0, 255),random.randint(0, 255))
        print self.map[50][50], self.map[-50][-50]
printing => (87, 92, 0) (31, 185, 156)

负值怎么可能不超出范围?它应该抛出 IndexError。

最佳答案

使用负数从列表后面开始倒计时,这就是它们仍然有效的原因。

关于Python,负值不超出列表范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19166673/

相关文章:

python - apache prefork/mod_wsgi 产生的进程计数看似过去的配置

Python Force List Index out of Range 异常

python - 迭代列表的有效方法?

javascript - JavaScript 中的负零?

Python:从数字列表中删除负数

python - 检查列表中是否存在值

python - 比较 SQLAlchemy ORM 中两列之间的差异

python - 遍历 pyspark 数据框列

c# - Entity Framework 中的 ICollection<T> 与 List<T>

java - 将正整数转换为负值