需要涉及数组和随机位置的基本 Python 编程帮助 [英] Basic Python programming help needed involving arrays and random locations

查看:48
本文介绍了需要涉及数组和随机位置的基本 Python 编程帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑一个 100X100 的阵列.

Consider a 100X100 array.

  1. 生成一个包含数千个随机位置的数组这样的数组,例如(3,75) 和 (56, 34).
  2. 计算您的随机位置之一落在 15 以内的频率任何(直线)边缘的像素

我正在尝试做上述问题,以帮助我学习编程语言 Python,我是编程新手.

I am trying to do the above question in order to help me to learn the programming language Python, i am new to programming.

这是我目前所拥有的:

from __future__ import division
from pylab import *
import math as m
from numpy import *

array1=[]
nMax=1001
n=1
while n<nMax:
    array1.append(random.randint(1, 101))
    n=n+1
array1=array(array1)
array1=array1.reshape(1000,1)
print array1

array2=[]
nMax=1001
n=1
while n<nMax:
    array2.append(random.randint(1, 101))
    n=n+1
array2=array(array2)
array2=array2.reshape(1000,1)
print array2

这会生成两个包含 1000 个随机整数的单列数组.一个数组是我的随机 x 坐标集,另一个数组是我的随机 y 坐标集.我正在尝试合并两个数组,以便我有一个 x 和 y 坐标数组,我该怎么做?一旦我有了一个随机 x 和 y 坐标数组,我就可以使用 if、else 语句来完成问题吗?例如如果 xcoordinate>85 or xcoordinate<15 or ycoordinate>85 or ycoordinate<15 然后将一个数字附加到一个空列表,然后使用列表的长度作为计数器?

This produces two single column arrays of 1000 random integers. One array is my random set of x coordinates and the other array my random set of y coordinates. I am trying to merge the two arrays so that i have one array of x and y coordinates, how do i do this? Once i have a single array of random x and y coordinates will i be able to use if, else statements to complete the question? e.g. if xcoordinate>85 or xcoordinate<15 or ycoordinate>85 or ycoordinate<15 then append a number to an empty list, then use the length of the list as a counter?

请问有人可以就这个问题给我一些指导吗?我这样做对吗?如果是这样,我怎么不是?如果是这样,我该如何继续?

Please can someone give me some guidance on this question? Am i doing it the right way? If so how am i not? If so, how do i continue?

...感谢大家的帮助.

. . . Thanks for everyones help.

我现在已经完成了这两部分,但现在我面临着挑战,作为前一部分的扩展:

I have now managed to complete these two parts, but now i have been challenged to do this as an extension to the previous:

iii) 计算您的随机位置之一落在(任一侧)15 个像素范围内的频率一个半径为 50 像素的圆,并将结果写入屏幕,例如一般数组中 N% 的位置落在此区域内 [N 是介于 0和 100] 请注意,角中的坐标在圆的 15 像素内,然后它们应该包含在其中(如果它们相距超过 15 个像素,那么它们不应该是).

iii) Calculate how often one of your random locations falls within (either side) 15 pixels of a circle with a radius of 50 pixels, and write the result to the screen e.g. On average N% of the locations in the array fall in this region [N being a number between 0 and 100] Note that coordinates in the corners are within 15 pixels of the circle, then they should be included in this (if they are are more than 15 pixels away, then they shouldn’t be).

iv) 添加向用户请求阵列中的位置然后发出警报的代码如果该位置属于该区域,例如警告:您选择的位置落在靠近圆的边缘.

iv) Add code that requests a location within your array from the user and then alerts them if that location falls that region, e.g. Warning: your chosen location falls near the edge of the circle.

我会怎么做呢?我什至不知道从哪里开始.有人可以给我一些指点吗?谢谢

How would i even go about this? I have no idea of even where to begin. Can anybody give me some pointers please? Thanks

推荐答案

可以跳过实际生成数组直接进入频率计算:

首先需要注意的是,由于 x 和 y 坐标都是完全随机的:

First of all, it should be noted that since both the x and y coordinates are completely random:

我们有规则:

P(A or B) = P(A) + P(B) - P(A and B)

所以我们可以说

P(X or Y in 15) = P(X in 15) + P(Y in 15) - P(X and Y in 15)

因为暗示 X 和 Y 是从 0 到 99(或从 1 到 100)均匀随机的,我们可以假设

Since it's implied that X and Y are uniformly random from 0 to 99 (or from 1 to 100), we can assume that

P(X in 15) = P(Y in 15)

所以整个事情就变成了

P(X or Y in 15) = 2P(X in 15) - P(X in 15)^2

现在,我们只需要计算P(X in 15).观察到 X 只能取整数值,所以函数 f(x) = |X - 49.5|- 对于 x = 0..14x = 85..99,35 将大于零.(我们从零开始索引,但这相当于从 1...100 开始.

Now, we just need to calculate P(X within 15). Observe that X can only take integer values, so the function f(x) = |X - 49.5| - 35 will be greater than zero for x = 0..14 and x = 85..99. (We're indexing from zero, but it's the equivalent to going from 1...100).

所以,代码:

from random import randrange

N = 3000
P_x_within_15 = sum(abs(randrange(100) - 49.5) - 35 > 0 for _ in range(N)) / float(N)

print "Frequency of X or Y within 15 pixels of edge:",
print 2 * P_x_within_15 - P_x_within_15 ** 2

您可能已经注意到,我们实际上跳过了创建数组的步骤,只是将生成的随机数的布尔值(True/False with True = 1, False = 0)结果相加.

As you may have noticed, we skipped actually creating an array, and just added up the boolean (True/False with True = 1, False = 0) results of the random numbers as they were generated.

编辑 - 实际创建一个数组

要为 100 x 100 矩阵创建随机坐标数组:

To create an array of random coordinates for a 100 x 100 matrix:

使用 numpy 代码将是:

from random import randrange
N = 3000
coords_array = array([randrange(100) for _ in range(2 * N)]).reshape(N, 2)

使用纯python,那就是:

Using pure python, it would be:

from random import randrange
N = 3000
coords_list = [(randrange(100), randrange(100)) for _ in range(N)]

这里需要注意几点:

  1. 在python中,索引从零开始排序.randrange(100) 会给你一个 0 到 99 之间的数字,包括 0 和 99.

  1. In python, indices are sequenced from zero. randrange(100) will give you a number between 0 and 99, inclusive.

N 只是我们希望生成的坐标数

N is simply the number of coordinates we wish to generate

术语点:在python中,方括号表示的数据结构是list,而不是数组(尽管它们等同于其他编程语言中的数组).在上面的代码中, array1 是一个 python list,而不是一个 array (这是一种不同的数据结构).Counterpoint:numpy 然而,广泛使用数组.在您的代码中,当您执行 array(array1) 时,您实际上是在创建一个 numpy 数组.

Terminology point: In python, the data structures represented by square brackets are lists, not arrays (even though they are equivalent to arrays in other programming languages). In your code above array1 is a python list, not an array (which is a different data structure). Counterpoint: numpy however, extensively uses arrays. In your code, when you do array(array1), you are, in fact, creating a numpy array.

方括号之间的内容称为列表推导.它在语义上等同于:

The stuff between the square brackets is called a list comprehension. It is semantically equivalent to:

<小时>

coords_list = []
for _ in range(N):
    coords_list.append((randrange(100), randrange(100)))

这篇关于需要涉及数组和随机位置的基本 Python 编程帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆