简体   繁体   English

返回一个不在列表中的元素

[英]Return an element which is not in list

I have a list containing 3 elements, for example '1', '2', '3' and I wanna write a function which returns element which is not an function argument.我有一个包含 3 个元素的列表,例如“1”、“2”、“3”,我想编写一个返回不是函数参数的元素的函数。 For example:例如:

 def get_char(a, b):
     crs = ['1', '2', '3']
     return something

 x = get_char('1', '2')
 x = '3'

 x = get_char('3', '2')
 x = '1'

You can see what I'm trying to accomplish.你可以看到我正在努力完成什么。 I know that I can get it by using if statements but is there any simpler way of doing it?我知道我可以通过使用 if 语句来获得它,但是有没有更简单的方法呢?

Here is a general response:以下是一般性回应:

def get_chars(*args):
    chars = {'1', '2', '3'}
    return (chars^set(args)).pop()

Though you may want to do something to handle the case that you pass in 3 arguments or 1 argument.尽管您可能想要做一些事情来处理您传入 3 个参数或 1 个参数的情况。 Depends on your usecase.取决于您的用例。

Note also that you could change your chars set to have more characters, and this function would still work.另请注意,您可以将chars集更改为具有更多字符,并且此功能仍然有效。


As ShadowRanger points out in the comments, it probably makes more sense to do set subtraction than bit-wise xor.正如ShadowRanger 在评论中指出的那样,进行集合减法可能比按位异或更有意义。 Something like this:像这样的东西:

def get_chars(*args):
    return ({'1', '2', '3'} - set(args)).pop()

This is still subject to the same problem if passing in the wrong number of arguments, which you could check using something like this:如果传入错误数量的参数,这仍然会遇到同样的问题,您可以使用以下内容进行检查:

def get_chars(*args):
    diff = {'1', '2', '3'} - set(args)
    if len(diff) != 1:
        # Handle the incorrect number of args (or use error)
        return None
    return diff.pop()

You can use two lists, an iteration and the not in function:您可以使用两个列表,一个迭代和 not in 函数:

x = [1,2,3]

y = [1,5,7]


def compare(original_list, list_to_compare):
    for i in original_list:
        if i not in list_to_compare:
            print(i)


compare(x,y)
# result 2, 3
compare(y,x)
*emphasized text*# result 5,3

You may use if and in to test if the variable is present in the arguments list ( args here):您可以使用ifin来测试变量是否存在于参数列表中(此处为args ):

>>> def get_char(*args):
...     crs = ['1', '2', '3']
...     for x in crs:
...             if x not in args:
...                     return x
...     return Null
... 
>>> x = get_char('1', '2')
>>> 
>>> x
'3'

This returns the very first element in crs that is not also present in args , returns Null if all are present.这将返回crs中不存在于args第一个元素,如果所有元素都存在,则返回Null

I guess that this could be what you are asking for:我想这可能是您要的:

def elements_from_crs_not_in_args(*args): 
     crs = ['1','2','3']
     return [i for i in crs if i not in args] 

Then:然后:

input: elements_from_crs_not_in_args('1','2')
result: ['3']

If you are planning to return a single element (first of the list?) instead of a list, return[..][0]如果您打算返回单个元素(列表的第一个?)而不是列表,请返回 [..][0]

I would do two things first if set of char are same I will pull them outside the function so it does not get executed each time.如果char集相同,我会先做两件事,我会将它们拉到函数之外,这样它就不会每次都被执行。 Second use set operation symmetric difference ^ to find all the elements which are present in only one list of two lists or difference to find the difference of one set from another.第二种使用集合运算对称差^找到仅存在于两个列表中的一个列表中的所有元素,或者使用差来查找一组与另一组的差。 In the end you have to convert the set into a tuple in order to get the first element as set do not support indexes最后,您必须将集合转换为元组才能将第一个元素设为集合不支持索引

crs = set(['1', '2', '3'])
def get_char(a, b):
    return (crs ^ set((a,b))).pop()

print(get_char('1', '2')) #'3'

Or better或更好

crs = set(['1', '2', '3'])
def get_char(a, b):
     return (crs - set((a,b))).pop()

print(get_char('1', '2')) #'3'

In Python3 you can use set literal as pointed out by @ShadowRanger在 Python3 中,您可以使用 @ShadowRanger 指出的设置文字

crs = {'1', '2', '3'}
def get_char(a, b):
    return (crs - {a, b}).pop()

print(get_char('1', '2')) #'3'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM