Python Forum
Get the codemap combinations but got tuple index out of range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Get the codemap combinations but got tuple index out of range
#1
my main purpose is to get the 4 combinations of codemap, and I got an error which is tuple index out of range, the code and error as below:
Code:

 def get_hash_key(self, long_url):

        
        code_map = []
  
        for v in range(0, 10):
            if v == 0:
                continue
            else:
                code_map.append(str(v))
        # not including letter “i, j, l, o”
        for v in range(97, 123):
            if v == 105:
                continue
            elif v == 106:
                continue
            elif v == 108:
                continue
            elif v == 111:
                continue
            else:
                code_map.append(chr(v))
        # not including letter“I, O”
        for v in range(65, 91):
            if v == 73:
                continue
            elif v == 79:
                continue
            else:
                code_map.append(chr(v))

      
        code_map = tuple(code_map)

        md5 = self.get_md5(long_url)
        su = []
        for i in range(0, 4):
            n = int(md5[i * 8:(i + 1) * 8], 16)
            v = [] 
            e = 0
            for j in range(0, 5):
                x = 0x0000003D & n
                e |= ((0x00000002 & n) >> 1) << j
                v.insert(0, code_map[x])
                n = n >> 6
            e |= n << 5
            v.insert(0, code_map[e & 0x0000003D])
            su.append(''.join(v))
        return su

    def get(self, *args, **kwargs):
        self.write(str(self.get_hash_key("http://www.google.com")))
error
Error:
Traceback (most recent call last): File "C:\Users\Meng\PycharmProjects\tornado-shorturl\venv\lib\site-packages\tornado\web.py", line 1697, in _execute result = method(*self.path_args, **self.path_kwargs) File "C:\Users\Meng\PycharmProjects\tornado-shorturl\app\views\views_common.py", line 68, in get self.write(str(self.get_hash_key("http://www.baidu.com"))) File "C:\Users\Meng\PycharmProjects\tornado-shorturl\app\views\views_common.py", line 60, in get_hash_key v.insert(0, code_map[x]) IndexError: tuple index out of range
Reply
#2
code_map is a tuple so its indexes are 0 and 1. But on line 44 you are calling for index x which is 0x0000003D & n. That is not in the range of 0 or 1.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Thumbs Down I hate "List index out of range" Melen 20 3,161 May-14-2023, 06:43 AM
Last Post: deanhystad
  Finding combinations of list of items (30 or so) LynnS 1 838 Jan-25-2023, 02:57 PM
Last Post: deanhystad
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 5,975 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  IndexError: list index out of range dolac 4 1,846 Jul-25-2022, 03:42 PM
Last Post: deanhystad
  I'm getting a String index out of range error debian77 7 2,280 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  IndexError: list index out of range Anldra12 2 1,411 May-03-2022, 01:39 PM
Last Post: Anldra12
  matplotlib x axis range goes over the set range Pedroski55 5 3,111 Nov-21-2021, 08:40 AM
Last Post: paul18fr
  IndexError: list index out of range rf_kartal 6 2,763 Sep-07-2021, 02:36 PM
Last Post: Larz60+
  Python Error List Index Out of Range abhi1vaishnav 3 2,239 Sep-03-2021, 08:40 PM
Last Post: abhi1vaishnav
  How can I find all combinations with a regular expression? AlekseyPython 0 1,637 Jun-23-2021, 04:48 PM
Last Post: AlekseyPython

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020