[英]how to return only one different element in list in python
我有一个元素列表,只有一个(奇数或偶数)与其他元素不同,因此,我想要它在列表中的索引。
例如:
nums = [2, 4, 6, 8, 7] or nums = [1, 3, 4, 5]
#how using only "for" loop not def (it is too difficult for me yet) do it?
for i,num in enumerate (nums):
if num % 2 == 1 and ... # I was thinking here of using a count function (I do not know how to tell python that in the same time it should be unique element
print (i)
elif if num % 2 == 0 and ...
print (i)
请简单地解释一下,因为我非常了解Python。
确实很难理解问题的范围,但是如果您只想以多数票通过(因为在所有数字中,该组的投票是偶数还是奇数,那么您可以执行以下操作:
nums1 = [2,4,6,7,8]
nums2 = [2,1,3,5,7]
def get_one(nums):
nums_bin = [i%2 for i in nums]
if sum(nums_bin)>1:
return nums_bin.index(0)
return nums_bin.index(1)
print nums1, get_one(nums1)
print nums2, get_one(nums2)
# outputs
# [2, 4, 6, 7, 8] 3
# [2, 1, 3, 5, 7] 0
nums = [2, 4, 6, 8, 7]
#With the following line you take modulo 2 for each element so nums will become [0, 0, 0, 0, 1]
nums = [x%2 for x in nums]
#you still need to figure out, if the majority of elements was even or odd
#you can do that by checking the sum of the array. If it is 1, all but one element were even, if it is len(nums)-1 all but one were odd.
sum_of_nums = sum(nums)
for i in range(len(nums)):
#If the sum is ==1 you are looking for the one element that is 1
if nums[i] == 1 and sum_of_nums == 1:
print(i)
#If the sum is len(nums)-1 you are looking for the one element that is 0
if nums[i] == 0 and sum_of_nums == len(nums)-1:
print(i)
现在关于不要使用def
:
def
用于定义类似于数学知识的函数。 f(x)=x+1
将对f(1)=2 , f(2)=3
起作用,类似于您def = define
一个接受输入的函数( Math -> the x, here -> an array of Integers
),并做一些事情吧( Math -> Add 1, here -> find the element
)
语法为def functionName (inputParameter):
对于上面的代码:
def yourFunction (nums):
for i in range(len(nums)):
#If the sum is ==1 you are looking for the one element that is 1
if nums[i] == 1 and sum_of_nums == 1:
print(i)
#If the sum is len(nums)-1 you are looking for the one element that is 0
if nums[i] == 0 and sum_of_nums == len(nums)-1:
print(i)
您现在可以执行以下操作:
nums1 = [2, 4, 3]
nums2 = [3, 5, 8]
然后致电:
yourFunction(nums1)
,它将打印2
yourFunction(nums2)
,它将打印2
我会尽量解释的问题一步一步,并按照要求,我会尽量避免在最初定义的功能或列表推导,并试图解释如何从去for
到列表理解或定义功能。
我们从列表nums
开始,然后定义两个空列表。 在一个我们将存储偶数的位置nums
和奇者的其他位置:
#nums = [2, 4, 6, 8, 7]
nums = [1, 3, 4, 5]
odds = []
evens = []
for i,num in enumerate (nums):
if num % 2 == 1:
odds.append(i)
elif num % 2 == 0:
evens.append(i)
的,我们将打印长度为1的列表:
if len(odds)==1:
print odds[0]
elif len(evens)==1: # or else:
print evens[0]
有一个elif
而不是else来检查确实只有一个数字与其余数字不同,但是可以用else
代替。
现在简化并增加代码的pythonic。
使用列表推导,可以在同一行中编写赔率和偶数列表。 因此, for
循环可以简化为仅两行:
odds = [i for i,num in enumerate(nums) if num % 2 == 1]
evens = [i for i,num in enumerate(nums) if num % 2 == 0]
然后,如果要检查的列表不止一个,那么将所有命令包装到一个函数中将很有趣。 这样,只需调用您定义的此函数,所有命令将立即执行。
函数由def
命令定义,后跟function_name(input1,input2...):
最后,可以选择放置包含return output1,output2...
。 在这种情况下,只有一个输入(数字列表)和一个输出( 奇数/偶数out的索引),因此:
def oddeven_one_out(num_list):
odds = [i for i,num in enumerate(nums) if num % 2 == 1]
evens = [i for i,num in enumerate(nums) if num % 2 == 0]
if len(odds)==1:
result = odds[0]
elif len(evens)==1:
result = evens[0]
return result
现在,可以为每个列表调用该函数:
nums1 = [1, 3, 4, 5]
print oddeven_one_out(nums1)
nums2 = [2, 4, 6, 8, 7]
print oddeven_one_out(nums2)
但是,定义的函数有一个小问题,那就是,如果用户犯了一个错误并且输入了一个所有数字都是偶数的列表,则变量result
将不会被定义,而return
会引发错误。
这很容易解决,并且有助于理解return
命令。 当函数遇到return
,它将离开,而不执行下面的代码。 使用此行为,我们可以重新定义函数:
def oddeven_one_out(num_list):
odds = [i for i,num in enumerate(nums) if num % 2 == 1]
if len(odds)==1:
return odds[0]
evens = [i for i,num in enumerate(nums) if num % 2 == 0]
if len(evens)==1:
return evens[0]
现在,该函数检查是否只有一个奇数,如果是,则返回其位置,否则检查另一个数是否为偶数,如果是,则返回其位置。 另外,如果用户调用oddeven_one_out([1,3,5,7])
,则结果将为None
因为该函数不返回任何值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.