Replace every letter in an input with the numbers 123 [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 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

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)

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

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

Iterator python [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 8 years ago.
Improve this question
I was trying to do something like this for example
def functionx(x):
while x > 0:
x = 2 + 2
x -= 1
for x in functionx(x):
print(x)
Well, in for I wanted to print x = 2 + 2 but it just give me the direction where the function is.
Also just wanted to use x = 2 + 2, use it in another function but then use the stored number again and so on but I don't know how to do that.
Use the yield keyword.
Example
def functionx(x):
while x > 0:
x += 1
yield x
for i in functionx(1):
print i
This creates the functionx as an iterator.

Categories

Resources