Python: select digits from NoneType object - python

I have a 'NoneType' object like:
A='ABC:123'
I would like to get an object keeping only the digits:
A2=digitsof(A)='123'

Split at the colon:
>>> A='ABC:123'
>>> numA = int(A.split(':')[1])
123

How about:
>>> import re
>>> def digitsof(a):
... return [int(x) for x in re.findall('\d+', a) ]
...
>>> digitsof('ABC:123')
[123]
>>> digitsof('ABC:123,123')
[123, 123]
>>>

Regular Expressions?
>>> from re import sub
>>> A = 'ABC:123'
>>> sub(r'\D', '', A)
123

A simple filter function
A='ABC:123'
filter(lambda s: s.isdigit(), A)

Related

Filtering a list of tuples with lambda expressions in Python

What would be the right filter so l will contain [(7,10),(9,20)]
>>> l=[(0,5),(7,10),(9,20),(18,22)]
>>> l=filter(lambda x: x[0]>6 and x[1]<21, l)
>>> l
<filter object at 0x7fb2349829e8>
>>>
I'm getting a "filter object", rather than a list of the 2 middle tuples from the original list.
>>> l=[(0,5),(7,10),(9,20),(18,22)]
>>> l=filter(lambda x: x[0]>6 and x[1]<21, l)
>>> list(l)
>>> [(7,10),(9,20)]

Python re.search string search for open and close bracket []

Can someone explain me why my regex is not getting satisfied for below regex expression. Could someone let me know how to overcome and check for [] match.
>>> str = li= "a.b.\[c\]"
>>> if re.search(li,str,re.IGNORECASE):
... print("Matched")
...
>>>
>>> str = li= r"a.b.[c]"
>>> if re.search(li,str,re.IGNORECASE):
... print("Matched")
...
>>>
If I remove open and close brackets I get match
>>> str = li= 'a.b.c'
>>> if re.search(li,str,re.IGNORECASE):
... print("matched")
...
matched
You are attempting to match the string a.b.\\[c\\] instead of a.b.[c].
Try this:
import re
li= r"a\.b\.\[c\]"
s = "a.b.[c]"
if re.search(li, s, re.IGNORECASE):
print("Matched")
re.IGNORECASE is not needed in here by the way.
You can try the following code:
import re
str = "a.b.[c]"
if re.search(r".*\[.*\].*", str):
print("Matched")
Output:
Matched

Checking two string in python?

let two strings
s='chayote'
d='aceihkjouty'
the characters in string s is present in d Is there any built-in python function to accomplish this ?
Thanks In advance
Using sets:
>>> set("chayote").issubset("aceihkjouty")
True
Or, equivalently:
>>> set("chayote") <= set("aceihkjouty")
True
I believe you are looking for all and a generator expression:
>>> s='chayote'
>>> d='aceihkjouty'
>>> all(x in d for x in s)
True
>>>
The code will return True if all characters in string s can be found in string d.
Also, if string s contains duplicate characters, it would be more efficient to make it a set using set:
>>> s='chayote'
>>> d='aceihkjouty'
>>> all(x in d for x in set(s))
True
>>>
Try this
for i in s:
if i in d:
print i

Extract square-bracketed text from a string

Could someone please help me strip characters from a string to leave me with just the characters held within '[....]'?
For example:
a = newyork_74[mylocation]
b = # strip the frist characters until you reach the first bracket [
c = [mylocation]
Something like this:
>>> import re
>>> strs = "newyork_74[mylocation]"
>>> re.sub(r'(.*)?(\[)','\g<2>',strs)
'[mylocation]'
Assuming no nested structures, one way would be using itertools.dropwhile,
>>> from itertools import dropwhile
>>> b = ''.join(dropwhile(lambda c: c != '[', a))
>>> b
'[mylocation]'
Another would be to use regexs,
>>> import re
>>> pat = re.compile(r'\[.*\]')
>>> b = pat.search(a).group(0)
>>> b
'[mylocation]'

Convert a number to a list of integers [duplicate]

This question already has answers here:
How to split an integer into a list of digits?
(10 answers)
Closed 4 months ago.
How do I write the magic function below?
>>> num = 123
>>> lst = magic(num)
>>>
>>> print lst, type(lst)
[1, 2, 3], <type 'list'>
You mean this?
num = 1234
lst = [int(i) for i in str(num)]
a = 123456
b = str(a)
c = []
for digit in b:
c.append (int(digit))
print c
You could do this:
>>> num = 123
>>> lst = map(int, str(num))
>>> lst, type(lst)
([1, 2, 3], <type 'list'>)
magic = lambda num: map(int, str(num))
then just do
magic(12345)
or
magic(someInt) #or whatever
>>> from collections import deque
>>> def magic(num):
digits = deque()
while True:
num,r = divmod(num,10)
digits.appendleft(r)
if num == 0:
break
return list(digits)
>>> magic(123)
[1, 2, 3]
According to my timings, this solution is considerably faster than the string method (magic2), even for smaller examples.
>>> def magic2(num):
return [int(i) for i in str(num)]
Timings:
magic
>>> timeit.timeit(setup='from __main__ import magic', stmt='magic(123)')
1.3874572762508706
>>> timeit.timeit(setup='from __main__ import magic', stmt='magic(999999999)')
3.2624468999981673
magic2
>>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(123)')
3.693756106896217
>>> timeit.timeit(setup='from __main__ import magic2', stmt='magic2(999999999)')
10.485281719412114
Don't use the word list as variable name! It is a name of python built in data type.
Also, please clarify your question. If you are looking for a way to create a one-member list, do the following:
a = 123
my_list = [a]
and "pythonizing" Cannonade's answer:
a = 123
my_list = [int(d) for d in str(a)]
num = map(int, list(str(num)))
If it is named as magic, why not just use magic:
def magic(x):
if x < 10:
return [x]
else:
return magic(x//10) + [x%10]
for python 3.x:
num = 1234
lst = list(map(int, str(num)))
You can try this:
def convert_to_list(number):
return list(map(lambda x: int(x), str(number)))
convert_to_list(1245)
Just use :
a= str (num)
lst = list(a)

Categories