Gặp lỗi khi truy cập một "list of lists of tuples" trong python

Chào mọi người. Em đang học về list và tuples trong python. Em có khởi tạo một cái “list of lists of tuples” ( 1 list, mỗi phần tử trong list là 1 list con gồm các tuples). Tuy nhiên em không thể truy cập được vào các số trong các tuples. Lỗi chi tiết như hình đây:


Khi truy cập pop_size[i][j][0] ( là một số nguyên ) , python lại báo lỗi biến int k thể “subscriptable” đc :no_mouth: Ngồi gg cả tối mà chẳng ra được lỗi ( chắc mình gg kém :grin: ) Mong mọi người giúp em xem đây là lỗi gì và sửa như thế nào ạ :slight_smile: Cảm ơn mọi người :slight_smile:

Bạn up đoạn định nghĩa cái list đó được không ? :smile:

def get_permutation(lst):
    fin_lst = list()
    num_lst = list()
    for i in range(len(lst)):
        a = random.randint(1, 10000) % len(lst)
        while a in num_lst:
            a = random.randint(1, 10000) % len(lst)
        num_lst.append(a)
    for i in range(len(lst)):
        fin_lst.append(lst[num_lst[i]])
    return fin_lst

def initialize(num_cities,size_default):
    cities = list()    
    a = tuple()
    for i in range(num_cities):
        a = (random.randint(1, 1000), random.randint(1, 1000))
        cities.append(a)
    pop_size = list()
    for i in range(size_default):
        pop_size.append(get_permutation(cities))

    return pop_size

Đây bạn ơi :slight_smile:

1 Like

Bạn chạy thử đoạn này xem, cái lỗi đó là do bạn dùng ngoặc vuông ở kiểu int thôi, bạn thử x = 0; x[0] xem :smiley:

import random

def get_permutation(lst):
    fin_lst = list()
    num_lst = list()
    for i in range(len(lst)):
        a = random.randint(1, 10000) % len(lst)
        while a in num_lst:
            a = random.randint(1, 10000) % len(lst)
        num_lst.append(a)
    for i in range(len(lst)):
        fin_lst.append(lst[num_lst[i]])
    return fin_lst

def initialize(num_cities,size_default):
    cities = list()
    a = tuple()
    for i in range(num_cities):
        a = (random.randint(1, 1000), random.randint(1, 1000))
        cities.append(a)
    pop_size = list()
    for i in range(size_default):
        pop_size.append(get_permutation(cities))

    return pop_size

a = initialize(1,2)
print(a)
print(a[0])
print(a[0][0])
print(a[0][0][0])
print(a[0][0][0][0])


>>> x=0
>>> x[0]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'int' object is not subscriptable

mình chạy thử rồi, đến đoạn a[0][0][0] vẫn được, câu lệnh cuối cùng mới có lỗi. Cơ mà trên bài của mình cũng làm 3 số 0 mà :slight_smile:

1 Like

Thế thì bạn kiểm tra lại cấu trúc của của list của bạn có bị thay đổi ở đâu không :smile:
Bạn print cái list đó ra ngay trước dòng lỗi là biết thôi mà, muốn xem kĩ hơn thì import pdb;pdb.set_trace() ngay trước dòng bị lỗi đó, xong chạy bằng terminal là dò ra thôi :smile:

Có vẻ là nó bị lỗi ở chỗ khác thật :smile: Cảm ơn bạn nhé :slight_smile:

1 Like

Quan trọng là đoạn code báo lỗi ấy :grin: nó báo math.hypot…

83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?