Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn Python Network Programming III - Echo Server using socketserver network framework Python Network Programming II - Chat Server / Client Python Network Programming I - Basic Server / Client : B File Transfer Python Network Programming I - Basic Server / Client : A Basics REST API : Http Requests for Humans with Flask Web scraping with Selenium for checking domain availability Python HTTP Web Services - urllib, httplib2 MongoDB with PyMongo I - Installing MongoDB. Connecting to DB, create/drop table, and insert data into a table Priority queue and heap queue data structure Python Object Serialization - yaml and json Python Object Serialization - pickle and json Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarismĬlasses and Instances (_init_, _call_, etc.)īits, bytes, bitstring, and constBitStream Strings - Escape Sequence, Raw String, and Slicingįormatting Strings - expressions and method calls Object Types - Numbers, Strings, and None Running Python Programs (os, sys, import) Here is an equivalent version which is much more verbose: The expression permutes even numbers from 0 through 4 with odd numbers from 0 through 4. Here is a much more complicated list comprehension example: Though list comprehensions construct lists, they can iterate over any sequence: When for loop are nested within a list comprehension, they work like equivalent for loop statement: We can code any number of nested for loop in a list comprehension, and each may have an optional associated if test. > list( map((lambda x: x ** 2), filter((lambda x: x % 2= 0),range(10))) )Īctually, list comprehensions are more general. We have to combine filter with map iteration: The equivalent map requires a lot more work. The expression on the left computes the squares. The for loop skips numbers which the if on the right is false. We collect the squares of the even numbers from 0 to 9. But we can combine an if and a map, in a single expression: The filter call here is not much longer that the list comprehension either. > list(filter((lambda x: x % 2 = 0), range(10)))Īll of these use the modulus operator %, to extract even numbers. If we use if with list compression, it is almost equivalent to the filter built-in. For more advance kinds of expressions, however, list comprehensions will often require considerably less typing. It's only a little bit longer that the list comprehension. If we had to use map, we would need to write a function to implement the square operation, probably, lambda instead of using a def: List comprehensions become more convenient when we need to apply an arbitrary expression to a sequence: The effect is similar to that of the for loop and the map call. List comprehensions collect the result of applying an arbitrary expression to a sequence and return them in a new list. While map maps a function over a sequence, list comprehensions map an expression over a sequence: > result = list(map(ord,'Dostoyevsky'))īut, we can get the similar result from a list comprehension expression. If we use map, we can get the same result with a single function call: If we want to collect the ASCII codes of all characters in a string, the most straightforward method is using a for loop and append the results to a list: These generators are memory efficient as they allocate memory as the items are generated instead of allocating it at the beginning.Python's built-in ord returns the ASCII integer code of a character: New generators can be created from the existing list. Similar to the above we can take in a list and create a dictionary from it. Running the above code gives us the following result − In the below example we take a list and create a new list by adding 3 to each element of the given list. In this method, we create a new list by manipulating the values of an existing list. It may involve multiple steps of conversion between different types of sequences. It basically a way of writing a concise code block to generate a sequence which can be a list, dictionary, set or a generator by using another sequence. We can create new sequences using a given python sequence.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |