首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:将伪代码转换为代码

Python:将伪代码转换为代码
EN

Stack Overflow用户
提问于 2017-11-14 02:19:26
回答 1查看 1K关注 0票数 0

我试着对这些行进行编码:

代码语言:javascript
复制
copy the first {ceiling of} n/2 points of P to array Pleft
copy the same {ceiling of} n/2 points from Q to array Qleft
copy the remaining {floor of} n/2 points of P to array Pright
copy the same {floor of} n/2 points from Q to array Qright 
m = P[ ({ceiling of} n/2) - 1].x

我得到了:

代码语言:javascript
复制
mid = len(P) / 2

Pl = P[:mid]
Ql = Q[:mid]
Pr = P[mid:]
Qr = Q[mid:]
m = P[:mid - 1].x

但我不知道如何编码这一行:

代码语言:javascript
复制
copy all the points of array Q for which |x - m| < d into new array S[0..num - 1]

请帮帮忙。

EN

回答 1

Stack Overflow用户

发布于 2017-11-14 02:28:03

编辑:哎呀!忘了m是一个列表!

这就是清单理解的用途。因为m是一个列表,所以还需要使用python的zip函数。新数组的定义是:

代码语言:javascript
复制
S = [x for x, y in zip(Q, m) if abs(x - y) < d]

你得供应d。此外,为了完整起见:

代码语言:javascript
复制
num = len(S)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47276662

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档