将没有返回值的方法应用于列表的每个元素 [英] Applying a method with no return value to each element of a list

查看:99
本文介绍了将没有返回值的方法应用于列表的每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法在列表理解中使用没有返回值的方法(例如random.shuffle)?

Is there a way to use methods with no return value such as random.shuffle in a list comprehension?

>>> import pprint
>>> import random
>>> 
>>> L = [ random.shuffle(range(5)) for x in range(5)]
>>> 
>>> print L
[None, None, None, None, None]

这是for循环,将random.shuffle方法应用于列表中的每个项目:

This is the for loop that applies the random.shuffle method to each item of my list:

>>> L = [ range(5) for x in range(5) ]
>>> pprint.pprint(L)
[[0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4]]
>>> for element in L:
...     random.shuffle(element)
... 
>>> pprint.pprint(L)
[[2, 0, 3, 1, 4],
 [2, 0, 1, 4, 3],
 [4, 1, 3, 0, 2],
 [1, 2, 4, 3, 0],
 [1, 3, 0, 2, 4]]

我可以使用map,它的副作用是改写了原始列表,但返回了None

I can use map, which as a side effect shuffles the original list but returns a list of None

>>> L = [ range(5) for x in range(5) ]
>>> pprint.pprint(L)
[[0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4]]
>>> map(random.shuffle, L)
[None, None, None, None, None]
>>> pprint.pprint(L)
[[3, 0, 4, 1, 2],
 [2, 3, 0, 1, 4],
 [2, 3, 1, 4, 0],
 [4, 2, 0, 3, 1],
 [1, 3, 0, 2, 4]]

就像使用列表推导和随机播放一样:

as does using the list comprehension with shuffle:

>>> L = [ range(5) for x in range(5) ]
>>> pprint.pprint(L)
[[0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4],
 [0, 1, 2, 3, 4]]
>>> L1 = [ random.shuffle(x) for x in L ]
>>> pprint.pprint(L1)
[None, None, None, None, None]
>>> pprint.pprint(L)
[[1, 4, 0, 2, 3],
 [0, 4, 1, 3, 2],
 [2, 3, 4, 0, 1],
 [4, 1, 0, 2, 3],
 [2, 0, 4, 3, 1]]

关于堆栈溢出的许多问题和答案已经指出,使用map或lc产生副作用是不好的做法.我想知道是否有正确的方法来使用列表推导中没有返回值的方法.

Many questions and answers on stack overflow already point out that using map or a lc for the side effect is bad practice. I was wondering if there's any correct way to use a method with no return value in a list comprehension.

正在编写一种方法来包装不可返回的方法:

Is writing a method to wrap the non-returning method the only way:

>>> def shuffled(L):
...     ret_val = L[:]
...     random.shuffle(ret_val)
...     return ret_val
... 
>>> L = [ shuffled(range(5)) for x in range(5)]
>>> pprint.pprint(L)
[[2, 1, 0, 4, 3],
 [4, 0, 3, 1, 2],
 [4, 2, 3, 0, 1],
 [1, 0, 4, 2, 3],
 [2, 4, 3, 0, 1]]
>>> 

推荐答案

否-列表推导用于具有返回值的函数.定义它们的语义> :

No - list comprehensions are meant to be use with functions having return values. It's how their semantics are defined:

列表理解力简明扼要 创建列表而不用求助的方法 使用map(),filter()和/或 lambda.结果列表定义 往往比列表更清晰 使用这些构造来构建.每个 列表理解由 表达式后跟一个for子句, 则零个或多个for或if子句. 结果将是结果列表 通过评估表达式中的 for和if子句的上下文 紧随其后.

List comprehensions provide a concise way to create lists without resorting to use of map(), filter() and/or lambda. The resulting list definition tends often to be clearer than lists built using those constructs. Each list comprehension consists of an expression followed by a for clause, then zero or more for or if clauses. The result will be a list resulting from evaluating the expression in the context of the for and if clauses which follow it.

阅读此书后,应该很清楚,对没有返回值的函数的列表理解"是矛盾的.

Having read this, it should be clear that "a list comprehension from a function having no return value" is an oxymoron.

只需将for循环用于一次性":

Just use a for loop for something "one off":

import random
L = []
for x in range(5):
  l = range(5)
  random.shuffle(l)
  L.append(l)

干净简单.您的shuffled函数也很好,并且 可以用于列表理解.

Clean and simple. Your shuffled function is also just fine, and can be used in a list comprehension.

这篇关于将没有返回值的方法应用于列表的每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆