- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我忘记了 Python 的列表理解中的等式
[str(a)+str(b)+str(c) for a in range(3) for b in range(3) for c in range(3)]
['000', '001', '002', '010', '011', '012', '020', '021', '022', '100', '101', '102', '110', '111', '112', '120', '121', '122', '200', '201', '202', '210', '211', '212', '220', '221', '222']
我想限制 a!=b 和 b!=c。 for a!=b for b!=c
最后没有用。那么如何在列表推导中进行等式约束呢?
最佳答案
像这样:
["{}{}{}".format(a,b,c) for a in range(3) for b in range(3)
for c in range(3) if a!=b and b!=c]
或者更好地使用 itertools.product
:
>>> from itertools import product
>>> ["{}{}{}".format(a,b,c) for a, b, c in product(range(3), repeat=3)
if a!=b and b!=c]
['010', '012', '020', '021', '101', '102', '120', '121', '201', '202', '210', '212']
更新:
>>> from itertools import product, izip, tee
def check(lis):
it1, it2 = tee(lis)
next(it2)
return all(x != y for x,y in izip(it1, it2))
...
>>> n = 3
>>> [("{}"*n).format(*p) for p in product(range(3), repeat=n) if check(p)]
['010', '012', '020', '021', '101', '102', '120', '121', '201', '202', '210', '212']
>>> n = 4
>>> [("{}"*n).format(*p) for p in product(range(3), repeat=n) if check(p)]
['0101', '0102', '0120', '0121', '0201', '0202', '0210', '0212', '1010', '1012', '1020', '1021', '1201', '1202', '1210', '1212', '2010', '2012', '2020', '2021', '2101', '2102', '2120', '2121']
关于python - Python 列表理解中的平等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17972266/
我一直在努力理解 Javascript 的相等性。你能告诉我为什么以下行返回 false 吗? ? alert((function a(){}) == (function a(){})) // fal
scala 中有一个特别的问题每次都困扰着我。每次它咬我...它咬得很重... 为什么这一行编译通过 val x = "10" if (x != 10) { print("do somethin
我正在努力解决我网站的问题。 我想制作 与具有宽度 960px 的主体相等.我的代码有什么问题或缺失?对不起,我不能直接在这里发布代码,不允许我这样做。这是我的文件的链接: https://githu
我希望这些实例的原型(prototype)相同,但以下相等性检查的计算结果为 false。 var emp1 = new(EmployeeScope())("John"); var emp2 = ne
Hamcrest 中是否有匹配器来比较集合的相等性?有 contains 和 containsInAnyOrder 但我需要 equals 不绑定(bind)到具体的集合类型。例如。我无法将 Arra
struct Something { union { float k; int n; }; bool isFloat; bool ope
是否有 == 的等价物?但结果是x != NA如果 x不是 NA ? 以下是我想要的,但它很笨重: mapply(identical, vec1, vec2) 最佳答案 只需将“==”替换为 %in%
无论如何,我认为这是个问题。我正在使用一个 RelayCommand,它用两个委托(delegate)装饰一个 ICommand。一个是 _canExecute 的 Predicate,另一个是 _e
假设我有一个界面 interface IFoo{ val foo:String } 我想创建等于IFF的foo字符串匹配的类。 简单的例子: class A(override val foo:
谁能解释一下下面这部分代码的含义: private event UserChangedHandler m_UserChanged; public event UserChangedHandler Us
(为冗长的设置道歉。这里有一个问题,我保证。) 考虑一个类 Node具有在构建时分配的不可变唯一 ID。此 ID 用于在持久化对象图时进行序列化等。例如,当一个对象被反序列化时,它会根据 ID 针对主
Ruby API说: The eql? method returns true if obj and other refer to the same hash key. 我更改了 Object 的哈希
在我的 Nant 脚本中,我想将属性值与已知字符串进行比较。看完Nant Expressions文档 我相信我可以做一个基本的“==”比较来评估为 bool 值。 但是鉴于脚本块: 执行时,
简化示例: 我最近设置了 Single Table Inheritance在 Animal 上模型。 Cat和 Dog是 Animal 的子类. 我有一个 Animal工厂 : factory :an
如何使用 NHibernate 通过以下测试? 我认为只需覆盖实体类中的 Equals 和 GetHashCode 就足以使其按照我希望的方式工作。显然,对于非常微不足道的“点”对象,为相同的坐标保留
我是一名优秀的程序员,十分优秀!