String Project
String Project
$8.79 ;,; white&blue&red ;,;09/15/17 , Peggy Bell;,; $8.65 ;,;blue ;,; 09/15/17, Kenneth
Cunningham ;,; $10.53;,; green&blue ;,;
$17.13;,; black&blue;,; 09/15/17, Randy Fleming;,; $21.13 ;,;black ;,;09/15/17 ,Elisa Hart;,; $0.35
;,; black&purple;,; 09/15/17 ,
;,; $6.27 ;,; yellow ;,;09/15/17 ,Guadalupe Potter ;,;$21.12 ;,; yellow;,; 09/15/17 ,
;,; $7.47 ;,; red ;,; 09/15/17 , Anna Dean;,;$5.49 ;,; yellow&red ;,; 09/15/17 ,
;,;$19.55 ;,; green&white&red ;,; 09/15/17 ,Karl Ross;,; $15.68;,; white ;,; 09/15/17 , Brandy
Cortez ;,;$23.57;,; white&red ;,;09/15/17,
Mamie Riley ;,;$29.32;,; purple;,;09/15/17 ,Mike Thornton ;,; $26.44 ;,; purple ;,; 09/15/17,
,Josephine Keller ;,;$13.10 ;,;green;,; 09/15/17 , Tracey Wolfe;,;$20.39 ;,; red ;,; 09/15/17 ,
$24.49 ;,; black&red ;,; 09/15/17 , Everett Lyons ;,;$1.81;,; black&red ;,; 09/15/17 ,
#------------------------------------------------
1.
First, take a minute to inspect the string daily_sales in the code editor.
How is each transaction stored? How is each piece of data within the
transaction stored?
Start thinking about how we can split up this string into its individual pieces
of data.
2.
It looks like each transaction is separated from the next transaction by a ,,
and then each piece of data within a transaction is separated by the
artifact ;,;.
Replace all the instances of ;,; in daily_sales with some other character and
save the result to daily_sales_replaced.
Stuck? Get a hint
3.
Now we can split the string into a list of each individual transaction.
Append each of these split strings (which are lists now) to our new
list daily_transactions_split.
Stuck? Get a hint
7.
Print daily_transactions_split.
How’s it looking?
8.
It looks like each data item has inconsistent whitespace around it. First,
define an empty list transactions_clean.
If you performed the last step correctly, you shouldn’t see any unnecessary
whitespace.
10.
Create three empty lists. customers, sales, and thread_sold. We are going to
collect the individual data points for each transaction in these lists.
11.
Now, iterate through transactions_clean and for each transaction:
12.
Print customers, sales, and thread_sold to make sure each list is what you are
expected.
Determine the total value of the days sales.
13.
Now we want to know how much Thread Shed made in a day.
Iterate through sales and for each item, strip off the $, set it equal to a float,
and add it to total_sales
15.
Print total sales.
16.
Finally, we want to determine how many of each color thread we sold
today. Let’s start with a single color, and then we’ll generalize it.
The end product we want here is a list that contains an item for each color
thread sold, so no strings that have multiple colors in them.
If it is multiple colors, first split the string around the & character and then
add each color indivudally to thread_sold_split.
19.
Great, now we have a list thread_sold_split that contains an entry with the
color of every thread sold today.
If you are having trouble thinking of a character to replace ;,; with, try using ;.
Remember, to split a string around a certain delimiter, use the code py new_list
= my_string.split(delimiter)
An easy way to perform a task on every item in a list and add it to a new list is
by following this format:
new_list = []
for item in list:
new_list.append(task(item))
Think about how you can use code of this form to accomplish the given task.
This will require two for loops. One to iterate through the outer
list, daily_transactions_split and one to iterate through each of the transactions
and perform the .strip() on the data points.
22 Thread Shed sold 28 threads of white thread today.