Python中實現 a and b or c in xx 這種邏輯最簡潔的方式是怎樣的?

時間 2021-05-31 12:47:16

1樓:意群

不用lambda的話,最簡單的是用all。In[1]:a,

b,c=

1,10,

5In[2

]:xs

=list

(range(1

,6))In[6

]:all(i

inxs

foriin(

a,b))

orcinxs

Out[6]:

True

2樓:ohmyfish

使用 Perl 6 中的class Junction 是很簡潔的。

my ($a,$b,$c) = ;

my @list = ;

say so $a & $b | $c ∈ @list; # True

3樓:2gua

這種判斷,Pythonic中規中矩的做法還是用集合。當然,如果是僅僅a、b、c,還體現不出用集合的好處,但是條件組合越多,越能體現用集合的優勢。

以下詳細舉例:

# 資料a=

"foo"b=

"bar"c=

"joy"d=

"quu"

lst=

["foo"

,"rii"

,"bar"

,"doo"

,'ruu'

]# 轉換成集合

set1

=set([a

,b])set2

=set([c

,d])t_set

=set

(lst

)i_set1

=set1

&t_set

i_set1

==set1

# (a and b) in lst

i_set2

=set2

&t_set

i_set2

!=set

()and

i_set2

<=set2

# (c or d) in lst

# 小結

# ((a and b) in lst) or (c in lst)

ifi_set1

==set1or(

cinlst):

pass

# ((a and b) in lst) or ((c or d) in lst)

ifi_set1

==set1or(

i_set2

!=set

()and

i_set2

<=set2

):pass

# 測試

lists=[[

"foo"

,"rii"

,"bar"

,"doo"

,'ruu'],[

"foo"

,"rii"

,"joy"

,"doo"

,'ruu'],[

"foo"

,"rii"

,"bar"

,"doo"

,'joy'],[

"buu"

,"rii"

,"see"

,"joy"

,'ruu'],[

"buu"

,"foo"

,"see"

,"tii"

,'ruu'],[

"buu"

,"waa"

,"see"

,"tii"

,'bar'],[

"buu"

,"goo"

,"see"

,"tii"

,'ruu'],[

"buu"

,"waa"

,"quu"

,"tii"

,'bar'

]]# ((a and b) in lst) or (c in lst)

forl

inlists

:t_set

=set(l

)i_set1

=set1

&t_set

(lambdaa,

b,c,

lst:

print

(i_set1

==set1or(

cinlst)))(a,

b,c,

l)else

:print

()# ((a and b) in lst) or ((c or d) in lst)

forl

inlists

:t_set

=set(l

)i_set1

=set1

&t_set

i_set2

=set2

&t_set

(lambdaa,

b,c,

d,lst:

print

(i_set1

==set1or(

i_set2

!=set

()and

i_set2

<=set2

)))(a,

b,c,

d,l)

執行結果如下圖所示:

4樓:鬧鐘

題主表達得不好,但是提出的問題是對的。這個問題本質上是有沒有辦法簡化:f(a

)andf(

b)orf

(c)讓我們不用乙個乙個的寫這個f。

所以,問題就是:

是否可以構造乙個DSL,通過這個DSL可以構造乙個logic框架----在Python裡面就是乙個函式f: (a' -> Bool) -> Bool, 它接受乙個條件 , 返回乙個Bool值。

具體在某個場景下是否有必要這麼乾是一碼事,但是這顯然是乙個有趣的問題。

具體的構造 @方澤圖 的回答裡面給得很漂亮啦。

5樓:

型別是你的好幫手。你這個 (a and b or c) in cc 一眼看上去型別不太對,先讓我們把它寫的更加 explicit 一些:

in_(or_(and_(contains(a), contains(b)), contains(c), cc)

可以看出,如果上述式子的型別是 Bool,而且 forsome x. a, b, c :: x; cc ::

Set x; contains :: x -> Set x -> Bool,那麼乙個合理的型別推斷是 and_, or_ :: (x -> Bool) -> (x -> Bool) -> x -> Bool; in_ ::

forall x. x -> x。

正如 F91 所說,型別清楚之後,剩下的就是填空了。我們先用 Haskell 寫一遍:

containsx=

foldr

combine

False

where

combinex'r

=x'==x

||r-- Control.Concatenative

-- 也可以寫成 bi f g h = h <$> f <*> gbif

ghx=

fx`h

`gxand_fg

=bifg

(&&)or_fg

=bifg

(||)main

=print

xwherex=

(contains1`

and_

`contains2`

or_`

contains3)

[1,3

,5]寫成 Python 就是

import

operator

defcontains(x

):def

call(xs

):returnxin

xsreturn

call

defbi(f

,g,h

):def

call(x

):returnh(

f(x),

g(x))

return

call

defand_(f

,g):returnbi(

f,g,

operator

.and_

)def

or_(f,

g):returnbi(

f,g,

operator

.or_

)print

(or_

(and_

(contains(1

),contains(2

)),contains(3

))([1,

3,5]))

核心做法是這樣。至於用 typeclass 或者 RTTI + operator overloading 來把它寫的漂亮一些,那就留作作業了...

6樓:cnfan

python中的in只能表達元素在集合中,而你的題目必須用到in,所以只能分解成a在xx中並且b在xx中或者c在xx中。這是語法決定的。

7樓:依雲

如果都是 hashable,用集合;

否則自己實現乙個函式來做判斷。

「(a and b or c) in xx 這種邏輯」是不通的邏輯,不管是在 Python 裡還是在英語裡。正確的表述,你在問題描述裡已經寫了。用英語表述為:

either a and b in xx, or c in xx.

8樓:紙管

我現在的做法是實現

all_in(values,target) #判斷所有values 是不是都在 target 裡面

in_all(value,targets)#判斷 value 在所有的targets 裡面

one_in(values,target)# 至少有乙個 value 在target 裡面

in_one(value,targets)# value 至少存在乙個target裡面就是原生的 a in xx 語法

這幾個函式,比如題目的邏輯就可以寫成 all_in([a,b],xx) or (c in xx)

(a or b or c and (d or e))in xx 寫成 one_in([a,b,c],xx) and one_in([d,e],xx)

這樣做有什麼缺點?

python中def中def是如何實現的?

琉年 deff n def g passifn print g print in f else returngf 1 f 1 f 1 a f 0 print a a a f 0 print a a a f 0 print a a b f 0 print b b b f 0 print b b b f...

這個python怎樣實現?

木女孩 你要是賣個萌我就幫你寫 data 學號 姓名 出生日期 性別 生肖 first line True chinese zodiac signs 鼠 牛 虎 兔 龍 蛇 馬 羊 猴 雞 狗 豬 f None try f open C py stud.csv r encoding utf 8 ne...

python 如何實現遠端tail f?

37丫37 django channels寫個也不複雜,這裡有關於channels的介紹,也有實現tailf的例子 Django使用Channels實現WebSocket 上篇 guyskk tail f是不會結束的,所以你要開新執行緒去讀取日誌並寫入django response 並且respon...