Python:使用另一个列表顺序对列表进行排序,具有不同的长度,并且没有“排序”
我会正确解释这一点:
我所处的环境无法使用 python 内置函数(如“sorted”、“set”),无法声明方法,无法创建条件(if),也无法创建循环,除了:
可以调用方法(但每次只能调用一个方法,并将返回值保存在另一个变量上
foo python:item.sort(); #foo 变量采用 item.sort() 返回的值
bar python:foo.index(x);
并且可以进行列表理解
[item['bla'] for item in foo]
...我认为这对此没有帮助问题
我有一个“正确顺序”列表,具有以下值:
correct_order = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
我有一个“混乱顺序”列表,具有以下值:
messed_order = [55, 1, 44, 3, 66, 5, 4, 7, 2, 9, 0, 10, 6, 8]
嗯,我必须使用“正确顺序”的索引作为基础对“混乱顺序”列表重新排序。未包含在 Correct_order 中的其余项目的顺序并不重要。
像这样的事情可以解决(同样,除了我不能使用循环):
for item in correct_order:
messed_order[messed_order.index(item)], messed_order[correct_order.index(item)] = messed_order[correct_order.index(item)], messed_order[messed_order.index(item)]
并且会导致我想要的“ordered_list”:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 55, 66, 44]
那么,我该怎么做?
对于那些了解 zope/plone 的人,我在一个皮肤页面 (.pt) 上,它没有辅助 python 脚本(我认为这对于皮肤页面是不可能的,仅对于浏览器页面是不可能的。如果是的话,告诉我如何做,我会做)。
I'll to explain this right:
I'm in an environment where I can't use python built-in functions (like 'sorted', 'set'), can't declare methods, can't make conditions (if), and can't make loops, except for:
can call methods (but just one each time, and saving returns on another variable
foo python:item.sort(); #foo variable takes the value that item.sort() returns
bar python:foo.index(x);
and can do list comprehension
[item['bla'] for item in foo]
...what I don't think that will help on this question
I have a 'correct_order' list, with this values:
correct_order = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
and I have a 'messed_order' list, with this values:
messed_order = [55, 1, 44, 3, 66, 5, 4, 7, 2, 9, 0, 10, 6, 8]
Well, I have to reorder the 'messed_order' list, using the index of 'correct_order' as base. The order of the rest of items not included in correct_order doesn't matter.
Something like this would solve (again, except that I can't use loops):
for item in correct_order:
messed_order[messed_order.index(item)], messed_order[correct_order.index(item)] = messed_order[correct_order.index(item)], messed_order[messed_order.index(item)]
And would result on the 'ordered_list' that I want:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 55, 66, 44]
So, how can I do this?
For those who know zope/plone, I'm on a skin page (.pt), that doesn't have a helper python script (what I think that is not possible for skin pages, only for browser pages. If it is, show me how and I'll do it).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
很难回答,不知道什么是允许的,什么是不允许的。但是这个 O(N^2) 解决方案怎么样?
It's hard to answer, not knowing exactly what's allowed and what's not. But how about this O(N^2) solution?
在您的皮肤中创建一个
Script (Python)
对象并将其用作函数。 TALES 表达式受到限制是有原因的:它们只是帮助您创建 HTML 或 XML 标记,而不是执行全面的业务逻辑。更好的是,创建适当的浏览器视图并避免对通过网络可编辑代码施加的严格限制。另外,您歪曲或误解了 TALES。您可以使用内置方法,例如排序和设置。您可以使用 test(condition, iftrue, iffalse) 或旧的
condition 和 iftrue 或 iffalse
代替if
,但限制为iftrue< /code> 本身必须评估为 true。
更好的是,您可以通过
modules
字典访问一组有限的 Python 模块,例如modules['string']
。不过,您需要在文件系统 python 模块中进行额外的安全声明来扩展它。请参阅 TAL 文档的 Python TALES 表达式部分。请注意,其中列出的 TALES 可访问的内置列表已扩展为涵盖较新的 python 版本。
Create a
Script (Python)
object in your skin and use that as a function. TALES expressions are limited for a reason: they are there only to help you create HTML or XML markup, not do full-scale business logic. Better still, create a proper browser view and avoid the severe restrictions laid on Through-The-Web editable code.Also, you are misrepresenting or misunderstanding TALES. You can use builtin methods like sorted and set. And instead of
if
you can use test(condition, iftrue, iffalse) or a good oldcondition and iftrue or iffalse
with the limitation that the result ofiftrue
must itself evaluate to true.Even better, you can access a limited set of Python modules via the
modules
dictionary, such asmodules['string']
. You'll need to make additional security declarations in a filesystem python module to extend this though.See the Python TALES expression section of the TAL documentation. Note that the list of built-ins accessible to TALES listed there has since been expanded to cover newer python versions.
55/66/44 项的确切顺序是否重要,还是只需将它们列在最后?如果顺序不重要,你可以这样做:
Does the exact order of the 55/66/44 items matter, or do they just need to be listed at the end? If the order doesn't matter you could do this:
这是破坏
messed_order
的一个,这个对
messed_order
进行原地排序Here is one that destroys
messed_order
This one sorts
messed_order
in place不要偏离已经给出的答案,但它是 python - 你不会任意限制使用循环:
与将循环放在两行上一样有效。
或者,这是 Zope - 如果您不能在单个“python:”表达式中完成它,是的,您可以使用帮助程序脚本。脚本是通过获取找到的,因此包含以下内容的模板
将查找当前对象(上下文)的或者属性“脚本”[可以是方法或属性],或当前文件夹或任何祖先文件夹中名为 script 的“脚本 (Python)”对象!事实上,它甚至不需要是脚本对象 - 尽管出于您的目的,它需要是返回列表的某个对象。
根本不是塔纳托斯所说的“任意限制部”,而是限制不够!
Not to detract from the answers already given, but it's python - you aren't arbitrarily restricted from using loops:
is as valid as putting the loop on two lines.
Alternatively, this is Zope - if you can't do it in a single "python:" expression, yes you can use a helper script. Scripts are found by acquisition, so a template containing something like:
will lookup a either an attribute 'script' of the current object (context) [which could be a method or a property], or a "Script (Python)" object named script in the current folder, or in any ancestor folder! In fact, it doesn't even need to be a script object - though for your purpose it needs to be some object that returns a list.
Far from "the Department of Arbitrary Restrictions" as Thanatos put it, it's more as if there aren't enough restrictions!