列表理解中的断言 [英] Assertion in list comprehension
问题描述
有谁知道如何在列表理解中加入断言?我有以下列表理解的
,但是我想用断言来检查
检查rec_stdl的内容。我最终使用了另一个循环,
基本上复制了列表理解的功能。只是
看起来浪费了编码和计算机时间给我。
我只是希望我可以把断言放到列表推导中。
x = [(rec_stdl [0] .st / 10000.0,
rec_stdl [0] .cl,
rec_stdl [0] .bb,
rec_stdl [0] .bo,
rec_stdl [1] .bb,
rec_stdl [1] .bo,
rec_stdl [0] .ex
)
rec_by_ex中的rec_stdl如果len(rec_stdl)== 2
]
#duplicated loop
if __debug __:
rec_by_ex中的rec_stdl:
l = len(rec_stdl)
断言(l< = 2和l> 0)
如果l == 2:
断言(rec_stdl [0] .c == " C"和rec_stdl [1] .c ==" P")
assert(rec_stdl [0] .ex == rec_stdl [1] .ex)
assert(rec_stdl [0] .st == rec_stdl [1] .st)
assert(rec_stdl [0] .cp == rec_stdl [1] .cp)
>
谢谢,
Geoffrey
Hi,
Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.
I just wish I could put the assertions into list comprehensions.
x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]
#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)
Thanks,
Geoffrey
推荐答案
8月1日上午9:37,初学者< ; zyzhu2 ... @ gmail.comwrote:
On Aug 1, 9:37 am, beginner <zyzhu2...@gmail.comwrote:
有谁知道怎么把断言放在列表中理解?我有以下列表理解的
,但是我想用断言来检查
检查rec_stdl的内容。我最终使用了另一个循环,
基本上复制了列表理解的功能。只是
看起来浪费了编码和计算机时间给我。
我只是希望我可以把断言放到列表推导中。
x = [(rec_stdl [0] .st / 10000.0,
rec_stdl [0] .cl,
rec_stdl [0] .bb,
rec_stdl [0] .bo,
rec_stdl [1] .bb,
rec_stdl [1] .bo,
rec_stdl [0] .ex
)
rec_by_ex中的rec_stdl如果len(rec_stdl)== 2
]
#duplicated loop
if __debug __:
rec_by_ex中的rec_stdl:
l = len(rec_stdl)
断言(l< = 2和l> 0)
如果l == 2:
断言(rec_stdl [0] .c == " C"和rec_stdl [1] .c ==" P")
assert(rec_stdl [0] .ex == rec_stdl [1] .ex)
断言(rec_stdl [0] .st == rec_stdl [1] .st)
断言(rec_stdl [0] .cp == rec_stdl [1] .cp)
谢谢,
Geoffrey
Hi,
Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.
I just wish I could put the assertions into list comprehensions.
x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]
#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)
Thanks,
Geoffrey
你不能只是从列表理解中调用一个函数和
为每件商品做你想做的事吗?这样的事情(未经测试):
def checker(item):
断言(len(item)< = 2和len(item)0 )
if len(item)== 2:
assert(item [0] .c ==" C" and item [1] .c ==" ; P"
返回len(item)== 2
x = [如果检查者(item = item),则为all_items中的项目
Can''t you just call a function from within your list comprehension and
do whatever you want for each item? Something like this (not tested):
def checker(item):
assert(len(item) <= 2 and len(item) 0)
if len(item) == 2:
assert(item[0].c == "C" and item[1].c == "P"
return len(item) == 2
x = [whatever for item in all_items if checker(item = item)]
8月1日上午11点09分,danmcle ... @ yahoo.com< danmcle ... @ yahoo.com>
写道:
On Aug 1, 11:09 am, "danmcle...@yahoo.com" <danmcle...@yahoo.com>
wrote:
8月1日上午9:37,初学者< zyzhu2 ... @ gmail.comwrote:
On Aug 1, 9:37 am, beginner <zyzhu2...@gmail.comwrote:
Hi,
有谁知道怎么放列表理解中的断言?我有以下列表理解,但是我想使用断言来检查rec_stdl的内容。我最后使用了另一个循环
基本上复制了fu列表理解的部分。只是
看起来像是浪费了编码和计算机时间给我。
Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.
我希望我可以将断言放入列表推导中。
I just wish I could put the assertions into list comprehensions.
x = [(rec_stdl [0] .st / 10000.0,
rec_stdl [0] .cl,>
rec_stdl [0] .bb,
rec_stdl [0] .bo,
rec_stdl [1] .bb,
rec_stdl [1] .bo,
rec_stdl [0] .ex
)
如果len(rec_stdl)== 2,rec_by_ex中rec_stdl的
]
x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]
#duplicated loop
if __debug __:
对于rec_by_ex中的rec_stdl:
l = len(rec_stdl)
断言(l< = 2和l> 0)
如果l == 2:
断言(rec_stdl [0] .c ==" C"和rec_stdl [1] .c ==" P")
断言(rec_stdl [ 0] .ex == rec_stdl [1] .ex)
assert(rec_stdl [0] .st == rec_stdl [ 1] .st)
断言(rec_stdl [0] .cp == rec_stdl [1] .cp)
#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)
谢谢,
Geoffrey
Thanks,
Geoffrey
你不能只是从列表理解中调用一个函数和
为每件物品做你想做的事吗?这样的事情(未经测试):
def checker(item):
断言(len(item)< = 2和len(item)0 )
if len(item)== 2:
assert(item [0] .c ==" C" and item [1] .c ==" ; P"
返回len(item)== 2
x = [如果检查者(item = item),则为all_items中的项目 - 隐藏引用的文字 -
- 显示引用的文字 -
Can''t you just call a function from within your list comprehension and
do whatever you want for each item? Something like this (not tested):
def checker(item):
assert(len(item) <= 2 and len(item) 0)
if len(item) == 2:
assert(item[0].c == "C" and item[1].c == "P"
return len(item) == 2
x = [whatever for item in all_items if checker(item = item)]- Hide quoted text -
- Show quoted text -
好主意!谢谢!
Good idea! Thank you!
在8/1/07,初学者< zy ******* @ gmail.comwrote:
On 8/1/07, beginner <zy*******@gmail.comwrote:
有谁知道如何在列表理解中加入断言?我有以下列表理解的
,但我想使用断言要检查rec_stdl的内容。我最后使用了另一个循环
基本上复制了list comprehension的功能。它只是
看起来像浪费编码和计算机ti我对我说。
我只是希望我可以把断言放到列表推导中。
x = [(rec_stdl [0] .st / 10000.0,
rec_stdl [0] .cl,
rec_stdl [0] .bb,
rec_stdl [0] .bo,
rec_stdl [1] .bb,
rec_stdl [1] .bo,
rec_stdl [0] .ex
)
rec_by_ex中的rec_stdl如果len(rec_stdl)== 2
]
#duplicated loop
如果__debug__:
表示rec_by_ex中的rec_stdl:
l = len(rec_stdl)
断言(l< = 2且l> 0)
如果l == 2:
断言(rec_stdl [0] .c ==" C"和rec_stdl [1] .c ==" P")
断言(rec_stdl [0] .ex == rec_stdl [1] .ex)
断言(rec_stdl [0] .st == rec_stdl [1] .st)
assert(rec_stdl [0] .cp == rec_stdl [1] .cp)
Hi,
Does anyone know how to put an assertion in list comprehension? I have
the following list comprehension, but I want to use an assertion to
check the contents of rec_stdl. I ended up using another loop which
essentially duplicates the functions of list comprehension. It just
look like a waste of coding and computer time to me.
I just wish I could put the assertions into list comprehensions.
x=[(rec_stdl[0].st/10000.0,
rec_stdl[0].cl,
rec_stdl[0].bb,
rec_stdl[0].bo,
rec_stdl[1].bb,
rec_stdl[1].bo,
rec_stdl[0].ex
)
for rec_stdl in rec_by_ex if len(rec_stdl)==2
]
#duplicated loop
if __debug__:
for rec_stdl in rec_by_ex:
l=len(rec_stdl)
assert(l<=2 and l>0)
if l==2:
assert(rec_stdl[0].c=="C" and rec_stdl[1].c=="P")
assert(rec_stdl[0].ex==rec_stdl[1].ex)
assert(rec_stdl[0].st==rec_stdl[1].st)
assert(rec_stdl[0].cp==rec_stdl[1].cp)
第一:你所有的断言都是错的。 Assert是一个声明,而不是
函数。这些具体的行为会像预期的那样表现,但是很容易写出总是通过这种方式写出的那些。
其次:这是浪费代码,因为如果没有定义__debug__
断言将被编译器跳过。你可以为两个分支使用相同的循环
块。
第三:这种测试正是单元测试和/或$ /
doctests适用于。
First: All your asserts are wrong. Assert is a statement, not a
function. These specific ones will behave as expected, but it''s easy
to accidentally write ones that always pass this way.
Secondly: This is a waste of code, because if __debug__ is not defined
asserts will be skipped by the compiler. You could use the same loop
block for both branches.
Thirdly: This sort of testing is precisely what unit tests and/or
doctests are for.
这篇关于列表理解中的断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!