Stuck on Python Project: "Thread Shed"


#1

For number 8 (see below) I can’t figure out how to keep the transactions together, my code simply takes every data entry and puts them all together in the same string, thus losing the individual transactions

Any help appreciated

" 8.

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 ."

https://www.codecademy.com/paths/computer-science/tracks/cspath-python-objects/modules/cspath-python-strings/projects/thread-shed


Thread Shed - Task
#2

Line 131 has too much indentation. It should be in the outer loop.

But this leads to another problem. Only a single value, rather than a complete transaction. The way I worked this out was to use indexes, rather than values.

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

Then append each updated row to the list.


#3

Thank you for your response!

I’m very new to Python and I don’t think I understand your suggestion - perhaps I need to revisit a few lessons!

Below is my code from trying your solution - I’m getting “IndexError: list index out of range”

I’d be grateful for any further advice you’re able to give.


#4

for col in range(len(daily_transactions_split)):

Our row is iterating through that list. Recall that row is also a list.


#5

Sorry, I’m still not getting it. Maybe I should try again with a clear head tomorrow.

What should my code be?


#6

The outer loop iterates over the values in the daily_transactions_split list, the inner loop iterates over the indexes of the row.

  for col in range(len(row)):

We don’t append a column to the transactions_clean, list, but the whole row. That’s why it has to be outside of the inner loop.

transactions_clean.append(row)

transactions_clean.append(row)


#7

Perfect got it - thank you so much for your help and patience, very much appreciated!


#8

Hello! This is my own solution. It may not be elegant, but it works.
transactions_clean=[]
for tr in daily_transactions_split:
x=[]
for data in tr:
data=data.strip()
x.append(data)
transactions_clean.append(x)


#9

Yes it works, but it also creates a second data structure that in the end would need to be garbage collected so as to return resources. A minor concern here, but still…

The earlier example is not so elegant, either, but it works in-place, and doesn’t leave a trace. MInd this whole exercise is eating up a tonne of memory since we are progressively adding more structures. It would not be so in a production environment.

This is what makes functions most invaluable, in terms of memory management. By focusing all the intermediate steps within functions, and keeping only the final results, we effectively give back the memory the processes used.

Of course, this discussion is for another day, so keep forging ahead and learn everything you can. Nothing is too small or trivial to study, microscopically.


#10

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