why can't I use if x == "T" x == " " to replace T - python

I want to write a function, replacing T by space.
b=""
xs=list("fghtThjchk")
for x in xs:
if x=="T" or "t":
x==" "
b=b+x
I can do it with .replace, but I still want to know can I use list to do it.

x==" " isn't the same as x=" " which is why b is getting the wrong value
if x=="T" or "t": doesn't mean "is x one of 'T' or 't'". You need to say it like this if x in 'Tt': or this if x=="T" or x=="t":
Perhaps this is what you are trying to do
b = ""
for x in "fghtThjchk":
if x == "T" or x == "t":
x = " "
b = b + x

This line doesn't do what you want
if x=="T" or "t":
That condition always evaluates to true, because "t" is considered truthy.
You mean
if x=="T" or x=="t":
And the line that says
x==" "
was meant, I guess, to be an assignment and not a comparison:
x=" "

The syntax you are using is little bit incorrect.
The correct way of using if/else in list comprehensions is given below:
''.join([' ' if x=='T' or x=='t' else x for x in xs])
-- This method is littler faster & optimized when the string is of large length.
-- We are creating string objects again & again in above answers, which is not recommended.
-- So always use join() to join the list after performing all the operations on it instead of string concatenation in loops.

We can make use of the re (Regular Expression) module, which is fast:
import re
aa = re.sub(r'([.tT])', " ", "fghtThjchk")

You need to reference the list by index, such as this:
for i, x in enumerate(xs):
if x in ["T", "t"]:
xs[i] = " "

When you do for x in xs you're assigning the name x to each element of xs one at a time. When you do x=" " (I presume == was a typo) you're reassigning the name x but not changing the original element in xs.

Related

Python use in the same line "For", "If" and "Else"

I want to learn python "secrets", it's possible to put all this code in one line?:
word_not_clean="Casà: 25$"
word_clean=""
for x in word_not_clean:
if x in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789':
word_clean+=x
else:
word_clean+='_'
This sentences work, but no replace especial characters:
word_clean="".join(x for x in word_not_clean if x in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789')
But this sentence with else doesn't works.
word_clean="".join(x for x in word_not_clean if x in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789' else '_')
Thank you in advance.
If you want to do it using list comprehension (one-lining lines) add the if-else statement before the for:
chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'
word_clean = "".join([x if x in chars else '_' for x in word_not_clean])
You can try like the below and also, you can use string.ascii_uppercase + string.ascii_lowercase + string.digits.
import string
word_not_clean="Casà: 25$"
chars_check = string.ascii_uppercase + string.ascii_lowercase + string.digits
result = ''.join(x if x in chars_check else '_' for x in word_not_clean)
print(result)
# 'Cas___25_'
Explanation:
>>> import string
>>> string.ascii_uppercase + string.ascii_lowercase + string.digits
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
There are two different kinds of if statements in generator expressions. The first kind (which I’ll call an “inclusion if”) comes after the for statement, and indicates whether a given item from an iterator should be included in the resultant list. This is what you’re using in the first example.
The second kind (which I’ll call a “ternary if”) comes before the if statement, and used Python’s ternary operator __ if __ else __ to decide what should be outputted by the iterator. This is what you want for the second example, but you need to reorder your terms:
OKAY = “abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789”
word_clean = "".join(
x if x in OKAY else '_' # Ternary if
for x in word_not_clean
)

Use Count to count the number of characters in two separate strings

I am trying to use count to count the number of characters in two separate strings. I can get it to work for just one but how can I format it to count for two strings? I have tried using 'and' but it is not working. What I want is when the user enters inputs for name1(Anna) and name2(Andy) I want the result to be 3 counting the letter 'a' in both names.
name1 = input("What is your name? \n")
name2 = input("What is their name? \n")
lower1 = name1.lower()
lower2 = name2.lower()
a = lower1.count("a") and lower2.count("a")
print(a)
and is boolean operator.
The expression x and y first evaluates x; if x is false, its value is
returned; otherwise, y is evaluated and the resulting value is
returned.
You want to use addition
a = lower1.count("a") + lower2.count("a")
You can also first concatenate the two strings and then count:
a = (lower1 + lower2).count("a")
I suppose you want to add the two counts, right? So simply:
a = lower1.count("a") + lower2.count("a")
print(a)
should be enough in your case.
and is a boolean operator, which cant be used when assigning variables with the sum of two other variables. The correct way is:
a = lower1.count("a") + lower2.count("a")

Surround strings in a list with a certain character

I have a list consisting of strings and I need to surround each of the strings with asterisks. With the input:
def addAsterisks(["abc", "ded"])
The output should be:
["*abc*", "*ded*"]
Here is what I have tried:
for x in strs:
x = "*" + x + "*"
However, the output is unchanged:
["abc", "ded"]
x is a local variable, so changing it is meaningless once it goes out of scope. You could, e.g., return the modified list and assign it somewhere:
def addAsterisks(l)
return ['*' + x + '*' for x in l]
myList = addAsterisks(["abc", "ded"])
You can use, also, str.format():
def addAsterisks(a):
# Or:
# return ["*%s*" %k for k in a]
return ["*{}*".format(k) for k in a]
print(addAsterisks(["abc", "ded"]))
>>> ['*abc*', '*ded*
You have to modify the list to reflect changes in your list. I used inline function for compact code.
Code
['*'+ i + "*" for picture in pictures]
input
["abc", "def"]
Output
["*abc*", "*def*"]
Modify the the array and return to where you want to use. Modifying a temp variable (x) wont work since u will loose it once you are out of scope.
def addAsterisks(string_list):
return ['*' + x + '*' for x in string_list]
print addAsterisks(["abc", "ded"])
This will output:
['*abc*', '*ded*']
You are assigning to a temporary variable x, which does not change the actual list. Try instead
return ["*" + x + "*" for x in picture]
or, if you want a loop:
for i,x in enumerate(picture):
picture[i] = "*" + x + "*"
You can also do like this:
In [22]: def addAsterisks(s):
...: newarr = []
...: for i in s:
...: newstr = "*" + str(i) + "*"
...: newarr.append(newstr)
...: print(newarr)
...:
In [23]: addAsterisks(["abc", "ded"])
['*abc*', '*ded*']
Here is the Pythonic way of tackling this question:
A = [ "*"+a+"*" for a in picture ]
Hint: Read about list comprehension in Python.
I'm late at the party.
Other solutions are better for the specific use case, but this might help someone else (it is a totally different approach from other answers).
If you are really sure about the length of each string to be exactly three, you can do the following:
def addAsterisks(items):
return [ f'{value:*^5}' for value in items ]
addAsterisks(["abc","def"])
Please note that if you change 5 to 7 you will surrond each string with 2 asterisks

Python - making a function that would add "-" between letters

I'm trying to make a function, f(x), that would add a "-" between each letter:
For example:
f("James")
should output as:
J-a-m-e-s-
I would love it if you could use simple python functions as I am new to programming. Thanks in advance. Also, please use the "for" function because it is what I'm trying to learn.
Edit:
yes, I do want the "-" after the "s".
Can I try like this:
>>> def f(n):
... return '-'.join(n)
...
>>> f('james')
'j-a-m-e-s'
>>>
Not really sure if you require the last 'hyphen'.
Edit:
Even if you want suffixed '-', then can do like
def f(n):
return '-'.join(n) + '-'
As being learner, it is important to understand for your that "better to concat more than two strings in python" would be using str.join(iterable), whereas + operator is fine to append one string with another.
Please read following posts to explore further:
Any reason not to use + to concatenate two strings?
which is better to concat string in python?
How slow is Python's string concatenation vs. str.join?
Also, please use the "for" function because it is what I'm trying to learn
>>> def f(s):
m = s[0]
for i in s[1:]:
m += '-' + i
return m
>>> f("James")
'J-a-m-e-s'
m = s[0] character at the index 0 is assigned to the variable m
for i in s[1:]: iterate from the second character and
m += '-' + i append - + char to the variable m
Finally return the value of variable m
If you want - at the last then you could do like this.
>>> def f(s):
m = ""
for i in s:
m += i + '-'
return m
>>> f("James")
'J-a-m-e-s-'
text_list = [c+"-" for c in text]
text_strung = "".join(text_list)
As a function, takes a string as input.
def dashify(input):
output = ""
for ch in input:
output = output + ch + "-"
return output
Given you asked for a solution that uses for and a final -, simply iterate over the message and add the character and '-' to an intermediate list, then join it up. This avoids the use of string concatenations:
>>> def f(message)
l = []
for c in message:
l.append(c)
l.append('-')
return "".join(l)
>>> print(f('James'))
J-a-m-e-s-
I'm sorry, but I just have to take Alexander Ravikovich's answer a step further:
f = lambda text: "".join([c+"-" for c in text])
print(f('James')) # J-a-m-e-s-
It is never too early to learn about list comprehension.
"".join(a_list) is self-explanatory: glueing elements of a list together with a string (empty string in this example).
lambda... well that's just a way to define a function in a line. Think
square = lambda x: x**2
square(2) # returns 4
square(3) # returns 9
Python is fun, it's not {enter-a-boring-programming-language-here}.

returning a list in a single line in python

I'm just curious if there is a simpler way to do this. If I want to print a list of items on one line I simply write
for i in things:
print i,
but if I substitute print for return I'm obviously only going to get the first item of the list. I needed the list to comma and space separated as well so I ended up with a function that looks like this
def returner(things):
thing = ""
n = 1
for i in things:
thing += i
if n < len(things):
thing += ", "
n += 1
return thing
Was there a better way to do this?
Use join
return ", ".join([str(x) for x in things])
You can use the string join function -
",".join(things)
I think you are confusing identity with representation
def returner(things): #misnomer since this is actually a generator
for itm in things:
yield itm
print ", ".join(map(str,returner(a_list)))

Categories

Resources