Hi,
I have a question related to the thread shed problem in Learn python 3.
Exercis here: https://www.codecademy.com/courses/learn-python-3/projects/thread-shed
I understand in the below example why we need a nested loop, since we have multiple of string in every array which then is a part of a bigger array:
daily_sales_replaced = daily_sales.replace(";,;", "+")
daily_transactions = daily_sales_replaced.split(",")
daily_transactions_split = []
for sale in daily_transactions:
daily_transactions_split.append(sale.split("+"))
transactions_clean = []
for sale in daily_transactions_split:
transaction_clean = []
for transaction in sale:
transaction_clean.append(transaction.replace("\n", "").strip())
transactions_clean.append(transaction_clean)
However, as you can se my code below, why do I need a nested loop to perfore the split of the colors?
The array constist of multiple string, but the string we which to split is already called with “sales” to my understand as it is one string and we want it to split to 2 or more depending on the number of “&”
customer = []
sales = []
thread_sold = []
for transaction in transactions_clean:
customer.append(transaction[0])
sales.append(transaction[1])
thread_sold.append(transaction[2])
total_sales = 0
for sale in sales:
total_sales += float(sale.strip("$"))
thread_sold_split = []
for sale in thread_sold:
for color in sale.split("&"):
thread_sold_split.append(color)
def color_count(color):
count = thread_sold_split.count(color)
return count
colors = ['red', 'yellow', 'green', 'white', 'black', ' blue', 'purple']
for color in colors:
print("Thread shed sold {0} of {1} threads today".format(color_count(color), color))
Could someone be so kind to elaborate a bit on it for me to understand why this is the case?