How to insert a variable in a regex pattern? [duplicate] - python

This question already has answers here:
How to use a variable inside a regular expression?
(12 answers)
Closed 2 years ago.
i have a list :
cpt=0
list=["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(".*derma.*",l):
cpt=cpt+1
So, instead of putting directly 'derma' as regex , i want :
a= "derma" #initialize
list=["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(".*a.*",l):
cpt=cpt+1

You can use an f-string:
a= "derma"
list = ["dermato","bioderma", "gatoderma"]
for l in list:
if re.findall(f".*{a}.*", l):
cpt += 1

Related

Python generate a list of consecutive numbers from a list of numbers [duplicate]

This question already has answers here:
Identify groups of consecutive numbers in a list
(19 answers)
How to pick consecutive numbers from a list [duplicate]
(2 answers)
Closed 4 years ago.
I have a list like this,
List1 = [1,2,3,7,8,11,14,15,16]
And I want to use python to generate a new list that looks like,
List2 = ["1:3", "7:8", "11", "14:16"]
How can I do this, is for loop the option here.
I don't want to use For loop as my list has more than 30 000 numbers.
You can use a generator:
List1 = [1,2,3,7,8,11,14,15,16]
def groups(d):
c, start = [d[0]], d[0]
for i in d[1:]:
if abs(i-start) != 1:
yield c
c = [i]
else:
c.append(i)
start = i
yield c
results = [str(a) if not b else f'{a}:{b[-1]}' for a, *b in groups(List1)]
Output:
['1:3', '7:8', '11', '14:16']

Handling numbers in lists (Python) [duplicate]

This question already has answers here:
Sum a list of numbers in Python
(26 answers)
Closed 4 years ago.
I'm starting to learn how to code and I've been trying to work out how to sum numbers from a list.
list1 = [1,2,3,4,5]
Using a for-loop, how would I set a variable to the sum of the list (15)?
Using a for loop:
acc = 0
for num in list1:
acc += num
Another approach:
acc = sum(list1)

if condition in Python's list comprehension is not working [duplicate]

This question already has answers here:
List comprehension with if statement
(5 answers)
Closed 4 years ago.
I have the following simple code in python giving SyntaxError: invalid syntax
I want a new list with non zero values.
data = [11,2,0,34,8,4]
new_data = [ if x for x in data ]
print( new_data )
Here is how to do this:
new_data = [x for x in data if x != 0]

how to count the letters in a string? [duplicate]

This question already has answers here:
How to count digits, letters, spaces for a string in Python?
(12 answers)
Closed 5 years ago.
how do I code a program that would produce this below?
As an example, the following code fragment:
print (count_letters("ab1c2d345"))
should produce the output:
4
You can try this:
def count_letters(s):
return len([i for i in s if i.isalpha()])
print(count_letters('ab1c2d345'))
Output:
4
Or, you can use regex for a cleaner solution:
import re
def count_letters(s):
return len(re.findall('[a-zA-Z]', s))
You can do it using simple loop/if statement.
def count_letters(str_ltr):
count_ltr = 0
for ch in str_ltr:
if ch.isalpha():
count_ltr += 1
return count_ltr
print(count_letters("ab1c2d345"))
Output: 4

why one line "for" expression is faster in Python? [duplicate]

This question already has answers here:
Python list comprehension expensive
(1 answer)
What is the advantage of a list comprehension over a for loop?
(1 answer)
Why is local variable access faster than class member access in Python?
(2 answers)
Closed 5 years ago.
I have tested four situations.
1.
testList = list()
for i in range(1000000):
testList.append(i)
2.
testList = list()
for i in range(1000000): testList.append(i)
3.
testList = list()
newAppend = testList.append
for i in range(1000000): newAppend(i)
4.
[i for i in range(1000000)]
Each time was
1 : 0.09166
2 : 0.08299
3 : 0.05003
4 : 0.04594
Why others are faster than 1?
Can I find related keywords?

Categories