|
5 | 5 | <link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/> |
6 | 6 | <link href="https://learnbyexample.github.io"/> |
7 | 7 | <generator uri="https://www.getzola.org/">Zola</generator> |
8 | | - <updated>2022-01-21T00:00:00+00:00</updated> |
| 8 | + <updated>2022-01-25T00:00:00+00:00</updated> |
9 | 9 | <id>https://learnbyexample.github.io/atom.xml</id> |
| 10 | + <entry xml:lang="en"> |
| 11 | + <title>Python tip 5: random choice and sample</title> |
| 12 | + <published>2022-01-25T00:00:00+00:00</published> |
| 13 | + <updated>2022-01-25T00:00:00+00:00</updated> |
| 14 | + <link href="https://learnbyexample.github.io/tips/python-tip-5/" type="text/html"/> |
| 15 | + <id>https://learnbyexample.github.io/tips/python-tip-5/</id> |
| 16 | + <content type="html"><p>Here are a couple of commonly used methods for the built-in <code>random</code> module:</p> |
| 17 | +<ul> |
| 18 | +<li><code>choice()</code> method helps you get a random element</li> |
| 19 | +<li><code>sample()</code> method helps you get a <code>list</code> of a specific count of random elements</li> |
| 20 | +</ul> |
| 21 | +<pre style="background-color:#f5f5f5;"> |
| 22 | +<code><span style="color:#72ab00;">&gt;&gt;&gt; import </span><span style="color:#1f1f1f;">random |
| 23 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#1f1f1f;">nums </span><span style="color:#72ab00;">= </span><span style="color:#1f1f1f;">[</span><span style="color:#b3933a;">1</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">4</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">5</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">2</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">51</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">3</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">6</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">22</span><span style="color:#1f1f1f;">] |
| 24 | + |
| 25 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#1f1f1f;">random.</span><span style="color:#5597d6;">choice</span><span style="color:#1f1f1f;">(nums) |
| 26 | +</span><span style="color:#b3933a;">3 |
| 27 | + |
| 28 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#1f1f1f;">random.</span><span style="color:#5597d6;">sample</span><span style="color:#1f1f1f;">(nums, </span><span style="color:#5597d6;">k</span><span style="color:#72ab00;">=</span><span style="color:#b3933a;">4</span><span style="color:#1f1f1f;">) |
| 29 | +[</span><span style="color:#b3933a;">51</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">2</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">3</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">1</span><span style="color:#1f1f1f;">] |
| 30 | + |
| 31 | +</span><span style="color:#72ab00;">&gt;&gt;&gt; </span><span style="color:#1f1f1f;">random.</span><span style="color:#5597d6;">sample</span><span style="color:#1f1f1f;">(</span><span style="color:#b39f04;">range</span><span style="color:#1f1f1f;">(</span><span style="color:#b3933a;">1000</span><span style="color:#1f1f1f;">), </span><span style="color:#5597d6;">k</span><span style="color:#72ab00;">=</span><span style="color:#b3933a;">5</span><span style="color:#1f1f1f;">) |
| 32 | +[</span><span style="color:#b3933a;">490</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">26</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">9</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">745</span><span style="color:#1f1f1f;">, </span><span style="color:#b3933a;">919</span><span style="color:#1f1f1f;">] |
| 33 | +</span></code></pre> |
| 34 | +<p><img src="/images/info.svg" alt="info" /> Both these methods will work on any sequence object. The <code>sample()</code> method also accepts a <code>set</code> object, but that will be deprecated.</p> |
| 35 | +<p><img src="/images/info.svg" alt="info" /> See my <a href="https://github.com/learnbyexample/100_page_python_intro">100 Page Python Intro</a> ebook for a short, introductory guide for the Python programming language.</p> |
| 36 | +</content> |
| 37 | + </entry> |
10 | 38 | <entry xml:lang="en"> |
11 | 39 | <title>Brag post: Hacker News Front Page entries</title> |
12 | 40 | <published>2022-01-21T00:00:00+00:00</published> |
@@ -2592,7 +2620,7 @@ $ diff </span><span style="color:#72ab00;">-</ |
2592 | 2620 | <entry xml:lang="en"> |
2593 | 2621 | <title>Customizing pandoc to generate beautiful pdf and epub from markdown</title> |
2594 | 2622 | <published>2020-07-21T00:00:00+00:00</published> |
2595 | | - <updated>2021-08-27T00:00:00+00:00</updated> |
| 2623 | + <updated>2022-01-25T00:00:00+00:00</updated> |
2596 | 2624 | <link href="https://learnbyexample.github.io/customizing-pandoc/" type="text/html"/> |
2597 | 2625 | <id>https://learnbyexample.github.io/customizing-pandoc/</id> |
2598 | 2626 | <content type="html"><p>Either you've already heard of <code>pandoc</code> or if you have searched online for <code>markdown</code> to <code>pdf</code> or similar, you are sure to come across <code>pandoc</code>. This tutorial will help you use <code>pandoc</code> to generate <code>pdf</code> and <code>epub</code> from a <a href="https://github.github.com/gfm/">GitHub style markdown</a> file. The main motivation for this blog post is to highlight what customizations I did to generate <code>pdf</code> and <code>epub</code> versions for <a href="https://learnbyexample.github.io/books/">self-publishing my ebooks</a>. It wasn't easy to arrive at the set-up I ended up with, so I hope this will be useful for those looking to use <code>pandoc</code> to generate <code>pdf</code> and <code>epub</code> formats. This guide is specifically aimed at technical books that has code snippets.</p> |
@@ -2826,6 +2854,7 @@ no VAL is specified, the key will be given the value true.</p> |
2826 | 2854 | <li>See also <a href="https://github.com/fastai/fastdoc">fastdoc</a> — the output of fastdoc is an asciidoc file for each input notebook. You can then use asciidoctor to convert that to HTML, DocBook, epub, mobi, and so forth</li> |
2827 | 2855 | </ul> |
2828 | 2856 | </li> |
| 2857 | +<li><a href="https://www.thedigitalcatbooks.com/maubook-introduction/">Mau</a> — template-based markup language, heavily inspired by AsciiDoc</li> |
2829 | 2858 | <li><a href="https://asciidoctor.org/docs/what-is-asciidoc/">Asciidoctor</a> |
2830 | 2859 | <ul> |
2831 | 2860 | <li><a href="https://shape-of-code.coding-guidelines.com/2019/08/11/my-books-pdf-generation-workflow/">pdf generation workflow with Asciidoc</a></li> |
|
0 commit comments