0
Want to extract number and string.
L=[1,2,3,"r","y",6] How can we extract only number through given list . 2nd - how can we only string in this given list?
6 Answers
+ 9
SoloProg , Ireneo language ,
it is not seen as a helpful behavior when we are going to post a code, as long as the op has not shown his attempt here.
it is helpful to give hints and tips, so that the op has a chance to find a solution by himself.
+ 4
inline version
numbers = [i for i in L if type(i) == int]
strings = [i for i in L if type(i) == str]
+ 3
print ( [e for e in L if str(e).isdigit() ] )
print ( [e for e in L if not str(e).isdigit() ] )
+ 3
To extract only numbers from the given list, you can use a for loop and an if statement:
numbers = []
for i in L:
if type(i) == int:
numbers.append(i)
To extract only strings from the given list, you can use a for loop and an if statement:
strings = []
for i in L:
if type(i) == str:
strings.append(i)
+ 3
Hope this helps
L=[1,2,3,"r","y",6]
d = [e for e in L if str(e).isdigit() ]
print ( d, type(d) )
x = int(''.join(map(str,d)))
print ( x, type(x) )
+ 1
Okay got it Ireneo language SoloProg