Thread Shed - Task


#1

Hi,

my code needs some tweaking as Im not getting the result I need to continue with my Task called Thread Shed in Learn: Python Strings. This issue has been addressed before, but the proper solution is not clear to me. Previous solution

There are instructions in the Task which instrucs me to do this:

It looks like each data item has inconsistent whitespace around it. First, define an empty list transactions_clean .

Now, Iterate through daily_transactions_split and for each transaction iterate through the different data points and strip off any whitespace.

Add each of these cleaned up transactions to the new list transactions_clean .

Problem with my code below(the first part of code) that it does not work the way I was hoping to. No Indentation error. Nothing. It just displays the same result like “daily_transactions_split”. Like there was no operation.

The second attempt (the code under the first code) does the job of removing whitespace, but also destroys the transaction list in the process and all the elements appear in one big list and I cannot therefore finish this excercise.

Pls be so kind and offer the comprehensive solution. Im sure it´s simple I just cant seem to see it for the last 4 hours.

daily_sales_replaced = daily_sales.replace(";,;","--")
daily_transactions =  daily_sales_replaced.split(",")

 
daily_transactions_split = []
for transaction in daily_transactions:
  transcation_splitted = transaction.split("--")
  daily_transactions_split.append(transcation_splitted)
  
transactions_clean = []
for transaction in daily_transactions_split:
  for element in transaction:
    element.strip()
  transactions_clean.append(transaction)
    
    
print(transactions_clean)

transactions_clean = []
for transaction in daily_transactions_split:
  for element in transaction:
    transactions_clean.append(element.strip())

#2

Please post the URL of this lesson.


#3

As @mtf said, a link to the exercise would be helpful.

Either that, or we need to know what the value of daily_sales is to recreate your code properly.


#5

Could you post the URL of this exercise? That would help two people, right off.


#6

Hi, sry. There you go: Excercise


#7

Hi there

I don’t code (yet) but I do proof-read and I can’t help noticing that there is a typo in this line:

transcation_splitted = transaction.split("--")

(‘transcation’ not ‘transaction’)

could it be that this variable is causing the error? Just thought it was worth pointing out :slight_smile:


#8

Hi thirst,

it does not, because “transcation_splitted” is variable defined within function only and is used correctly in the rest of the function:

for transaction in daily_transactions:
  **transcation_splitted** = transaction.split("--")
  daily_transactions_split.append(**transcation_splitted**)

it does not effect the result, but thank you for pointing that out. I have troubles with typos as im writting with all my 10 fingers super fast.


#9

Hi all,

I finally found the solution by luckily bumping into some old post:

the proper code is this:

transactions_clean = []
for transaction in daily_transactions_split:
  transactions_clean.append(transaction)
  for element in range(0,len(transaction)):
    transaction[element] = transaction[element].strip()

this one small task created various possibilities to fail from incorrect syntax, indentation, list uniterability, list has not atribute strip and int has no attribute strip.

The solution gave me good time to think about what are the limits of coding. I tried to everything in one fell swoop, but sometimes these tiny smalls steps are necessary.


#10

Do you know why the above even works, given that the mutation is done after the object is appended, and not before?

Consider,

transactions_clean = []
for row in daily_transactions_split:
  for col in range(len(row)):
    row[col] = row[col].strip()
  transactions_clean.append(row)

#11

Hi mtf,

I think I understand it. For me synthactically in step by step it would make more sense to use append.(transaction) as a last item, because I want to clean white space first and than append.

BUT

the computer thinks differently. In my previous not working solution the loop would .append “element-times”, because .append was indented in second nested loop. Also .strip method works when saved in variable. That way I could update the transaction. I mean I could go on, what else was wrong with it…but i know now. I just need to take my time and go though the logic what I wrote more meticulously next time.

The proposed solution, that you offered is also working, and I understand why it doesnt throw out IndentationError. The only difference I see is that first the transaction is appended and then update in found solution and in your solution its first updated and than appended. I cant say one solution is better thant the other.


#12

The question is, “why does your solution also work?” This is not a trick question. There is a definite reason.


#13

I dont know the proper answer, but the way I see it, as long as something is part of the correct loop, it should return the same result.

Im not talking about variables, which needs to be defined first before used.

Can you tell me please the “real reason” why also my solution works?

Thank you.


#14

So you know, this is only part of the reason, given the scope of the outer loop body. transaction and row are in outer scope.

It has to do with how objects are referenced. In both examples, the same object is pointed to by both references, transaction and transaction_clean, only your row is appended before the mutation, and mine is appended after. If we could look at the id of the object being referenced, we would find it to be the same.

There is only one object in memory, with, in either case two pointers. I’ll see if I can work up a good example of what’s happening here. More later.

Bottom line, we should not use solutions from the forums if we cannot fully explain them. This is an attempt to learn from rote which doesn’t work that well in programming since we need our brains to come up with solutions and cannot rely upon memory for this. Thought, reasoning and logic must come naturally and this only happens when we push ourselves to do those things.


#15
>>> array = [['one','two','three'],['four','five','six'],['seven','eight','nine']]
>>> spare = []
>>> for row in array:
	print (id(row))
	for col in range(len(row)):
		row[col] = row[col].upper()
	spare.append(row)
	print (id(spare[spare.index(row)]))

	
48301720
48301720
48237584
48237584
48237544
48237544
>>> spare = []
>>> for row in array:
	print (id(row))
	spare.append(row)
	for col in range(len(row)):
		row[col] = row[col].upper()
	print (id(spare[spare.index(row)]))

	
48301720
48301720
48237584
48237584
48237544
48237544
>>> 

#16

I was pushing quite hard yesterday for hours. Didnt really had the desired effect.

I suspect that only when you truly understand the basics and fundamentals of that you work with, than you can use it properly and logic will only help you get there. With sound logic and shaky fundamentals, it´s only gonna come to naught.

I tried that code of yours. It´s giving me different int as results and also Im not familiar with id function.

But I understand most parts of explanation that you provided. Thank you. I agree that everyone should come to their own solutions, not copying them. My solutions are usually more complicated than necessary :slight_smile:


#17

It will give different values in different sessions and on different machines. The main point is that they are identical pairs even though we are accessing different lists. The object they point to in memory is the same.

id(object)

This is not going to be covered in this course so don’t read too much into it.

>>> a = 5
>>> b = a
>>> id(5)
1786984272
>>> id(a)
1786984272
>>> id(b)
1786984272
>>> 

Notice that the object 5 is what actually has the id. a and b are pointers to that same object. If there are ten variables all with the value of 5, they will all have the same id since there is only the one number object in memory.


#18

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.