Reputation: 779
The current list looks like this:
line_list = ['Rent 350', 'Gas 60', 'Food 50', 'Clothing 40', 'Car Payment 500', 'Electric Bill 150', 'Cell Phone Bill 150', 'Miscellaneous 10']
I would like the output to look like this:
labels = ['Rent', 'Gas', 'Food', 'Clothing', 'Car Payment', 'Electric Bill', 'Cell Phone Bill', 'Miscellaneous']
amount = ['350', '60', '50', '40','500','150', '150', '10']
Basically, I'm trying to split the list into one list with just numbers and one list with the words/phrases.
Upvotes: 0
Views: 69
Reputation: 19422
Assuming the structure of your phrases is as in the example (some words and a number in the end), you could use re
's split
:
>>> import re
>>> word_list = []
>>> num_list = []
>>> for phrase in line_list:
parts = re.split(" (?=\d)", phrase)
word_list.append(parts[0])
num_list.append(parts[1])
>>> word_list
['Rent', 'Gas ', 'Food', 'Clothing', 'Car Payment', 'Electric Bill', 'Cell Phone Bill', 'Miscellaneous']
>>> num_list
['350', '60', '50', '40', '500', '150', '150', '10']
You might get tempted to use list-comprehension here but that would mean going over the list twice, so an old-fashioned loop will be best to loop once and create both lists.
Upvotes: 1
Reputation: 10809
line_list = ['Rent 350', 'Gas 60', 'Food 50', 'Clothing 40', 'Car Payment 500', 'Electric Bill 150', 'Cell Phone Bill 150', 'Miscellaneous 10']
expenses = []
costs = []
for *expense, cost in map(str.split, line_list):
expenses.append(" ".join(expense))
costs.append(cost)
Upvotes: 3