Python Program using List [closed] - python

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.

Related

Python. How to sum each element with multi times in loop? [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 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)

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))

Finding symbols in a 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 1 year ago.
Improve this question
I have a list e.g. ["1","2","n","5","n","x","0","v","-","a","l","4","1","v","m","z","o","5","%","d",";","a","m","6"]
I want to print out the elements that are a letter or special symbol.
how do I do this?
Python comes with an isnumeric() method that returns true when a string is a number. In the case you listed you could try:
string_list = ["1","2","n","5","n","x","0","v","-","a","l","4","1","v","m","z","o","5","%","d",";","a","m","6"]
for value in string_list:
if not value.isnumeric():
print(value)
...which would output
n
n
x
v
-
a
l
v
m
z
o
%
d
;
a
m
You can use isdigit(). Like so:
some_str = "12a"
for i in some_str:
print(i.isdigit())
# outputs
True
True
False

How Values are assigned in Anonymous function? [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 don't understand whether 15 is sent to x or n
def func_compute(n):
return lambda x : x * n
result = func_compute(2)
print("Double the number of 15 =", result(15))
Assume a function f(x, n) = xn. Now you want to have this function for a fixed n, i.e., f_n(x) = xn. This is what func_compute(n) does, it returns f_n(x), a function, for the given n.
result(15) is then f_2(15) = f(15, 2) = 30.

append all Integers of an integer in a list in python using recursive function [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 5 years ago.
Improve this question
I need an example of a recursive function that will append all integer elements of a number into a list using python and doing this with the most basic algorithm.
For example if n = 1234, list will be [1,2,3,4].
Try this?
def numberToList(number):
# base case
if number == 0:
return []
# recurse
return numberToList(number / 10) + [ number % 10 ]
Run it like this:
>>> numberToList(1234)
[4, 3, 2, 1]

Categories

Resources