如何从python函数中的列表理解中获得值?

发布于 2025-01-30 02:40:30 字数 877 浏览 1 评论 0原文

这是一个列表,n_list

n_list = [2, 4, 4, 5, 2, 1, 7, 1, 9]

问题语句

我想从列表理解中获得结果,但输出应该只是值,而不是列表。基本上,结果没有为其编写单独的循环。

简单的解决方案,但不是我想要的

如果我只需要打印结果,我可以这样做,使用*

print(*[val for val in n_list if n_list.count(val) == 1])

当前问题

但是,将其应用于函数时,我会遇到错误:

def singleVal(n_list):
    return(*[val for val in n_list if n_list.count(val) ==1])
    
singleVal(n_list)

错误:在这里无法使用启动表达式。

有什么方法可以从函数中的列表理解中获取值?

预期输出

最终,应该是以下代码,但以函数形式和使用列表理解方法:

result = [val for val in a if a.count(val) ==1]
    for i in result:
       return I

返回

int:仅发生一次

的元素

Here is a list, n_list

n_list = [2, 4, 4, 5, 2, 1, 7, 1, 9]

Problem Statement:

I would like to get the result from list comprehension but the output should just be the value, not the list. Basically, result without writing a separate for loop for it.

Simple Solution but not what I want:

If I had to only print the result, I can do this way, using *:

print(*[val for val in n_list if n_list.count(val) == 1])

Current Issue:

However, while applying it to a function, I am getting error:

def singleVal(n_list):
    return(*[val for val in n_list if n_list.count(val) ==1])
    
singleVal(n_list)

Error: Can’t use started expression here.

Is there any way to get only the values from list comprehension in a function?

Expected Output:

Ultimately, should be something like the following code but in a function form and using list comprehension method:

result = [val for val in a if a.count(val) ==1]
    for i in result:
       return I

Return:

int: the element that occurs only once

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

﹏半生如梦愿梦如真 2025-02-06 02:40:30

这里最简单的事情是返回列表,然后使用*操作员打印它:

def singleVal(n_list):
    return [val for val in n_list if n_list.count(val) == 1]
    
print(*singleVal([2, 4, 4, 5, 2]))  
# 5

如果您非常确定会有一个值可以返回,则可以将您的功能写入只需返回一个值,例如:

def singleVal(n_list):
    return next(val for val in n_list if n_list.count(val) == 1)
    
print(singleVal([2, 4, 4, 5, 2]))  
# 5

请注意,singleval的上述版本只有在n_list中有多个唯一值时才会返回第一个唯一值,并且会升级stopiteration如果唯一值零。

如果您希望您的函数以单个字符串的形式返回列表,则可以使用JOIN

def singleVal(n_list):
    return " ".join(str(val) for val in n_list if n_list.count(val) == 1)
    
print(singleVal([2, 4, 4, 5, 2]))  
# 5

请注意,将结果转换为字符串使其易于打印,但是很难做任何其他操作使用(您需要使用split()int()将其转换回数字值列表)。

如果您希望该函数仅打印结果而不是返回结果,请将print放入功能中,并且没有返回任何内容:

def singleVal(n_list):
    print(*(val for val in n_list if n_list.count(val) == 1))

singleVal([2, 4, 4, 5, 2])
# 5

The simplest thing to do here is to return the list, and then use the * operator to print it:

def singleVal(n_list):
    return [val for val in n_list if n_list.count(val) == 1]
    
print(*singleVal([2, 4, 4, 5, 2]))  
# 5

If you are very certain that there will be exactly one value to return, you can write your function to just return that one value, e.g.:

def singleVal(n_list):
    return next(val for val in n_list if n_list.count(val) == 1)
    
print(singleVal([2, 4, 4, 5, 2]))  
# 5

Note that the above version of singleVal will only return the first unique value if there is more than one unique value in n_list, and will raise StopIteration if there are zero unique values.

If you want your function to return the list in the form of a single string, you can use join:

def singleVal(n_list):
    return " ".join(str(val) for val in n_list if n_list.count(val) == 1)
    
print(singleVal([2, 4, 4, 5, 2]))  
# 5

Note that converting the result to a string makes it easy to print, but very difficult to do anything else with (you'd need to use split() and int() to convert it back to a list of numeric values).

If you want the function to just print the result rather than returning it, put the print inside the function, and don't have it return anything:

def singleVal(n_list):
    print(*(val for val in n_list if n_list.count(val) == 1))

singleVal([2, 4, 4, 5, 2])
# 5
流云如水 2025-02-06 02:40:30
def singleVal(L):
    result = [str(val) for val in L if L.count(val)==1]
    return '\n'.join(result)
def singleVal(L):
    result = [str(val) for val in L if L.count(val)==1]
    return '\n'.join(result)
奈何桥上唱咆哮 2025-02-06 02:40:30

您可以使用元组解开包装,左侧有一个单元元组:

result_list = [val for val in n_list if n_list.count(val) ==1]
singleVal, = result_list  # the comma makes the LHS a tuple
return singleVal

如果result_list不完全包含一个元素,则会丢弃一个错误:value eRROR:太多了如果有多个元素或valueerror:没有足够的值(预期1,got 0)如果列表为空,则取消包装的值(预期1)如果valueerror:没有足够的值。

You can use tuple unpacking, with a one-element tuple on the left-hand side:

result_list = [val for val in n_list if n_list.count(val) ==1]
singleVal, = result_list  # the comma makes the LHS a tuple
return singleVal

If result_list doesn't contain exactly one element, this will throw an error: either ValueError: too many values to unpack (expected 1) if there is more than one element, or ValueError: not enough values to unpack (expected 1, got 0) if the list is empty.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文