通过使用一系列角度python创建一个圆形列表

本文介绍了通过使用一系列角度python创建一个圆形列表的处理方法,对大家解决问题具有一定的参考价值

问题描述

我有一个列表列表,其中包含角度集的下限和上限

I have a list of lists which contains the lower and upper limit of sets of angles

类似

[[1,22],[2,24]...[359,15],[360,21]]

[[1,22],[2,24]...[359,15],[360,21]]

总共 360 个元素

现在我想检查从 1 到 360 的每个角度列表中包含该角度的元素

Now I want to check for each angle from 1 to 360 the elements in the list that contains that angle

我正在考虑使用下限和上限来创建列表的所有元素 rangenp.arange(lower,upper) 并检查是否角度包含在内,但是np.arange在lower高于upper时生成空列表

I was thinking about using the lower and upper limits to create all the elements of the list with range or np.arange(lower,upper) and check if the angle is contained, but np.arange generates empty list when lower is higher than upper

for i in range(1,361):
    sel=[]
    for coe in coef:
        if i in range(coe[0],coe[1]):
            sel.append(coe)

我也尝试了 itertoolscycle 但我不知道在这种情况下如何应用它

I tried also with itertools and cycle but i do not see how it can be applied in this case

推荐答案

执行此操作的标准方法是使用模数.这个答案 by sschuberth 展示了如何在 C/C++ 中做到这一点.

The standard way to do this is to use modulus. This answer by sschuberth shows how to do this in C/C++.

这是一个 Python 实现和测试.Python 中的代码更简单,因为在 Python 中 a % b 总是与 b 具有相同的符号.

Here's a Python implementation and test. The code is simpler in Python, because in Python a % b always has the same sign as b.

测试代码以 60 的步长循环从 0 到 360 度的所有角度对,对于 ab,测试所有角度 x 从 0 到 360 度,步长为 30.扇区从 a 开始并扫过 b.因此扇区 (60, 120) 包含 60°,但 (120, 60) 包含 300°.

The test code loops through all pairs of angles from 0 to 360 degrees in steps of 60, for a and b, testing all angles x from 0 to 360 degrees in steps of 30. The sector starts at a and sweeps through to b. Thus the sector (60, 120) contains 60°, but (120, 60) contains 300°.

如果 x 在扇区内(包括端点),它将被添加到 result 列表中.

If x is within the sector (including the endpoints) it gets added to the result list.

def in_angle_interval(x, a, b):
    return (x - a) % 360 <= (b - a) % 360

# test 

for a in range(0, 420, 60):
    for b in range(0, 420, 60):
        result = [x for x in range(0, 390, 30) if in_angle_interval(x, a, b)]
        print('{:3}-{:3} {}'.format(a, b, result))

输出

  0-  0 [0, 360]
  0- 60 [0, 30, 60, 360]
  0-120 [0, 30, 60, 90, 120, 360]
  0-180 [0, 30, 60, 90, 120, 150, 180, 360]
  0-240 [0, 30, 60, 90, 120, 150, 180, 210, 240, 360]
  0-300 [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 360]
  0-360 [0, 360]
 60-  0 [0, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360]
 60- 60 [60]
 60-120 [60, 90, 120]
 60-180 [60, 90, 120, 150, 180]
 60-240 [60, 90, 120, 150, 180, 210, 240]
 60-300 [60, 90, 120, 150, 180, 210, 240, 270, 300]
 60-360 [0, 60, 90, 120, 150, 180, 210, 240, 270, 300, 330, 360]
120-  0 [0, 120, 150, 180, 210, 240, 270, 300, 330, 360]
120- 60 [0, 30, 60, 120, 150, 180, 210, 240, 270, 300, 330, 360]
120-120 [120]
120-180 [120, 150, 180]
120-240 [120, 150, 180, 210, 240]
120-300 [120, 150, 180, 210, 240, 270, 300]
120-360 [0, 120, 150, 180, 210, 240, 270, 300, 330, 360]
180-  0 [0, 180, 210, 240, 270, 300, 330, 360]
180- 60 [0, 30, 60, 180, 210, 240, 270, 300, 330, 360]
180-120 [0, 30, 60, 90, 120, 180, 210, 240, 270, 300, 330, 360]
180-180 [180]
180-240 [180, 210, 240]
180-300 [180, 210, 240, 270, 300]
180-360 [0, 180, 210, 240, 270, 300, 330, 360]
240-  0 [0, 240, 270, 300, 330, 360]
240- 60 [0, 30, 60, 240, 270, 300, 330, 360]
240-120 [0, 30, 60, 90, 120, 240, 270, 300, 330, 360]
240-180 [0, 30, 60, 90, 120, 150, 180, 240, 270, 300, 330, 360]
240-240 [240]
240-300 [240, 270, 300]
240-360 [0, 240, 270, 300, 330, 360]
300-  0 [0, 300, 330, 360]
300- 60 [0, 30, 60, 300, 330, 360]
300-120 [0, 30, 60, 90, 120, 300, 330, 360]
300-180 [0, 30, 60, 90, 120, 150, 180, 300, 330, 360]
300-240 [0, 30, 60, 90, 120, 150, 180, 210, 240, 300, 330, 360]
300-300 [300]
300-360 [0, 300, 330, 360]
360-  0 [0, 360]
360- 60 [0, 30, 60, 360]
360-120 [0, 30, 60, 90, 120, 360]
360-180 [0, 30, 60, 90, 120, 150, 180, 360]
360-240 [0, 30, 60, 90, 120, 150, 180, 210, 240, 360]
360-300 [0, 30, 60, 90, 120, 150, 180, 210, 240, 270, 300, 360]
360-360 [0, 360]

<小时>

这是使用问题中的数据进行的测试.


And here's a test using the data in the question.

coef = [[1,22], [2,24], [359,15], [360,21]]
print(coef)
for x in range(0, 361):
    sel = [coe for coe in coef if in_angle_interval(x, coe[0], coe[1])]
    if sel:
        print('{:3} {}'.format(x, sel))

输出

[[1, 22], [2, 24], [359, 15], [360, 21]]
  0 [[359, 15], [360, 21]]
  1 [[1, 22], [359, 15], [360, 21]]
  2 [[1, 22], [2, 24], [359, 15], [360, 21]]
  3 [[1, 22], [2, 24], [359, 15], [360, 21]]
  4 [[1, 22], [2, 24], [359, 15], [360, 21]]
  5 [[1, 22], [2, 24], [359, 15], [360, 21]]
  6 [[1, 22], [2, 24], [359, 15], [360, 21]]
  7 [[1, 22], [2, 24], [359, 15], [360, 21]]
  8 [[1, 22], [2, 24], [359, 15], [360, 21]]
  9 [[1, 22], [2, 24], [359, 15], [360, 21]]
 10 [[1, 22], [2, 24], [359, 15], [360, 21]]
 11 [[1, 22], [2, 24], [359, 15], [360, 21]]
 12 [[1, 22], [2, 24], [359, 15], [360, 21]]
 13 [[1, 22], [2, 24], [359, 15], [360, 21]]
 14 [[1, 22], [2, 24], [359, 15], [360, 21]]
 15 [[1, 22], [2, 24], [359, 15], [360, 21]]
 16 [[1, 22], [2, 24], [360, 21]]
 17 [[1, 22], [2, 24], [360, 21]]
 18 [[1, 22], [2, 24], [360, 21]]
 19 [[1, 22], [2, 24], [360, 21]]
 20 [[1, 22], [2, 24], [360, 21]]
 21 [[1, 22], [2, 24], [360, 21]]
 22 [[1, 22], [2, 24]]
 23 [[2, 24]]
 24 [[2, 24]]
359 [[359, 15]]
360 [[359, 15], [360, 21]]

这篇关于通过使用一系列角度python创建一个圆形列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2

WordPress使用python会话上载文件

我需要上传图像到wordpress编程,理想情况下没有安装额外的插件。不过,我对涉及插件的最佳实践持开放态度。到目前为止,我已经能够使用会话登录和移动站点,但是当我尝试将文件上载到媒体时-新建.php或异步-上传.php我收到以下错误消息The file is a test text file with a single line (also the upload limit on the site is 1GB) so it\"s not the common file size limit. This ...

日期:2021-08-21 05:00:01 浏览:801

压缩序列化的Python数据最节省空间的方法是什么?

本文介绍了压缩序列化的Python数据最节省空间的方法是什么?的处理方法,对大家解决问题具有一定的参考价值 问题描述 发件人the Python documentation:默认情况下,Pickle数据格式使用相对紧凑的二进制表示。如果您需要最佳大小特性,您可以高效地压缩酸洗数据。我将在一个运行了几个小时的过程结束时序列化...

日期:2022-06-23 08:50:34 浏览:773

在Python中,有没有一种方法可以将一个单词分割成等分?

本文介绍了在Python中,有没有一种方法可以将一个单词分割成等分?的处理方法,对大家解决问题具有一定的参考价值 问题描述 几周前我问了这个问题,得到了答案this is the original post但我需要将输出分成相等的部分,无论字符串的长度如何,所以在我发布的第一个帖子中,我得到了这个答案,它很好地工作了,这要...

日期:2022-06-24 06:52:06 浏览:507

如何在 Google AppEngine Python37 中获取凭据

本文介绍了如何在 Google AppEngine Python37 中获取凭据的处理方法,对大家解决问题具有一定的参考价值 问题描述 我在 AppEngine Python3.7 标准中启动了新应用.I started new app in AppEngine Python3.7 stadard.我正在尝试使用以下代码段...

日期:2022-06-24 09:00:27 浏览:552

为什么 python 字符串和元组是不可变的?

本文介绍了为什么 python 字符串和元组是不可变的?的处理方法,对大家解决问题具有一定的参考价值 问题描述 我不确定为什么字符串和元组是不可变的;使它们不可变的优点和缺点是什么?I am not sure why strings and tuples were made to be immutable; what ar...

日期:2022-06-24 09:00:30 浏览:918

使用 Python 解析 Gmail 并将所有早于日期的内容标记为“已读"

本文介绍了使用 Python 解析 Gmail 并将所有早于日期的内容标记为“已读"的处理方法,对大家解决问题具有一定的参考价值 问题描述 长话短说,我创建了一个新的 gmail 帐户,并将其他几个帐户关联到该帐户(每个帐户都有 1000 条消息),我正在导入这些帐户.所有导入的邮件都以未读的形式到达,但我需要它们显示为已...

日期:2022-06-24 10:00:29 浏览:810

了解python线程错误

本文介绍了了解python线程错误的处理方法,对大家解决问题具有一定的参考价值 问题描述 阅读http://bugs.python.org/msg160297,我可以看到Stephen White编写的一个简单脚本,它演示了该异常是如何导致python线程出错的Exception AttributeError: Attri...

日期:2022-06-24 21:00:28 浏览:966

从python调用url时获取“错误"的页面源

本文介绍了从python调用url时获取“错误"的页面源的处理方法,对大家解决问题具有一定的参考价值 问题描述 尝试从网站检索页面源时,得到的文本与通过 Web 浏览器查看相同页面源时完全不同(且更短).Trying to retrieve the page source from a website, I get a c...

日期:2022-06-25 01:00:31 浏览:629

基于 Python 类的装饰器,带有可以装饰方法或函数的参数

本文介绍了基于 Python 类的装饰器,带有可以装饰方法或函数的参数的处理方法,对大家解决问题具有一定的参考价值 问题描述 我见过很多 Python 装饰器的例子:I've seen many examples of Python decorators that are:函数样式装饰器(包装函数)类样式装饰器(实现 __...

日期:2022-06-25 04:00:31 浏览:962

用python解析outlook .msg文件

本文介绍了用python解析outlook .msg文件的处理方法,对大家解决问题具有一定的参考价值 问题描述 环顾四周,没有找到满意的答案.有谁知道如何使用 Python 解析 Outlook 中的 .msg 文件?Looked around and couldn't find a satisfactory answer...

日期:2022-06-25 06:00:30 浏览:655