Categories
Python Answers

How to do list rotation with Python?

Sometimes, we want to do list rotation with Python.

In this article, we’ll look at how to do list rotation with Python.

How to do list rotation with Python?

To do list rotation with Python, we can use list slicing.

For instance, we write

def rotate(l, n):
    return l[n:] + l[:n]

to create the rotate function that rotates list l for n positions.

In it, we get the slice of the array from index n to the end of the array.

And we append the list l from index 0 to the index n - 1 after the first list slice.

Conclusion

To do list rotation with Python, we can use list slicing.

Categories
Python Answers

How to use list comprehension with if statement with Python?

Sometimes, we want to use list comprehension with if statement with Python.

In this article, we’ll look at how to use list comprehension with if statement with Python.

How to use list comprehension with if statement with Python?

To use list comprehension with if statement with Python, we put the if expression at the end of the list comprehension expression.

For instance, we write

[y for y in a if y not in b]

to get the values of y in list a that’s not in list b.

Conclusion

To use list comprehension with if statement with Python, we put the if expression at the end of the list comprehension expression.

Categories
Python Answers

How to ignore the namespace of XML files to locate matching element when using the method “find”, “findall” with the Python ElementTree module?

Sometimes, we want to ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module.

In this article, we’ll look at how to ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module.

How to ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module?

To ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module, we can strip the namespace from the DOM object with the rpartition method.

For instance, we write

from io import StringIO 
import xml.etree.ElementTree as ET

# ...

it = ET.iterparse(StringIO(xml))
for _, el in it:
    prefix, has_namespace, postfix = el.tag.partition('}')
    if has_namespace:
        el.tag = postfix
root = it.root

to call ET.iterparse to parse rhe xml file.

Then we loop through the nodes with a for loop.

In it, we call el.tag.rpartition with '}' to get the tag value from postfix.

Then we check if has_namespace is True.

If it is, then we assign el.tag to postfix.

Conclusion

To ignore the namespace of XML files to locate matching element when using the method "find", "findall" with the Python ElementTree module, we can strip the namespace from the DOM object with the rpartition method.

Categories
Python Answers

How to share a result queue among several processes with Python?

Sometimes, we want to share a result queue among several processes with Python.

In this article, we’ll look at how to share a result queue among several processes with Python.

How to share a result queue among several processes with Python?

To share a result queue among several processes with Python, we use the Queue class.

For instance, we write

import multiprocessing
def worker(name, que):
    que.put("%d is done" % name)

if __name__ == '__main__':
    pool = multiprocessing.Pool(processes=3)
    m = multiprocessing.Manager()
    q = m.Queue()
    workers = pool.apply_async(worker, (33, q))

to create a queue from the Manager object with m.Queue.

Then we call pool.apply_async with the worker function and (33, q) to call worker with 33 and queue q as the arguments.

In worker, we call que.put to return a result after worker is done running.

Conclusion

To share a result queue among several processes with Python, we use the Queue class.

Categories
Python Answers

How to find common substring between two strings with Python?

Sometimes, we want to find common substring between two strings with Python.

In this article, we’ll look at how to find common substring between two strings with Python.

How to find common substring between two strings with Python?

To find common substring between two strings with Python, we can use the os.path.commonprefix method.

For instance, we write

import os
common = os.path.commonprefix(['apple pie available', 'apple pies'])

to call os.path.commonpredix with a list of strings to find the common substring between the 2 strings.

Then common should be 'apple pie'.

Conclusion

To find common substring between two strings with Python, we can use the os.path.commonprefix method.