使用没有迭代变量的列表理解
Use list comprehension without iteration variable
我想知道是否有办法使用例如如果我不需要,没有迭代变量的列表理解?例如,在此代码示例中:
a = [random.randrange(-10, 11) / 10 for i in range(100)]
我收到警告 "Local variable 'i' value is not used"。是否有任何不带迭代变量的列表理解构造的变体?
直接丢弃i
的值:
a = [random.randrange(-10, 11) / 10 for _ in range(100)]
_
is considered the "last value" in Python and by convention is
used as a "throw away" value.
我想知道是否有办法使用例如如果我不需要,没有迭代变量的列表理解?例如,在此代码示例中:
a = [random.randrange(-10, 11) / 10 for i in range(100)]
我收到警告 "Local variable 'i' value is not used"。是否有任何不带迭代变量的列表理解构造的变体?
直接丢弃i
的值:
a = [random.randrange(-10, 11) / 10 for _ in range(100)]
_
is considered the "last value" in Python and by convention is used as a "throw away" value.