TIL: Python Comprehensions Make Matrices Work

Hello world!

First, a super brief introduction: my name is Ry, and I’m midway through a web development bootcamp in Chicago. I was a consultant for the last 5+ years; I wrote a ton of code, but primarily for data analysis/data manipulation in SAS and bending various Microsoft products to my will. Now I’m doing this bootcamp and also boning up on my Python in the hopes of doing some kind of software development/data engineer/????? something other than consulting. I’m still a neophyte (at best) at anything that isn’t SAS or VBA, but I’m working hard and learning a lot and getting better! I’m going to be using this as a blog for things I learned, cool stuff I ran into, projects I’m particularly excited about, etc.

As part of my efforts to git gud, I’ve been doing Advent of Code for the first time! Which is how I ended up finding out something really weird about how Python handles creating lists of lists.

The Problem

Day 3 of Advent of Code involves a bunch of people trying to divide up a sheet of cloth, but they all have different ideas of where to cut and how much to cut. To translate that into less cute and story-relevant terms, I solved this by creating a 2D matrix and finding the coordinates of all overlapping sections within the matrix from the coordinates provided.

Essentially, what I wanted to create was as follows:

[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]

Enter fullscreen mode Exit fullscreen mode

where each 0 represents a single square inch of fabric.

The Solution: Take 1

I cycled through all the coordinates provided in the input; at each square inch covered by a slice, I added +1 to the corresponding 0 in my matrix. By the end, every element in the list with a value of 2 or more was covered by overlapping slices.

This is all quite straightforward; once I had gotten through the spatial reasoning required to understand the question in the first place, writing the code to cycle through everything was relatively trivial. What took me ages, however, was creating the matrix in the first place.

My first thought was to make a blank list. Or:

sleeve = [0] * 5
shirt = [sleeve] * 5

Enter fullscreen mode Exit fullscreen mode

which provides you with a list like so:

[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]

Enter fullscreen mode Exit fullscreen mode

To make sure my coordinates were working correctly (I don’t believe in regex if I don’t really literally have to), I typed in what I naively thought would work to change a single cell—

shirt[2][1] = 500

Enter fullscreen mode Exit fullscreen mode

Which, logically, provides:

[[0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0]]

Enter fullscreen mode Exit fullscreen mode

…Sure! Okay! Maybe there’s something funny as a result of multiplying sleeve by 5?

Let’s make this out of whole cloth, then —

The Solution: Take 2

shirt = [[0] * 5] * 5

[[0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0],
 [0, 0, 0, 0, 0]]

Enter fullscreen mode Exit fullscreen mode

so now it’s all be done in one step. Nothing weird could possibly have happened around the definition of “sleeve”, right?

[[0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0],
 [0, 500, 0, 0, 0]]

Enter fullscreen mode Exit fullscreen mode

…okay, sure! Let’s try something else —

The Solution: Third Time’s The Charm?

sleeve = []
shirt = []

for j in range(5):
    for i in range(1):
        sleeve.append(0)
    shirt.append(arr)

[[ 0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0],
  [0, 0, 0, 0, 0]]

shirt[2][1] = 500

[[ 0, 500, 0, 0, 0],
  [0, 500, 0, 0, 0],
  [0, 500, 0, 0, 0],
  [0, 500, 0, 0, 0],
  [0, 500, 0, 0, 0]]

Enter fullscreen mode Exit fullscreen mode

At this point, I went to the internet and asked what the heck is going on. The answer was, apparently, “use NumPy”, but I am stubborn and determined to figure it out on my own (mistake!).

I — actually still don’t quite know what’s going on. I suspect it has to do with pointers? And somehow the list “sleeve”, when it’s multiplied into the “shirt” list, is just copying in the single sleeve array instead of making copies of sleeve; I haven’t really managed to find any explanation, though, so if you know a ton about python and pointers and how this all works, please (please, for the love of god, this is driving me up the walls) let me know the explanation!

(Is it a pointer? I got the flu during the one (1) CS course I took in college and missed pointer week and I assume all my suffering is a result of that)

Anyway — comprehensions were introduced to me as, “basically a for loop, with some minor differences, which you don’t have to worry about”. Turns out the minor differences you don’t have to worry about are making the damn thing work.

The Solution: Sure Why Not!!!

shirt = [[0 for i in range(1000)] for j in range(1000)] 

Enter fullscreen mode Exit fullscreen mode

SEVERAL HOURS OF MY LIFE I WILL NEVER GET BACK.

原文链接:TIL: Python Comprehensions Make Matrices Work

© 版权声明
THE END
喜欢就支持一下吧
点赞7 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容