<bdo id='4kNZR'></bdo><ul id='4kNZR'></ul>

    <small id='4kNZR'></small><noframes id='4kNZR'>

      <tfoot id='4kNZR'></tfoot>
      <i id='4kNZR'><tr id='4kNZR'><dt id='4kNZR'><q id='4kNZR'><span id='4kNZR'><b id='4kNZR'><form id='4kNZR'><ins id='4kNZR'></ins><ul id='4kNZR'></ul><sub id='4kNZR'></sub></form><legend id='4kNZR'></legend><bdo id='4kNZR'><pre id='4kNZR'><center id='4kNZR'></center></pre></bdo></b><th id='4kNZR'></th></span></q></dt></tr></i><div id='4kNZR'><tfoot id='4kNZR'></tfoot><dl id='4kNZR'><fieldset id='4kNZR'></fieldset></dl></div>
    1. <legend id='4kNZR'><style id='4kNZR'><dir id='4kNZR'><q id='4kNZR'></q></dir></style></legend>
    2. `xrange(2**100)` -&gt;溢出错误:long int 太大而无法转换为 int

      `xrange(2**100)` -gt; OverflowError: long int too large to convert to int(`xrange(2**100)` -gt;溢出错误:long int 太大而无法转换为 int)
      <tfoot id='01qct'></tfoot>

        <legend id='01qct'><style id='01qct'><dir id='01qct'><q id='01qct'></q></dir></style></legend>

          <i id='01qct'><tr id='01qct'><dt id='01qct'><q id='01qct'><span id='01qct'><b id='01qct'><form id='01qct'><ins id='01qct'></ins><ul id='01qct'></ul><sub id='01qct'></sub></form><legend id='01qct'></legend><bdo id='01qct'><pre id='01qct'><center id='01qct'></center></pre></bdo></b><th id='01qct'></th></span></q></dt></tr></i><div id='01qct'><tfoot id='01qct'></tfoot><dl id='01qct'><fieldset id='01qct'></fieldset></dl></div>
            <tbody id='01qct'></tbody>
            <bdo id='01qct'></bdo><ul id='01qct'></ul>

            <small id='01qct'></small><noframes id='01qct'>

              1. 本文介绍了`xrange(2**100)` -&gt;溢出错误:long int 太大而无法转换为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                xrange 函数不适用于大整数:

                xrange function doesn't work for large integers:

                >>> N = 10**100
                >>> xrange(N)
                Traceback (most recent call last):
                ...
                OverflowError: long int too large to convert to int
                >>> xrange(N, N+10)
                Traceback (most recent call last):
                ...
                OverflowError: long int too large to convert to int
                

                Python 3.x:

                Python 3.x:

                >>> N = 10**100
                >>> r = range(N)
                >>> r = range(N, N+10)
                >>> len(r)
                10
                

                是否有用于 Python 2.x 的 py3k 内置 range() 函数的反向移植?

                Is there a backport of py3k builtin range() function for Python 2.x?

                我正在寻找惰性"range() 的完整实现,而不仅仅是其某些功能的部分实现.

                I'm looking for a complete implementation of "lazy" range(), not just a partial implementation of some of its functionality.

                推荐答案

                好的,下面是更全面的重新实现.

                Okay, here's a go at a fuller reimplementation.

                class MyXRange(object):
                    def __init__(self, a1, a2=None, step=1):
                        if step == 0:
                            raise ValueError("arg 3 must not be 0")
                        if a2 is None:
                            a1, a2 = 0, a1
                        if (a2 - a1) % step != 0:
                            a2 += step - (a2 - a1) % step
                        if cmp(a1, a2) != cmp(0, step):
                            a2 = a1
                        self.start, self.stop, self.step = a1, a2, step
                
                    def __iter__(self):
                        n = self.start
                        while cmp(n, self.stop) == cmp(0, self.step):
                            yield n
                            n += self.step
                
                    def __repr__(self):
                        return "MyXRange(%d,%d,%d)" % (self.start, self.stop, self.step)
                
                    # NB: len(self) will convert this to an int, and may fail
                    def __len__(self):
                        return (self.stop - self.start)//(self.step)
                
                    def __getitem__(self, key):
                        if key < 0:
                            key = self.__len__() + key
                            if key < 0:
                                raise IndexError("list index out of range")
                            return self[key]
                        n = self.start + self.step*key
                        if cmp(n, self.stop) != cmp(0, self.step):
                            raise IndexError("list index out of range")
                        return n
                
                    def __reversed__(self):
                        return MyXRange(self.stop-self.step, self.start-self.step, -self.step)
                
                    def __contains__(self, val):
                        if val == self.start: return cmp(0, self.step) == cmp(self.start, self.stop)
                        if cmp(self.start, val) != cmp(0, self.step): return False
                        if cmp(val, self.stop) != cmp(0, self.step): return False
                        return (val - self.start) % self.step == 0
                

                还有一些测试:

                def testMyXRange(testsize=10):
                    def normexcept(f,args):
                        try:
                            r = [f(args)]
                        except Exception, e:
                            r = type(e)
                        return r
                
                    for i in range(-testsize,testsize+1):
                        for j in range(-testsize,testsize+1):
                            print i, j
                            for k in range(-9, 10, 2):
                                r, mr = range(i,j,k), MyXRange(i,j,k)
                
                                if r != list(mr):
                                    print "iter fail: %d, %d, %d" % (i,j,k)
                
                                if list(reversed(r)) != list(reversed(mr)):
                                    print "reversed fail: %d, %d, %d" % (i,j,k)
                
                                if len(r) != len(mr):
                                    print "len fail: %d, %d, %d" % (i,j,k)
                
                                z = [m for m in range(-testsize*2,testsize*2+1)
                                      if (m in r) != (m in mr)]
                                if z != []:
                                    print "contains fail: %d, %d, %d, %s" % (i,j,k,(z+["..."])[:10])
                
                                z = [m for m in range(-testsize*2, testsize*2+1) 
                                      if normexcept(r.__getitem__, m) != normexcept(mr.__getitem__, m)]
                                if z != []:
                                    print "getitem fail: %d, %d, %d, %s" % (i,j,k,(z+["..."])[:10])
                

                这篇关于`xrange(2**100)` -&gt;溢出错误:long int 太大而无法转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                What happens when you compare 2 pandas Series(当你比较 2 个 pandas 系列时会发生什么)
                Quickly find differences between two large text files(快速查找两个大文本文件之间的差异)
                Python - Compare 2 files and output differences(Python - 比较 2 个文件和输出差异)
                Why do comparisions between very large float values fail in python?(为什么在 python 中非常大的浮点值之间的比较会失败?)
                Dictionary merge by updating but not overwriting if value exists(字典通过更新合并,但如果值存在则不覆盖)
                Find entries of one text file in another file in python(在python中的另一个文件中查找一个文本文件的条目)
                    <tbody id='kEe6c'></tbody>
                    • <bdo id='kEe6c'></bdo><ul id='kEe6c'></ul>
                    • <tfoot id='kEe6c'></tfoot>
                      <legend id='kEe6c'><style id='kEe6c'><dir id='kEe6c'><q id='kEe6c'></q></dir></style></legend>

                          <small id='kEe6c'></small><noframes id='kEe6c'>

                        1. <i id='kEe6c'><tr id='kEe6c'><dt id='kEe6c'><q id='kEe6c'><span id='kEe6c'><b id='kEe6c'><form id='kEe6c'><ins id='kEe6c'></ins><ul id='kEe6c'></ul><sub id='kEe6c'></sub></form><legend id='kEe6c'></legend><bdo id='kEe6c'><pre id='kEe6c'><center id='kEe6c'></center></pre></bdo></b><th id='kEe6c'></th></span></q></dt></tr></i><div id='kEe6c'><tfoot id='kEe6c'></tfoot><dl id='kEe6c'><fieldset id='kEe6c'></fieldset></dl></div>