英文:
ValueError: The truth value of an array
问题
以下是您提供的代码的中文翻译部分:
我正在编写一个Python 3.11程序,出现了以下错误:
Traceback (most recent call last):
File "Égalisation\Python Version\Wiener Filter\main.py", line 14, in
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "Égalisation\Python Version\Wiener Filter\main.py", line 14, in
^^^^^^^^^^^^^^^^
File "Programs\Python\Python311\Lib\random.py", line 369, in choice
if not seq:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
当我运行这个简单的代码时:
```python
import numpy as np
import random
nbsymb = 8
M = 16
A = np.arange(-np.sqrt(M)+1, np.sqrt(M), 2)
symb = [random.choice(A) for _ in range(8)]
<details>
<summary>英文:</summary>
I work on a Python 3.11 program and I have this error:
Traceback (most recent call last):
File "Égalisation\Python Version\Wiener Filter\main.py", line 14, in <module>
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "Égalisation\Python Version\Wiener Filter\main.py", line 14, in <listcomp>
symb = [random.choice(A) for _ in range(nbsymb)]
^^^^^^^^^^^^^^^^
File "\Programs\Python\Python311\Lib\random.py", line 369, in choice
if not seq:
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
When I run this simple code:
import numpy as np
import random
nbsymb = 8
M = 16
A = np.arange(-np.sqrt(M)+1, np.sqrt(M), 2)
symb = [random.choice(A) for _ in range(8)]
</details>
# 答案1
**得分**: 0
Sounds like this is due to a [regression in Python 3.11](https://github.com/python/cpython/issues/100805) which was later fixed. Upgrade to Python >=3.11.2
Numpy has its own random stuff, perhaps you should use that instead: https://numpy.org/doc/stable/reference/random/
If for some reason you can't upgrade, choose random elements from a list instead of a `numpy` array:
```py
A = list(np.arange(-np.sqrt(M)+1, np.sqrt(M), 2))
symb = [random.choice(A) for _ in range(nbsymb)]
英文:
Sounds like this is due to a regression in Python 3.11 which was later fixed. Upgrade to Python >=3.11.2
Numpy has its own random stuff, perhaps you should use that instead: https://numpy.org/doc/stable/reference/random/
If for some reason you can't upgrade, choose random elements from a list instead of a numpy
array:
A = list(np.arange(-np.sqrt(M)+1, np.sqrt(M), 2))
symb = [random.choice(A) for _ in range(nbsymb)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论