I have lists that I am trying to remove integer only data from. I want to keep any strings that have $, , or -, in them.
string = (",A', B', C', 145', $50', Split', D', E', F', 1', 2000', -678', $10', Split', H', I', J', 3', 4', 50', 132', $45', Split'").replace("'", '')
ls_of_lists = [x.split(",") for x in string.split('Split')]
# What I want my out put to be:
desired_ls_of_ls = [
["A", "B", "C", " 145", "$50"],
["D", "E", "F", "-678", "$10"],
["H", "I", "J", " 132","$45"]
]
I've tried so many things and I bet it's something easy I'm missing/overlooking. On all modified int_filters(see below) I tried search, .contains, and other methods I could think of and looking around the web hasn't helped me either. I've also tried swapping yield and return.
Below is my work:
def int_filter_0( someList ):
for v in someList:
try:
int(v)
continue # Skip these
except ValueError:
yield v # Keep these
def int_filter_1(some_list):
for v in some_list:
if v.find("\\ "):
yield v # Keep these
if v.find("\\-"):
yield v # Keep these
else:
try:
int(v)
continue # Skip these
except ValueError:
yield v # Keep these
def int_filter_2(some_list):
for v in some_list:
if v.contains("\\ "):
yield v # Keep these
if v.contains("\\-"):
yield v # Keep these
else:
try:
int(v)
continue # Skip these
except ValueError:
yield v # Keep these
def int_filter_3( someList ):
for v in someList:
try:
if v.find("\\ "):
continue
if v.find("\\-"):
continue
int(v)
continue # Skip these
except ValueError:
yield v # Keep these
test_0 = [list(int_filter_0(item)) for item in ls_of_lists]
test_1 = [list(int_filter_1(item)) for item in ls_of_lists]
test_2 = [list(int_filter_2(item)) for item in ls_of_lists]
test_3 = [list(int_filter_3(item)) for item in ls_of_lists]
CodePudding user response:
string = (",A', B', C', 145', $50', Split', D', E', F', 1', 2000', -678', $10', Split', H', I', J', 3', 4', 50', 132', $45', Split'")
string = string.replace("'", '').replace(' ', '') # clean ' and spaces
ls_of_lists = [x.split(",") for x in string.split('Split')]
ls_of_lists = [[c for c in ls if (not c.isdigit() and c)] for ls in ls_of_lists] # Clean the numbers and empty strings
ls_of_lists = [ls for ls in ls_of_lists if ls] # Clean empty lists
for l in ls_of_lists:
print(l)
# Output:
# ['A', 'B', 'C', ' 145', '$50']
# ['D', 'E', 'F', '-678', '$10']
# ['H', 'I', 'J', ' 132', '$45']
CodePudding user response:
Here is a efficient method using a regex and a generator:
import re
def split(s):
l = []
for x in re.split(',\s*', s):
if x.isdigit() or not x:
continue
elif x == 'Split':
yield l
l = []
else:
l.append(x)
# if l:
# yield l
out = list(split(string))
Output:
[['A', 'B', 'C', ' 145', '$50'],
['D', 'E', 'F', '-678', '$10'],
['H', 'I', 'J', ' 132', '$45']]
NB. If you want to get the values even if there is no terminal "Split", uncomment the last two lines.
CodePudding user response:
I figured out what I was doing wrong. ON the If v.find it needs to be more direct(Not include the \)and include the !=
def int_filter_1(some_list):
for v in some_list:
if v.find(" ") != -1:
yield v # Keep these
if v.find("-")!= -1:
yield v # Keep these
else:
try:
int(v)
continue # Skip these
except ValueError:
yield v # Keep these
This gives me the corrected results