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?

8th Feb 2023, 11:44 AM
Akash Gupta
Akash Gupta - avatar
6 Respostas
+ 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.
8th Feb 2023, 6:23 PM
Lothar
Lothar - avatar
+ 4
inline version numbers = [i for i in L if type(i) == int] strings = [i for i in L if type(i) == str]
8th Feb 2023, 12:28 PM
Ireneo language
+ 3
print ( [e for e in L if str(e).isdigit() ] ) print ( [e for e in L if not str(e).isdigit() ] )
8th Feb 2023, 12:01 PM
SoloProg
SoloProg - avatar
+ 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)
8th Feb 2023, 12:20 PM
Ireneo language
+ 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) )
8th Feb 2023, 12:26 PM
SoloProg
SoloProg - avatar
+ 1
8th Feb 2023, 12:39 PM
Akash Gupta
Akash Gupta - avatar