#can someone who is proficient in Python
1 messages · Page 1 of 1 (latest)
I understand it as: freq is a fixed-sized array of arrays, where the size is that of the length of nums
so this technique is called list comprehension right instead of the array.
We're just creating len(nums) + 1 deepcopies of empty arrays insides of freq
since freq only has 1 layer
this is a 1 by (len(nums) + 1) array
so what do I mean by deepcopy
well
we can look at an example here
n = 10
grid1 = [[]] * n
grid2 = [[] for _ in range(n)]
here I created 2 arrays
both dimensions are 1 by n
by what is really the difference?
what do you think might happen if I do
grid1[0].append(1)
grid2[0].append(1)
grid1 will become
[[1], [1], [1], [1], [1], [1], [1], [1], [1], [1]]
and grid2 is
[[1], [], [], [], [], [], [], [], [], []]
by doing
grid1 = [[]] * n
we're creating n shallow copies
for which each arrays in grid1
shares the same address
meaning
if we modify one of the created arrays
then we're modifying all
while, in comparison we're creating a deepcopy in grid2, each individual array is its own identity
wow, that is crazy LOL. thank you. so many details.