Python .ipynb file prints output to the terminal without print function .py doesnt

huangapple go评论95阅读模式
英文:

Python .ipynb file prints output to the terminal without print function .py doesnt

问题

  1. 我正在实现一个样本概率密度函数尽管我的代码中没有打印函数但它会从我的值中打印样本输出我使用的是.ipynb文件当我将代码复制到.py文件中时就不会打印出来我不明白问题出在哪里以下是我的代码
  2. ```python
  3. import numpy as np
  4. def gen_b():
  5. samples = []
  6. num_samples = 10000
  7. while len(samples) < num_samples:
  8. b = np.random.random() * 6
  9. y = np.random.random()
  10. if y <= pdf_b(b):
  11. samples.append(b)
  12. return samples
  13. gen_b()

pdf_b(x) 是一个简单的 f(x) = y 函数,在其中没有打印函数。

我该如何解决这个问题?我不想让样本输出被打印出来。以下是我不想被打印的输出:

  1. [2.140133171853366,
  2. 3.782237422460611,
  3. 4.252458824774285,
  4. 1.9767482772032179,
  5. 2.250855035249897,
  6. 4.788930465436959,
  7. 1.9076515188916072,
  8. 2.745557799097914,
  9. 3.620720920902893,
  10. 2.540311807348857,
  11. 3.3786579434452295,
  12. 4.654430681635432,
  13. 3.885627334171001,
  14. 3.03255430392689,
  15. 2.078660750831954,
  16. 1.563168692059776,
  17. 4.066738029089566,
  18. 4.173320782327262,
  19. 2.3957708748717117,
  20. 2.392456670794856,
  21. 2.0479422380490147,
  22. 3.0472011553416785,
  23. 1.2691569709730193,
  24. 2.930679937209872,
  25. 3.0627475835692044,
  26. ...
  27. 2.3660255564569264,
  28. 2.3079729976179957,
  29. 2.8430403284524752,
  30. 3.9573097908844583,
  31. ...]
  1. <details>
  2. <summary>英文:</summary>
  3. I am implementing a sample probability density function. Although there is no print function in my code it prints sample output from my value. I use .ipynb file, and when i copy the code into .py file, it is not printed. I don&#39;t understand where is the problem. Here is my code:

import numpy as np

def gen_b():

  1. samples = []
  2. num_samples = 10000
  3. while len(samples) &lt; num_samples:
  4. b = np.random.random() * 6
  5. y = np.random.random()
  6. if y &lt;= pdf_b(b):
  7. samples.append(b)
  8. return samples

gen_b()

  1. `pdf_b(x)` is a simple `f(x) = y` function, there is no printing function inside of it.
  2. How can i solve the problem? I don&#39;t want to sample output is printed. Here is the output that I don&#39;t want to be printed:

[2.140133171853366,
3.782237422460611,
4.252458824774285,
1.9767482772032179,
2.250855035249897,
4.788930465436959,
1.9076515188916072,
2.745557799097914,
3.620720920902893,
2.540311807348857,
3.3786579434452295,
4.654430681635432,
3.885627334171001,
3.03255430392689,
2.078660750831954,
1.563168692059776,
4.066738029089566,
4.173320782327262,
2.3957708748717117,
2.392456670794856,
2.0479422380490147,
3.0472011553416785,
1.2691569709730193,
2.930679937209872,
3.0627475835692044,
...
2.3660255564569264,
2.3079729976179957,
2.8430403284524752,
3.9573097908844583,
...]
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

答案1

得分: 2

IPython是一个REPL,即一个读取、评估、打印循环。最后被评估的语句是gen_b(),所以该值被打印出来。如果你不想打印它,你可以简单地将结果赋值给某个变量,即使是一个临时变量,_ = gen_b()(或者在REPL中不运行它)。

英文:

IPython is a REPL, a read, evaluate, print loop. The last statement that is evaluated is gen_b(), so the value is printed. If you don't want it printed, one thing you can do is to simply assign the result to something, even a throwaway variable, _ = gen_b() (or just don't run it in a REPL).

huangapple
  • 本文由 发表于 2023年6月6日 06:20:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76410331.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定