In this section, we will see how two sorted lists can be combined using the heapq module in Python. For example, if list1 = [10, 20, 30, 40] and list2 = [100, 200, 300, 400, 500], then after concatenation it will return list3 = [10, 20, 30, 40, 100, 200, 300 , 400, 500]
We will use the heapq module to accomplish this task. This module comes with Python as a standard library module. Therefore, we must import it before using it.
import heapq
The heapq module has some properties. It’s like below &
heapq.heapify method (repeatable) p >
It is used to transform an iterative dataset into a heap data structure.
The heapq.heappush method (heap, item)
This method is used to insert an item into the heap. Then copy the entire heap structure again.
The heapq.heappop method (heap)
This method is used to return and remove an item from the top of the heap and perform heapify on the rest.
The heapq.heappushpop method (heap, element)
This method is used to insert and paste an element into one statement.
The heapq.heapreplace method (heap, element)
This method is used to insert and paste an element into a single statement. It removes an element from the heap root, then inserts the element into the heap.
The heapq.nlargest (n, iterable, key = none) method
This method is used to return the n largest element from heap.
Method heapq.nsmallest (n, repeatable, key = None)
This method is used to return the n smallest element from the heap.
Code example < / h2> import heapq first_list = [45, 12, 63, 95, 74, 21, 20, 15, 36] second_list = [42, 13, 69, 54, 15] firs t_list = sorted (first_list) second_list = sorted (second_list) print (’First sorted list:’ + str (first_list)) print (’Second sorted list:’ + str (second_list)) final_list = list (heapq.merge (first_list, second_list)) print (’The final list:’ + str (final_list))
Output
First sorted list: [12, 15, 20, 21, 36, 45, 63, 74, 95] Second sorted list: [13, 15, 42, 54, 69] The final list: [12, 13, 15, 15, 20, 21, 36, 42, 45, 54, 63, 69, 74, 95]
How to concatenate 2 sorted arrays in Python using heapq? import heapq: Questions
How to concatenate 2 sorted arrays in Python using heapq? insert: Questions
How to insert newlines on argparse help text?
5 answers
I"m using argparse
in Python 2.7 for parsing input options. One of my options is a multiple choice. I want to make a list in its help text, e.g.
from argparse import ArgumentParser
parser = ArgumentParser(description="test")
parser.add_argument("-g", choices=["a", "b", "g", "d", "e"], default="a",
help="Some option, where
"
" a = alpha
"
" b = beta
"
" g = gamma
"
" d = delta
"
" e = epsilon")
parser.parse_args()
However, argparse
strips all newlines and consecutive spaces. The result looks like
~/Downloads:52$ python2.7 x.py -h usage: x.py [-h] [-g {a,b,g,d,e}] test optional arguments: -h, --help show this help message and exit -g {a,b,g,d,e} Some option, where a = alpha b = beta g = gamma d = delta e = epsilon
How to insert newlines in the help text?
Answer #1
Try using RawTextHelpFormatter
:
from argparse import RawTextHelpFormatter
parser = ArgumentParser(description="test", formatter_class=RawTextHelpFormatter)
Is a Python list guaranteed to have its elements stay in the order they are inserted in?
5 answers
If I have the following Python code
>>> x = []
>>> x = x + [1]
>>> x = x + [2]
>>> x = x + [3]
>>> x
[1, 2, 3]
Will x
be guaranteed to always be [1,2,3]
, or are other orderings of the interim elements possible?
Answer #1
Yes, the order of elements in a python list is persistent.
Inserting image into IPython notebook markdown
5 answers
I am starting to depend heavily on the IPython notebook app to develop and document algorithms. It is awesome; but there is something that seems like it should be possible, but I can"t figure out how to do it:
I would like to insert a local image into my (local) IPython notebook markdown to aid in documenting an algorithm. I know enough to add something like <img src="image.png">
to the markdown, but that is about as far as my knowledge goes. I assume I could put the image in the directory represented by 127.0.0.1:8888 (or some subdirectory) to be able to access it, but I can"t figure out where that directory is. (I"m working on a mac.) So, is it possible to do what I"m trying to do without too much trouble?
Answer #1
Most of the answers given so far go in the wrong direction, suggesting to load additional libraries and use the code instead of markup. In Ipython/Jupyter Notebooks it is very simple. Make sure the cell is indeed in markup and to display a image use:

Further advantage compared to the other methods proposed is that you can display all common file formats including jpg, png, and gif (animations).