如何查找任意列表中是否存在偶数个元素或奇数个元素。
我尝试了list.index()以获取所有索引......但我仍然不知道如何告诉程序什么是偶数和什么是奇数。
答案 0 :(得分:6)
您可以使用内置函数len()
。
<强> Python Doc -- len() 强>
获取任意列表的长度(元素数)。
myList = [0,1,2,3,4,5]
if len(myList) % 2 == 0:
print ("even")
else
print ("odd")
定义返回bool(true或false)的函数。
def is_even(myList):
if len(myList) % 2 == 0:
return true
else:
return false
main():
myList = [0,1,2,3]
theListIsEven = is_even(myList) # will be true in this example
# because 4 items in myList
if theListIsEven(myList) == True:
# do something
else:
# do something else
return 0
模数运算符 %
给出余数。
EX:7%2 = 1
EX:4%2 = 0
答案 1 :(得分:4)
你需要的只是
len(listName)
这将为您提供长度。
我想你也可以这样做
if len(listName) % 2 == 0:
return True # the number is even!
else:
return False # the number is odd!
答案 2 :(得分:4)
your_list = [1,2,3,(4,5)]
# modulo operation finds the remainder of division of one number by another.
if len(your_list) % 2 == 0:
print "Even Number"
else:
print"number is odd"
答案 3 :(得分:2)
if len(mylist)%2==0:
#even
else:
#odd
答案 4 :(得分:1)
def has_even_length(some_sequence):
return not len(some_sequence)%2
def has_odd_length(some_sequence):
return bool(len(some_sequence)%2)
答案 5 :(得分:0)
偶数可以被2整除。奇数不是。
len(X)
将获得X
如果X
的长度可被2整除,那么它是偶数
如果X
的长度不能被2整除,那么它就是奇数
len(X)%2
返回除法问题的“余数”
例如5%2
将返回非{0}的1
(因为5除以2是2,余数为1)因此它不是偶数。
与6%4
相同的是返回2
,因为6除以4是1,余数为2。
所以len(X)%2
X
是你的列表,将返回1,表示它是奇数,或者0表示它是偶数。
答案 6 :(得分:0)
如果您想对此有所了解,也可以使用。
if len(mylist)&1:
print("odd")
else:
print("even")
如果您知道您的列表更有可能是奇怪的,这真是好看。
if len(mylist)&1:
print("list was odd")
elif CheckSomthingElse():
print("list was even AND CheckSomthingElse was True")
else:
print("list was even AND CheckSomthingElse was False")
通过%2以下的测试在python中哇更快。 python必须为此进行优化。在某些其他语言中,&1更快。这就是为什么始终进行测试很重要的原因。
import timeit
MyListOdd=[1,2,3,4,5,6,7]
MyListEven=[1,2,3,4,5,6]
print("MyListOdd == ",MyListOdd)
print("MyListEven == ",MyListEven)
print("len(MyListOdd) == ",len(MyListOdd))
print("len(MyListEven) == ",len(MyListEven))
def TestIfEvenBitwise(MyList):
if len(MyList)&1:
return False #Odd
else:
return True #Even
def TestIfEvenModulus(MyList):
if len(MyList)%2:
return False #Odd
else:
return True #Even
print("TestIfEvenBitwise(MyListOdd) == ",TestIfEvenBitwise(MyListOdd))
print("TestIfEvenModulus(MyListOdd) == ",TestIfEvenModulus(MyListOdd))
print("TestIfEvenBitwise(MyListEven) == ",TestIfEvenBitwise(MyListEven))
print("TestIfEvenModulus(MyListEven) == ",TestIfEvenModulus(MyListEven))
mysetup = """
MyListOdd=[1,2,3,4,5,6,7]
MyListEven=[1,2,3,4,5,6]
def TestIfEvenBitwise(MyList):
if len(MyList)&1:
return False #Odd
else:
return True #Even
def TestIfEvenModulus(MyList):
if len(MyList)%2:
return False #Odd
else:
return True #Even
"""
print("timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListOdd)', number=100000) == ",
timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListOdd)', number=100000))
print("timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListOdd)', number=100000) == ",
timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListOdd)', number=100000))
print("timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListEven)', number=100000) == ",
timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListEven)', number=100000))
print("timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListEven)', number=100000) == ",
timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListEven)', number=100000))
测试结果。
MyListOdd == [1, 2, 3, 4, 5, 6, 7]
MyListEven == [1, 2, 3, 4, 5, 6]
len(MyListOdd) == 7
len(MyListEven) == 6
TestIfEvenBitwise(MyListOdd) == False
TestIfEvenModulus(MyListOdd) == False
TestIfEvenBitwise(MyListEven) == True
TestIfEvenModulus(MyListEven) == True
timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListOdd)', number=100000) == 0.02574796500000004
timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListOdd)', number=100000) == 0.022446242000000005
timeit.timeit(setup = mysetup,stmt ='TestIfEvenBitwise(MyListEven)', number=100000) == 0.026081517000000054
timeit.timeit(setup = mysetup,stmt ='TestIfEvenModulus(MyListEven)', number=100000) == 0.025758655999999935
答案 7 :(得分:-1)
使用模数运算符 - %
给出余数。为了得到均匀,你需要除以2,例如:
7 % 2 = 1
8 % 2 = 0
最蟒蛇的方式是:
def is_even(your_list):
if len(your_list) % 2: # 0 in case of even i.e. No remainder
return True
else:
return False