Kinh Nghiệm về Python recursion append to list 2022
Bạn đang tìm kiếm từ khóa Python recursion append to list được Cập Nhật vào lúc : 2022-10-05 17:20:38 . Với phương châm chia sẻ Bí kíp Hướng dẫn trong nội dung bài viết một cách Chi Tiết Mới Nhất. Nếu sau khi tìm hiểu thêm nội dung bài viết vẫn ko hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Admin lý giải và hướng dẫn lại nha.
I want to append to a list recursively but I cannot come up with a function that works. The function takes two arguments times and data. times should be the number of times to append the data.
Nội dung chính
- How do you add elements to a recursion list in Python?How do you implement a recursive function in python?What is recursion example?
Here is my code so far:
def replicate_recur(times, data):
result2 = []
if times == 0:
result2.append(data)
else:
result2.append(data)
replicate_recur(times – 1, data)
return result2
MSeifert
138k32 gold badges318 silver badges333 bronze badges
asked Feb 22, 2022 9:54
4
You could use a intermediate list to append to in each recursive call. That avoids these redefinition problems you’re encountering currently:
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
When called:
>>> replicate_recur(4, 2)
[2, 2, 2, 2]
answered Feb 22, 2022 10:06
MSeifertMSeifert
138k32 gold badges318 silver badges333 bronze badges
To make your code work, you need to extend the
list in the current execution with the output of the next recursive call. Also, the lowest depth of the recursion should be defined by times = 1:
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
On another note, you can simply replicate your list with:
def replicate(times, data):
return [data]*times
Adirio
4,8801 gold badge14 silver badges25 bronze badges
answered Feb 22, 2022 10:00
Moses KoledoyeMoses Koledoye
75.8k8 gold badges124 silver badges133 bronze badges
2
You can use xrange for this, there is no point to use recursion unless it is a coding test.
def replicate(times, data):
result2 = []
for i in xrange(times):
result2.append(data)
return result2
Same function can be written in a
recursive way like this:
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 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
Adirio
4,8801 gold badge14 silver
badges25 bronze badges
answered Feb 22, 2022 9:58
3
Because your redefining result2 everytime. Keep result2 outside the function and it should work.
Also you could consider doing data*times to replicate if data is a list or simply do
(result2.append(data))*times
answered Feb 22, 2022 9:58
Abhishek JAbhishek J
2,2662 gold badges21 silver badges21 bronze badges
1
In the
recursion, each time replicate_recur is called, a fresh result2 in new name space is created.
[data] * times
Would do what you are trying to achieve.
Rahul K P
11.9k3 gold badges34 silver badges50 bronze badges
answered Feb 22, 2022 10:04
2
How do you add elements to a recursion list in Python?
sum number in a list python using recursion. def listsum(numList):. if len(numList) == 1:. return numList[0]. return numList[0] + listsum(numList[1:]). print(listsum([1,3,5,7,9])).
How do you implement a recursive function in python?
Python also accepts function recursion, which means a defined function can call itself. Recursion is a common mathematical and programming concept. It means that a function calls itself. This has the benefit of meaning that you can loop through data to reach a result.
What is recursion example?
Recursion is the process of defining a problem (or the solution to a problem) in terms of (a simpler version of) itself. For example, we can define the operation “find your way home” as: If you are home, stop moving. Take one step toward home.
Tải thêm tài liệu liên quan đến nội dung bài viết Python recursion append to list
programming
python
Python recursion list
Print recursive Python
Recursive list Java
Reply
5
0
Chia sẻ
Review Python recursion append to list ?
Bạn vừa tìm hiểu thêm tài liệu Với Một số hướng dẫn một cách rõ ràng hơn về Review Python recursion append to list tiên tiến và phát triển nhất
Share Link Down Python recursion append to list miễn phí
Quý khách đang tìm một số trong những Chia SẻLink Download Python recursion append to list miễn phí.
Thảo Luận vướng mắc về Python recursion append to list
Nếu Bạn sau khi đọc nội dung bài viết Python recursion append to list , bạn vẫn chưa hiểu thì hoàn toàn có thể lại Comment ở cuối bài để Tác giả lý giải và hướng dẫn lại nha
#Python #recursion #append #list