concept list in category data

appears as: list, list, lists, n list, The list
Data Science Bookcamp: Five Python Projects

This is an excerpt from Manning's book Data Science Bookcamp: Five Python Projects.

We ran 4 nested for-loops to explore the sequence of 4 births. This is not an efficient use of code. We can more easily generate our sample space using Python’s built-in itertools.product function. The function returns all pairwise combinations of all elements across all input lists. Below, we input 4 instances of the possible_children list into intertools.product. The product function then proceeds to iterate over all 4 instances of the list, computing all the combinations of list elements. The final output equals our sample space.

HTML text elements can vary in complexity. Beyond just headers and paragraphs, we can also visualize lists of texts within an HTML document. Suppose, for instance, that we wish to display a list of popular data science libraries. We’ll start by defining that list in Python.

Listing 16.10. Defining a list of data science libraries
libraries = ['NumPy', 'Scipy', 'Pandas', 'Scikit-Learn']
Figure 17.1. The rendered HTML for the first job-posting. The initial paragraph summarizes the data science job. The paragraph is followed by lists of bullet-points. Each bullet-point contains a skill that is required to get the job.
fig17 1
Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code

This is an excerpt from Manning's book Mastering Large Datasets with Python: Parallelize and Distribute Your Python Code.

Figure 1.4. We can use the map function to transform a sequence of data from one type to another, such as transforming web page URLs into lists of links found on those pages.

Now that we’re able to turn phone numbers of any format into phone numbers in a pretty format, we can combine our class with map to apply it to a list of phone numbers of any length. To combine the two, we’ll instantiate our class and pass the method as the function that map will apply to all the elements of a sequence. We can do that as shown in the following listing.

Listing 2.3. Applying the .pretty_format method to phone numbers
phone_numbers = [                                   #1
  "(123) 456-7890",
  "1234567890",
  "123.456.7890",
  "+1 123 456-7890"
]

P = PhoneFormatter ()                               #2
print(list(map(P.pretty_format, phone_numbers)))    #3

You’ll notice at the very bottom that we convert our map results to a list before we print them. If we were going to use them in our code, we would not need to do this; however, because maps are lazy if we print them without converting them to a list, we’ll just see a generic map object as output. This isn’t as satisfying as the nicely formatted phone numbers that we expected.

Another thing you’ll notice about this example is that we were set up perfectly to take advantage of map because we were doing a 1-to-1 transformation. That is, we were transforming each element of a sequence. In essence, we’ve turned this problem into our middle-school algebra example: applying n+7 to a list of numbers.

The printed output won’t be ordered, but the list that’s returned will be. The printed output shows the order in which the chunks were worked; the list output shows the data structure that was returned. We can see that even though Python works the problem in the “wrong” order, it still orders the results properly. When is this going to cause problems for us? Well, if we rely on state for one.

So far, we’ve only looked at using map to transform homogenous lists, which contain data of all the same type. There’s no reason, though, why we couldn’t use map to transform a list of heterogenous data. Write a function that turns [1, "A", False] into [2,"B",True].