Don’t really grasp where does the difference come from?

Hi! I have finished the excercise and got the correct prints before looking up the solutions. The way I have done some of the steps is different than what the description was suggesting to do. In particular I have been trying to do step 11 as suggested, but was failing and decided to do it like this:

customers = transactions_clean[0::4]
sales =transactions_clean[1::4]
thread_sold = transactions_clean[2::4]

After finishing the excercise I have skimmed through the YouTube video and tried doing:
customers =
sales =
thread_sold =
for transaction in transactions_clean:
customers.append(transaction[0])
sales.append(transaction[1])
thread_sold.append(transaction[2])

But what I get printed after this are the first, second and third characters in transaction. So I guess it is string vs list problem, but I don’t really grasp where does the difference come from?

This is the full code I wrote for the excercise(with the 2nd method on step 11 omitted by #):

daily_sales_replaced = daily_sales.replace(";,;", ";")
daily_transactions = daily_sales_replaced.split(",")
daily_transactions_split = []
for i in daily_transactions:
  daily_transactions_split.append(i.split(";"))

transactions_clean = []
for i in daily_transactions_split:
  for a in i:
    transactions_clean.append(a.strip())
customers = transactions_clean[0::4]
sales =transactions_clean[1::4]
thread_sold = transactions_clean[2::4]
#for transaction in transactions_clean:
#  customers.append(transaction[0])
#  sales.append(transaction[1])
#  thread_sold.append(transaction[2])
#print(customers)
#print(sales)
#print(thread_sold)

#Total value
total_sales = 0
for i in sales:
  value = float(i.strip("$"))
  total_sales += value
#print(total_sales)
#How much thread of specific color
thread_sold_split = []
for i in thread_sold:
  if i.find("&") == -1:
    thread_sold_split.append(i)
  else:
    b = i.split("&")
    for c in b:
    	thread_sold_split.append(c)
#print(thread_sold_split)
def color_count(color):
  count = 0
  for i in thread_sold_split:
    if i == color:
      count += 1
  return count
#print(color_count("white"))

colors = ['red','yellow','green','white','black','blue','purple']
for i in colors:
  x=color_count(i)  
  print("Thread Shed sold {count} thread of {color} thread today.".format(color = i, count = x))
    

Is what I wrote wrong in some way?
Thanks

1 Like

No, you did a great job. Just make sure that you understand how the code in the video works.

In Python, the magic of for-in means that we often do not need to use indexing at all, or if we use it, we often are able to omit one level of indexing in nested lists. It’s rare to see slicing used to break them down. (In plain Python, that is; in Numpy & Pandas it’s quite common!)


Anyway,

Yes.

“Your” transactions_clean list is a list of strings:
['Edith Mcbride', '$1.21', 'white', '09/15/17', 'Herbert Tran'... etc. ]
so one transaction from transactions_clean is a string:
'Edith Mcbride'
… making transaction[0], transaction[1], etc, separate characters, ‘E’, ‘d’, ‘i’, etc.

Whereas “their” transactions_clean list is a list of lists of strings:
[['Edith Mcbride', '$1.21', 'white', '09/15/17'], ['Herbert Tran',...etc], etc]
… so one transaction is a list:
[['Edith Mcbride', '$1.21', 'white', '09/15/17']
… making transaction[0] a string: 'Edith Mcbride'

1 Like

Hi noispaxen.

I hope you can help me a little bit.

‘thread_sold_split =
for i in thread_sold:
if i.find(“&”) == -1:
thread_sold_split.append(i)
else:
b = i.split(“&”)
for c in b:
thread_sold_split.append(c)’

In this code that you wrote, can you explain me the “-1” after che t i.find(“&”)? Why have you put that one? In that case i prefered to write this, although, I believe that It is not right.

‘thread_sold_split =
for x in thread_sold:
for i in x:
if “&” not in i:
thread_sold_split.append(i)
elif “&” in i:
thread_sold_split.append(i.split(“&”))
print(thread_sold)’

Thank you for you time!

hey guys, I coded a correct code that get the job done perfectly, I uploaded it at GitHub, who ever have an update or another way to code the solution for the given problem is welcome at GitHub