Lets look at some of the interesting functions.Ĭhain – chains multiple iterators together. The itertools module in the standard library provides lot of intersting tools to work with iterators. Problem 7: Write a program split.py, that takes an integer n and aįilename as command line arguments and splits the file into multiple smallįiles with each having n lines. Ignoring empty and comment lines, in all python files in the specified Problem 6: Write a function to compute the total number of lines of code, Problem 5: Write a function to compute the total number of lines of code inĪll python files in the specified directory recursively. Problem 4: Write a function to compute the number of python files (.pyĮxtension) in a specified directory recursively. The module standardizes a core set of fast, memory efficient tools that are useful by themselves or in combination. Each has been recast in a form suitable for Python. Problem 3: Write a function findfiles that recursively descends theĭirectory tree for the specified directory and generates paths of all the This module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML. Prints all the lines which are longer than 40 characters. Problem 2: Write a program that takes one or more filenames as arguments and Move all these functions into a separate module and reuse it in other programs. The code is much simpler now with each function doing one small thing. In python 2.x, zip() used to return lists.Def readfiles ( filenames ): for f in filenames : for line in open ( f ): yield line def grep ( pattern, lines ): return ( line for line in lines if pattern in line ) def printlines ( lines ): for line in lines : print ( line, end = "" ) def main ( pattern, filenames ): lines = readfiles ( filenames ) lines = grep ( pattern, lines ) printlines ( lines ) Please note, in python 3.x, izip() and izip_longest() are not there as both zip() and zip_longest() return iterators. If you are using python 2.x and your lists are huge then you need to use itertools.izip() that returns an iterator, thereby saving memory. itertools.izip_longest() runs till all lists are exhausted. zip() function iterates the lists till any of them gets exhausted. In this article, we have learnt different ways to traverse two lists in parallel. > for (a, b) in itertools.izip_longest(num1, num2): If you want python to traverse till the longest list is exhausted, then use itertools.izip_longest() function instead. > for (a, b) in itertools.izip(num1, num2): This is because in python 2.x izip() returns an iterator while zip() returns list. Please note, zip() function runs only till the smallest list runs.Īlso is you are using really long lists, then you should use itertools.izip() instead to save memory and for better performance. It returns a list of tuples, where each tuple contains 1 item from each of the lists being iterated. Here is an example to iterate over 2 lists in parallel using zip() function. In other words, zip() stops when the shortest list in the group stops. Zip() function traverses both lists in parallel but stops the moment any of the individual list is exhausted. It fills the empty values with None, and returns an iterator of tuples. It works just like the zip () function except that it stops when the longest list ends. You can use either zip() or itertools.zip_longest() functions to iterate over multiple lists in parallel in Python. If you need to iterate through two lists till the longest one ends, use itertools.ziplongest (). How to Iterate over Multiple Lists in Parallel in Python In this article, we will look at how to iterate multiple lists in parallel in Python. There are different ways to do this in python. Sometimes you may need to iterate over multiple lists together or simultaneously.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |