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
Related
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)
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 7 months ago.
Improve this question
Here is my current code. I would like it to be in one line I don't know how to though so I came here.
if int(input("enter num")) %2 == 0:
print("yay")
Since your question wasn't "completely clear". Such as what the output is supposed to be, and if you wanted to compress your code into 1 line or not...
But try this:
#1st code~ returns output like: ['123ello', 'h123llo', 'he123123o', 'he123123o', 'hell123']
#2nd code ~ returns output like: 123123123123123 (#Tim Roberts's comment)
1 line versions
#1
i = input(">");print([i.replace(i[d],"123") for d in range(len(i))])
#2
x = input(">");print('123' * len(i))
Multiline versions
#1
i = input(">")
x = []
for d in range(len(i)):
x.append(i.replace(i[d],"123"))
print(x)
#2
x = input(">")
print('123' * len(i))
The following code works with regex. Does it give the desired result?
import re
i = 'i2t'
print(re.sub('[^\W\d_]', '123', i))
The output from above will be: 1232123
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))
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]
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 7 years ago.
Improve this question
How can I find the index of an element of a list which may contain duplicate items?
This is my code:
def as_str(L):
string = "("
for element in L:
string += str(element)
if L.index(element) < len(L) - 1:
string += ", "
string += ")"
return string
which doesn't work if the list contains duplicate items.
You could simply use join, e.g.:
def as_str(L):
return "({})".format(", ".join([str(x) for x in L]))
Addressing your comment, L.index takes optional 'start' and 'stop' parameters that you could use to search a range of indexes (e.g. in a loop).