Python. How to sum each element with multi times in loop? [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 months ago.
Improve this question
Example:
import numpy
a = [1,2,3,4,5]
b = []
for i in range(len(a)):
b.append(a[i]+1)
Here, I have b = [2,3,4,5,6].
But I want sum multi times...(maybe I want to sum with 3 times). I expected after three times sum I have b = [4,5,6,7,8].
So, how I can get b=[4,5,6,7,8] from a=[1,2,3,4,5] with 3 times add 1 with loop?

Add 3 to each element using a list comprehension;
b = [i+3 for i in a]
or as a function, where you can change the value added to the list easily;
def add_k_to_list(k, a_list):
return [i+k for i in a_list]
To repeatedly apply as per your comment;
def add_k_to_list(k, a_list):
for _ in range(k):
a_list = [i+1 for i in a_list]
return(a_list)

Related

Compare tow list order and return the result [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
l = [1,2,3,4,5]
l1 = [2,5]
l2 = [5,1]
compare(l1,l)
result = PASS
compare(l2,l)
result - FAIL
I have a main list called l. I need to compare the order of the list - If l1 is in the order of l then the result is "PASS", then comparing l2 is in the order of l the it is not in the order of l then "FAIL"
def compare(tested, base):
indexes = [base.index(t) for t in tested]
return {True: "PASS", False: "FAIL"}[indexes == sorted(indexes)]
print(compare(l1, l))

How do these simple Python functions work? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I have run the the following functions and studied them line-by-line. I understand how the outer for-loop in f(n) works and how the while-loop works in g(n) but I understand the role of the inner for-loop in f(n). Also, how do these loops work with t = t+1? Thanks in advance!
def f(n):
t=0
for i in range(n):
for j in range(2*i):
t=t+1
return t
f(5)
def g(n):
t=0
j=n
while j>1:
t = t+1
j = j/2
return t
g(32)
The inner loop keeps adding 1 to t until it adds 2 times each item from the outer loop. So it adds 0 + 2 + 4 + 6 + 8. The range(5) is similar to a list equivalent to [0,1,2,3,4].
t=t+1 simply adds 1 to the value of t every time the line is run.

Python Program using List [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
Accept N numbers in the list L. Shift the first half element to second half and second half elements to first half. Eg. L = [1,2,3,4,5,6] after shifting L = [4,5,6,1,2,3].
If L is a list of length N:
L = L[N // 2:] + L[:N // 2]
will switch the first and second halves of the list.

Convert a List to integer without using map() or join() [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
if I have
list=[1,2,3,4,5,6]
How can i make it
list=123456
Thanks for your help in advance!
You can do this:
inlist=[1,2,3,4,5,6]
length = len(inlist)
s = 0
for i in range(length):
s += (inlist[i] * ( 10 ** (length-1-i)))
inlist = s
print(inlist)
This will give you:
123456
You need to utilize the power of 10 to multiply it with each number.
Note that you shouldn't use list as a variable name as it is a Python keyword.
Another version (without using any built-in functions at all):
inlist=[1,2,3,4,5,6]
count = 1
s = 0
for elem in inlist[::-1]:
s += (elem * ( 10 ** (count-1)))
count += 1
inlist = s
print(inlist)
you can do it by for and join likes the following:
int(''.join([str(i) for i in my_list]))

Python. List comprehension. List in the list and so on [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Is it possible do like that with list comprehension. For example, if I call function f4(5), it should show like: [[[[[]]]]]. So each time append empty list in previous list.
You can append a list to itself, which since lists are mutable, sets off a recursion that leads to an infinitely nested list that can be indexed up until the recursion limit. Python uses the elipisis placeholder to display this:
>>> lst = []
>>> lst.append(lst)
>>> lst
[[...]]
>>> lst[0][0][0][0][0]
[[...]]
However, I can imagine no practical use for this at all.
def f(x):
if x == 1:
return []
return [f(x-1)]
print(f(5))
Output:
[[[[[[]]]]]]
I don't see any real use for that, but of course it's possible.
def f(n):
return [f(n - 1)] if n > 1 else []

Categories

Resources