Python method equivalent to racket's build-vector

I am looking for a method that can do exactly that in python but with lists instead of vectors:

(build-vector n (lambda _ (build-vector n (lambda _ (my-random n)))))

This is what I came up with (but with a 2D list)

   table = []
    for i in range(n):
        trans = [(random n) for i in range(n)]
        table = table + [trans]

But they give different results. I'm not sure where it goes wrong, any help appreciated, thank you!

Answers


I'm not familiar with racket but build-vector looks like a list comprehension or map:

Racket: (build-vector n proc)
Python: [proc(i) for i in range(n)]
Python: map(proc, range(n))

So it looks like you are building a nxn matrix of random numbers:

Racket:

(build-vector n (lambda _ (build-vector n (lambda _ (my-random n)))))

Python:

[[my_random(n) for _ in range(n)] for _ in range(n)]

So using random.randrange for my_random:

>>> from random import randrange as my_random
>>> n = 5
>>> [[my_random(n) for _ in range(n)] for _ in range(n)]
[[1, 2, 2, 0, 3],
 [3, 0, 2, 1, 3],
 [3, 1, 3, 0, 0],
 [0, 0, 2, 4, 4],
 [1, 0, 1, 4, 1]]

Need Your Help

Check for split keyboard

ios ipad cocoa-touch ios5 keyboard

As many of you know iOS 5 introduced a slick split keyboard for thumb-typing. Unfortunately, I have some UI that is dependent on the normal full-screen keyboard layout. One of my view controllers

DataReader IndexOutofRangeException was unhandled by user code

c# asp.net sqldatareader datareader

I ran into another issue again. I was trying to get data from the database using DataReader but I got the error when i was testing my code. Can anyone help me out? The error occurred at this line: