如何从python函数中的列表理解中获得值?
这是一个列表,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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里最简单的事情是返回列表,然后使用
*
操作员打印它:如果您非常确定会有一个值可以返回,则可以将您的功能写入只需返回一个值,例如:
请注意,
singleval
的上述版本只有在n_list
中有多个唯一值时才会返回第一个唯一值,并且会升级stopiteration
如果唯一值零。如果您希望您的函数以单个字符串的形式返回列表,则可以使用
JOIN
:请注意,将结果转换为字符串使其易于打印,但是很难做任何其他操作使用(您需要使用
split()
和int()
将其转换回数字值列表)。如果您希望该函数仅打印结果而不是返回结果,请将
print
放入功能中,并且没有返回任何内容:The simplest thing to do here is to return the list, and then use the
*
operator to print it: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.:
Note that the above version of
singleVal
will only return the first unique value if there is more than one unique value inn_list
, and will raiseStopIteration
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
: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()
andint()
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:您可以使用元组解开包装,左侧有一个单元元组:
如果
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:
If
result_list
doesn't contain exactly one element, this will throw an error: eitherValueError: too many values to unpack (expected 1)
if there is more than one element, orValueError: not enough values to unpack (expected 1, got 0)
if the list is empty.