从python中的列表理解中仅打印列表的值
考虑一下我有一个列表,n_list
n_list = [2, 4, 4, 5, 2]
我的目标是获取具有出现频率的唯一val在列表中仅为1。
我可以按以下方式做:
result = [val for val in n_list if n_list.count(val) == 1]
for i in result:
print(i)
但是,我想获得列表中列表理解的结果,只是值。基本上,在没有为其编写单独的循环的情况下,这意味着没有:
for i in result:
print(i)
使用列表理解。
Consider I have a list, n_list
n_list = [2, 4, 4, 5, 2]
My goal is get the unique val that has frequency of occurrence is only 1 in the list.
I am able to do as follows:
result = [val for val in n_list if n_list.count(val) == 1]
for i in result:
print(i)
However, I would like to get the result of list comprehension with the list and just the value. Basically, without writing a separate for loop for it, that means, without:
for i in result:
print(i)
but using list comprehension.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只是发现了,我们可以使用
*
打印结果,但是,当作为函数测试时,它会产生错误:
Just figured out, we can print the result using
*
However, when tested as a function, it gives error: