Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Tạo non-literal Python Tuple

Tạo list trước, sửa giá trị, rồi convert sang tuple.

Ví dụ

def create_non_literal_tuple(a, b, c):
    x = [1] * a
    x[c] = b
    return tuple(x)

create_non_literal_tuple(6, 0, 2)

Kết quả: (1, 1, 0, 1, 1, 1) - list length 6, position 2 = 0.