Getting 27 for the “white” count instead of 28

Are you familiar with the list.extend() method? It might be just the thing you are looking for.

I looked the .extend method up and tried implementing it in my code like this. It unfortunately still put the sublists in the thread_sold_split list. Did I use it in the wrong way or should I try something else?

daily_sales = \
"""Edith Mcbride   ;,;$1.21   ;,;   white ;,; 
09/15/17   ,Herbert Tran   ;,;   $7.29;,; 
white&blue;,;   09/15/17 ,Paul Clarke ;,;$12.52 
;,;   white&blue ;,; 09/15/17 ,Lucille Caldwell   
;,;   $5.13   ;,; white   ;,; 09/15/17,
Eduardo George   ;,;$20.39;,; white&yellow 
;,;09/15/17   ,   Danny Mclaughlin;,;$30.82;,;   
purple ;,;09/15/17 ,Stacy Vargas;,; $1.85   ;,; 
purple&yellow ;,;09/15/17,   Shaun Brock;,; 
$17.98;,;purple&yellow ;,; 09/15/17 , 
Erick Harper ;,;$17.41;,; blue ;,; 09/15/17, 
Michelle Howell ;,;$28.59;,; blue;,;   09/15/17   , 
Carroll Boyd;,; $14.51;,;   purple&blue   ;,;   
09/15/17   , Teresa Carter   ;,; $19.64 ;,; 
white;,;09/15/17   ,   Jacob Kennedy ;,; $11.40   
;,; white&red   ;,; 09/15/17, Craig Chambers;,; 
$8.79 ;,; white&blue&red   ;,;09/15/17   , Peggy Bell;,; $8.65 ;,;blue   ;,; 09/15/17,   Kenneth Cunningham ;,;   $10.53;,;   green&blue   ;,; 
09/15/17   ,   Marvin Morgan;,;   $16.49;,; 
green&blue&red   ;,;   09/15/17 ,Marjorie Russell 
;,; $6.55 ;,;   green&blue&red;,;   09/15/17 ,
Israel Cummings;,;   $11.86   ;,;black;,;  
09/15/17,   June Doyle   ;,;   $22.29 ;,;  
black&yellow ;,;09/15/17 , Jaime Buchanan   ;,;   
$8.35;,;   white&black&yellow   ;,;   09/15/17,   
Rhonda Farmer;,;$2.91 ;,;   white&black&yellow   
;,;09/15/17, Darren Mckenzie ;,;$22.94;,;green 
;,;09/15/17,Rufus Malone;,;$4.70   ;,; green&yellow 
;,; 09/15/17   ,Hubert Miles;,;   $3.59   
;,;green&yellow&blue;,;   09/15/17   , Joseph Bridges  ;,;$5.66   ;,; green&yellow&purple&blue 
;,;   09/15/17 , Sergio Murphy   ;,;$17.51   ;,;   
black   ;,;   09/15/17 , Audrey Ferguson ;,; 
$5.54;,;black&blue   ;,;09/15/17 ,Edna Williams ;,; 
$17.13;,; black&blue;,;   09/15/17,   Randy Fleming;,;   $21.13 ;,;black ;,;09/15/17 ,Elisa Hart;,; $0.35   ;,; black&purple;,;   09/15/17   ,
Ernesto Hunt ;,; $13.91   ;,;   black&purple ;,;   
09/15/17,   Shannon Chavez   ;,;$19.26   ;,; 
yellow;,; 09/15/17   , Sammy Cain;,; $5.45;,;   
yellow&red ;,;09/15/17 ,   Steven Reeves ;,;$5.50   
;,;   yellow;,;   09/15/17, Ruben Jones   ;,; 
$14.56 ;,;   yellow&blue;,;09/15/17 , Essie Hansen;,;   $7.33   ;,;   yellow&blue&red
;,; 09/15/17   ,   Rene Hardy   ;,; $20.22   ;,; 
black ;,;   09/15/17 ,   Lucy Snyder   ;,; $8.67   
;,;black&red  ;,; 09/15/17 ,Dallas Obrien ;,;   
$8.31;,;   black&red ;,;   09/15/17,   Stacey Payne 
;,;   $15.70   ;,;   white&black&red ;,;09/15/17   
,   Tanya Cox   ;,;   $6.74   ;,;yellow   ;,; 
09/15/17 , Melody Moran ;,;   $30.84   
;,;yellow&black;,;   09/15/17 , Louise Becker   ;,; 
$12.31 ;,; green&yellow&black;,;   09/15/17 ,
Ryan Webster;,;$2.94 ;,; yellow ;,; 09/15/17 
,Justin Blake ;,; $22.46   ;,;white&yellow ;,;   
09/15/17,   Beverly Baldwin ;,;   $6.60;,;   
white&yellow&black ;,;09/15/17   ,   Dale Brady   
;,;   $6.27 ;,; yellow   ;,;09/15/17 ,Guadalupe Potter ;,;$21.12   ;,; yellow;,; 09/15/17   , 
Desiree Butler ;,;$2.10   ;,;white;,; 09/15/17  
,Sonja Barnett ;,; $14.22 ;,;white&black;,;   
09/15/17, Angelica Garza;,;$11.60;,;white&black   
;,;   09/15/17   ,   Jamie Welch   ;,; $25.27   ;,; 
white&black&red ;,;09/15/17   ,   Rex Hudson   
;,;$8.26;,;   purple;,; 09/15/17 ,   Nadine Gibbs 
;,;   $30.80 ;,;   purple&yellow   ;,; 09/15/17   , 
Hannah Pratt;,;   $22.61   ;,;   purple&yellow   
;,;09/15/17,Gayle Richards;,;$22.19 ;,; 
green&purple&yellow ;,;09/15/17   ,Stanley Holland 
;,; $7.47   ;,; red ;,; 09/15/17 , Anna Dean;,;$5.49 ;,; yellow&red ;,;   09/15/17   ,
Terrance Saunders ;,;   $23.70  ;,;green&yellow&red 
;,; 09/15/17 ,   Brandi Zimmerman ;,; $26.66 ;,; 
red   ;,;09/15/17 ,Guadalupe Freeman ;,; $25.95;,; 
green&red ;,;   09/15/17   ,Irving Patterson 
;,;$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, 
Jamie Vaughn   ;,; $17.24;,;green ;,; 09/15/17   , 
Noah Day ;,;   $8.49   ;,;green   ;,;09/15/17   
,Josephine Keller ;,;$13.10 ;,;green;,;   09/15/17 ,   Tracey Wolfe;,;$20.39 ;,; red   ;,; 09/15/17 ,
Ignacio Parks;,;$14.70   ;,; white&red ;,;09/15/17 
, Beatrice Newman ;,;$22.45   ;,;white&purple&red 
;,;   09/15/17, Andre Norris   ;,;   $28.46   ;,;   
red;,;   09/15/17 ,   Albert Lewis ;,; $23.89;,;   
black&red;,; 09/15/17,   Javier Bailey   ;,;   
$24.49   ;,; black&red ;,; 09/15/17   , Everett Lyons ;,;$1.81;,;   black&red ;,; 09/15/17 ,   
Abraham Maxwell;,; $6.81   ;,;green;,;   09/15/17   
,   Traci Craig ;,;$0.65;,; green&yellow;,; 
09/15/17 , Jeffrey Jenkins   ;,;$26.45;,; 
green&yellow&blue   ;,;   09/15/17,   Merle Wilson 
;,;   $7.69 ;,; purple;,; 09/15/17,Janis Franklin   
;,;$8.74   ;,; purple&black   ;,;09/15/17 ,  
Leonard Guerrero ;,;   $1.86   ;,;yellow  
;,;09/15/17,Lana Sanchez;,;$14.75   ;,; yellow;,;   
09/15/17   ,Donna Ball ;,; $28.10  ;,; 
yellow&blue;,;   09/15/17   , Terrell Barber   ;,; 
$9.91   ;,; green ;,;09/15/17   ,Jody Flores;,; 
$16.34 ;,; green ;,;   09/15/17,   Daryl Herrera 
;,;$27.57;,; white;,;   09/15/17   , Miguel Mcguire;,;$5.25;,; white&blue   ;,;   09/15/17 ,   
Rogelio Gonzalez;,; $9.51;,;   white&black&blue   
;,;   09/15/17   ,   Lora Hammond ;,;$20.56 ;,; 
green;,;   09/15/17,Owen Ward;,; $21.64   ;,;   
green&yellow;,;09/15/17,Malcolm Morales ;,;   
$24.99   ;,;   green&yellow&black;,; 09/15/17 ,   
Eric Mcdaniel ;,;$29.70;,; green ;,; 09/15/17 
,Madeline Estrada;,;   $15.52;,;green;,;   09/15/17 
, Leticia Manning;,;$15.70 ;,; green&purple;,; 
09/15/17 ,   Mario Wallace ;,; $12.36 ;,;green ;,; 
09/15/17,Lewis Glover;,;   $13.66   ;,;   
green&white;,;09/15/17,   Gail Phelps   ;,;$30.52   
;,; green&white&blue   ;,; 09/15/17 , Myrtle Morris 
;,;   $22.66   ;,; green&white&blue;,;09/15/17"""

#------------------------------------------------
# Start coding below!

#print(daily_sales)
daily_sales_replaced = daily_sales.replace(';,;', '+')
daily_transactions = daily_sales_replaced.split(',')
#print(daily_transactions)
daily_transactions_split = []
for transaction in daily_transactions:
    daily_transactions_split.append(transaction.split('+'))
#print(daily_transactions_split)
transactions_clean = []
for transaction in daily_transactions_split:
  transaction_clean = []
  for data_point in transaction:
    transaction_clean.append(data_point.replace("\n", "").strip(" "))
  transactions_clean.append(transaction_clean)
#print(transactions_clean)

customers =[]
sales =[]
thread_sold=[]
for transactions in transactions_clean:
  customers.append(transactions[0])
  sales.append(transactions[1])
  thread_sold.append(transactions[2])

#print(sales)

total_sales = 0
for sale in sales:
 total_sales += float(sale.strip('$'))
#print(total_sales)

#print(thread_sold)
thread_sold_split =[]
test =[]

for thread in thread_sold:
  if thread == 'white':
    thread_sold_split.append(thread)
  if thread == 'purple':
    thread_sold_split.append(thread)
  if thread == 'blue':
    thread_sold_split.append(thread)
  if thread =='black':
    thread_sold_split.append(thread)
  if thread == 'green':
    thread_sold_split.append(thread)
  if thread == 'yellow':
    thread_sold_split.append(thread)
  if thread == 'red':
    thread_sold_split.append(thread)
  else:
    test.append(thread.split('&'))
thread_sold_split.extend(test)
print(thread_sold_split)

This is appending a list to test…

 test[[...]]

The idea I have is to remove all the if statements and have just one line…

 thread_sold.extend(thread.split('&'))
1 Like

Ahhh I see, well I guess that one line is a lot easier than the mess I created haha.
Thanks for the help my guy!!!

1 Like

Thanks! You just helped me solve a problem that has had me stuck for the last 4 hours.

I am with you on this one… I don’t see any clear explanation here on why that 00000000005 is added…

This is the explanation I was looking for, thanks Roy!

1 Like
transactions_clean = []
for trans in daily_transactions_split:
  transaction_clean = []
  for data in trans: 
    transaction_clean.append(data.replace("\n"," ").strip(" "))
    transactions_clean.append(transaction_clean) #<----- Here

You are appending the same transaction 4 times in transactions_clean, because there are 4 data points and you are saying append the transaction for each data.

[[‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘09/15/17’], [‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘09/15/17’], [‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘09/15/17’], [‘Edith Mcbride’, ‘$1.21’, ‘white’, ‘09/15/17’]…]

The code only need to be unindent

transactions_clean = []
for trans in daily_transactions_split:
  transaction_clean = []
  for data in trans: 
    transaction_clean.append(data.replace("\n"," ").strip(" "))
  transactions_clean.append(transaction_clean) #<----- Here

That’s why you have 4 times the expected value
PD: sorry for my English :smile:

I’m working on step 17 and I’m trying the following code:

thread_sold_split = []
double_thread = []
for thread in thread_sold:
  if not '&' in thread:
    thread_sold_split.append(thread)
  else:
    double_thread.append(thread.split("&"))
    for color in double_thread:
      for c in color:
        thread_sold_split.append(c)

When I print double_thread it seems to contain the values I’m looking for, but when I iterate through the entire loop I end up with many many more entries than in the original list, and some seemingly strange order. I’m clearly doing something wrong but I can’t figure it out. Can someone let me know?

Have a look at the values you’re iterating through, either within the loop or at the end of this section (something like print might help). Is there anything that has been increasing when it shouldn’t?

Thanks for the guidance - using the Python visualizer (great tool!) I was able to figure it out. What I failed to realize is each time through the loop double_thread I was adding a list without removing the previous list.

This means for the first else loop double_thread would work, but I was iterating over the entire double_thread list, adding the split string for the new list as well as all previous list each time.

I solved this by simply setting double_thread to empty after each loop through. The new code looks like this.

thread_sold_split = []
double_thread = []
for thread in thread_sold:
  if not '&' in thread:
    thread_sold_split.append(thread)
  else:
    double_thread.append(thread.split("&"))
    for color in double_thread:
      for c in color:
        thread_sold_split.append(c)
      double_thread = []
print(thread_sold_split)

And actually looking again I don’t need to define double thread outside of the loop because it’s only used in the else portion. Glad I figured it out!

Hi There,
For step 8 i wrote the following code:

transactions_clean=

for listt in daily_transactions_split:

for datapoint in listt:

datapoint.strip()

transactions_clean.append(listt)

It doesn’t remove any whitespace, if i change the datapoint.strip() to datapoint.strip(’ ') it removes only the whitespace after the word/number.
Could anybody help me?

>>> "    word    ".strip()
'word'
>>> " . . . word . . . ".strip('. ')
'word'
>>> 

Thanks for your answer mtf,
I understand how the strip command works and what you did in the code above. Yet I don’t understand why my code doesn’t remove the whitespace and if i change it to .strip(’ ') it doest, but only the whitespace after the datapoints.

I’ve made a screenshot of my written code, hopefully you can clear some things out for me!
Kind regards,
Stijn van den Berg

image

Line 121 needs to reassign, perhaps? Or append to a temporary list created in the outer list?

i got the same thing and it bothered me too, but to keep it clean and to not trigger my OCD i used:

print(“${}”.format(round(total_sales,2)))

…and then it made… cents! :joy:
( :grimacing: sorry)

With string formatting we don’t need to use round(). Simply use the directive, .2f

print (f"${total_sales:.2f}")
1 Like

This is my third time trying to get this project right. keep having an issue with my code not cleaning out any of the “\n” characters. Can someone give me a suggestion here? Below is my code and the error I get currently:

daily_sales_replaced = daily_sales.replace(“;,;”,“+”)

daily_transactions = daily_sales_replaced.split(“,”)

daily_transactions_split =

for transaction in daily_transactions:

daily_transactions_split.append(transaction.split(“+”))

print(daily_transactions_split)

transactions_clean =

for transaction in daily_transactions_split:

transaction_clean =

for data_point in transaction:

transaction_clean.append(data_point.strip(" "))

transactions_clean.append(transaction_clean)

#print(transactions_clean)

customers =

sales =

thread_sold =

for transaction in transactions_clean:

customers.append(transaction[0])

sales.append(transaction[1])

thread_sold.append(transaction[2])

total_sales = 0

for sale in sales:

total_sales += float(sale.strip(“$”))

print(total_sales)

ERROR
Traceback (most recent call last):
File “script.py”, line 141, in
total_sales += float(sale.strip(“$”))
ValueError: could not convert string to float: ‘\n$17.98’

Any thoughts?

It’s difficult to tell w/o formatted code. Can you please format your code by selecting the “</>” button in the menu?

1 Like