Hướng dẫn how do you add elements to a list in recursion in python? - làm cách nào để bạn thêm các phần tử vào danh sách đệ quy trong python?

Tôi muốn nối vào một danh sách đệ quy nhưng tôi không thể đưa ra một chức năng hoạt động. Hàm có hai đối số times

def replicate_recur[times, data, result=None]:
    if result is None:  # create a new result if no intermediate was given
        result = []
    if times == 1:
        result.append[data]
    else:
        result.append[data]
        replicate_recur[times - 1, data, result]  # also pass in the "result"
    return result
0. times nên là số lần để nối dữ liệu.

Đây là mã của tôi cho đến nay:

def replicate_recur[times, data]:
    result2 = []
    if times == 0:
        result2.append[data]
    else:
        result2.append[data]
        replicate_recur[times - 1, data]
    return result2

MSEifert

139K33 Huy hiệu vàng325 Huy hiệu bạc340 Huy hiệu đồng33 gold badges325 silver badges340 bronze badges

Đã hỏi ngày 22 tháng 2 năm 2017 lúc 9:54Feb 22, 2017 at 9:54

4

Bạn có thể sử dụng một danh sách trung gian để nối vào mỗi cuộc gọi đệ quy. Điều đó tránh được những vấn đề xác định lại mà bạn đang gặp phải hiện tại:

def replicate_recur[times, data, result=None]:
    if result is None:  # create a new result if no intermediate was given
        result = []
    if times == 1:
        result.append[data]
    else:
        result.append[data]
        replicate_recur[times - 1, data, result]  # also pass in the "result"
    return result

Khi được gọi:

>>> replicate_recur[4, 2]
[2, 2, 2, 2]

Đã trả lời ngày 22 tháng 2 năm 2017 lúc 10:06Feb 22, 2017 at 10:06

MSEifertMseifertMSeifert

139K33 Huy hiệu vàng325 Huy hiệu bạc340 Huy hiệu đồng33 gold badges325 silver badges340 bronze badges

Đã hỏi ngày 22 tháng 2 năm 2017 lúc 9:54

def replicate_recur[times, data]:
    result2 = []
    if times == 1:
        result2.append[data]
    else:
        result2.append[data]
        result2.extend[replicate_recur[times - 1, data]]
    return result2

Bạn có thể sử dụng một danh sách trung gian để nối vào mỗi cuộc gọi đệ quy. Điều đó tránh được những vấn đề xác định lại mà bạn đang gặp phải hiện tại:

def replicate[times, data]:
    return [data]*times

Khi được gọi:

Đã trả lời ngày 22 tháng 2 năm 2017 lúc 10:061 gold badge14 silver badges25 bronze badges

MSEifertMseifertFeb 22, 2017 at 10:00

Để làm cho mã của bạn hoạt động, bạn cần

def replicate_recur[times, data, result=None]:
    if result is None:  # create a new result if no intermediate was given
        result = []
    if times == 1:
        result.append[data]
    else:
        result.append[data]
        replicate_recur[times - 1, data, result]  # also pass in the "result"
    return result
2 danh sách trong quá trình thực thi hiện tại với đầu ra của cuộc gọi đệ quy tiếp theo. Ngoài ra, độ sâu thấp nhất của đệ quy phải được xác định bởi
def replicate_recur[times, data, result=None]:
    if result is None:  # create a new result if no intermediate was given
        result = []
    if times == 1:
        result.append[data]
    else:
        result.append[data]
        replicate_recur[times - 1, data, result]  # also pass in the "result"
    return result
3:Moses Koledoye

Trên một lưu ý khác, bạn có thể chỉ cần sao chép danh sách của mình bằng:8 gold badges126 silver badges133 bronze badges

2

Adirio

def replicate[times, data]:
    result2 = []
    for i in xrange[times]:
        result2.append[data]
    return result2

4,8901 Huy hiệu vàng14 Huy hiệu bạc25 Huy hiệu đồng

def replicate_recur[times, data, listTest=None]:
    # If a list has not been passed as argument create an empty one
    if[listTest == None]:
        listTest = []
    # Return the list if we need to replicate 0 more times
    if times == 0:
        return listTest
    # If we reach here at least we have to replicate once
    listTest.append[data]
    # Recursive call to replicate more times, if needed and return the result
    replicate_recur[times-1, data, listTest]
    return listTest

Khi được gọi:

Đã trả lời ngày 22 tháng 2 năm 2017 lúc 10:061 gold badge14 silver badges25 bronze badges

MSEifertMseifertFeb 22, 2017 at 9:58

3

Để làm cho mã của bạn hoạt động, bạn cần

def replicate_recur[times, data, result=None]:
    if result is None:  # create a new result if no intermediate was given
        result = []
    if times == 1:
        result.append[data]
    else:
        result.append[data]
        replicate_recur[times - 1, data, result]  # also pass in the "result"
    return result
2 danh sách trong quá trình thực thi hiện tại với đầu ra của cuộc gọi đệ quy tiếp theo. Ngoài ra, độ sâu thấp nhất của đệ quy phải được xác định bởi
def replicate_recur[times, data, result=None]:
    if result is None:  # create a new result if no intermediate was given
        result = []
    if times == 1:
        result.append[data]
    else:
        result.append[data]
        replicate_recur[times - 1, data, result]  # also pass in the "result"
    return result
3:

Trên một lưu ý khác, bạn có thể chỉ cần sao chép danh sách của mình bằng:

[result2.append[data]]*times

MSEifertMseifertFeb 22, 2017 at 9:58

Để làm cho mã của bạn hoạt động, bạn cần

def replicate_recur[times, data, result=None]:
    if result is None:  # create a new result if no intermediate was given
        result = []
    if times == 1:
        result.append[data]
    else:
        result.append[data]
        replicate_recur[times - 1, data, result]  # also pass in the "result"
    return result
2 danh sách trong quá trình thực thi hiện tại với đầu ra của cuộc gọi đệ quy tiếp theo. Ngoài ra, độ sâu thấp nhất của đệ quy phải được xác định bởi
def replicate_recur[times, data, result=None]:
    if result is None:  # create a new result if no intermediate was given
        result = []
    if times == 1:
        result.append[data]
    else:
        result.append[data]
        replicate_recur[times - 1, data, result]  # also pass in the "result"
    return result
3:Abhishek J

Trên một lưu ý khác, bạn có thể chỉ cần sao chép danh sách của mình bằng:2 gold badges21 silver badges21 bronze badges

1

Adirio

[data] * times

4,8901 Huy hiệu vàng14 Huy hiệu bạc25 Huy hiệu đồng

Đã trả lời ngày 22 tháng 2 năm 2017 lúc 10:00

Moses Koledoyemoses Koledoye4 gold badges34 silver badges52 bronze badges

76.1K8 Huy hiệu vàng126 Huy hiệu bạc133 Huy hiệu đồngFeb 22, 2017 at 10:04

2

Bài Viết Liên Quan

Chủ Đề