Cause we slice it ads4/11/2023 This makes sense slicing operations just take part of an existing list, so it would be inefficient to create a deep copy and duplicate existing values. All slices, like x_list and the empty slice, are shallow copies. The answer lies in what many call the most efficient method to copy a list - using x_list, which is an ‘empty slice’, or a slice with no bounds that hence copies the entire list. This begs the question: why, then, would anyone use shallow copies if it’s attached to the original list? Only the shallow copy is affected, because it is the exact same array as the original ( x_list), just under a different name. Suppose I make a change: x_list = 10, which sets the first element of x_list, the original array, equal to 10. However, in deep copying, one drinker’s unpleasant habits don’t affect the other’s drink, and both can alter their drink however they please without affecting the other. In shallow copying, if one of the two drinkers spits down the straw into the drink, the other’s drink (and appetite) is changed as well. Think of shallow copying as drinking from the same drink (data) with two straws (access mechanisms), whereas deep copying is ordering the same drink and drinking individually from that one. Deep copies are independent from the original array, and expectedly using them takes up more storage. Instead of storing memory locations for values, the physical elements are recorded: np.array(). On the other hand, deep copies duplicate every value and allocate new spaces in memory. Because shallow copies are essentially the same array as the original but are recorded under a different name, they can be thought of as ‘views’ of the original data. Hence, these types of copies are dependent on the original array. That means that they actually store data like this: np.array(). Instead, they reference the memory location of each of the values from x_list. Shallow copies don’t actually copy each element’s value. What is different about these two copies? I can choose two paths, a shallow copy (also known as a view, for reasons you’ll see soon) or a deep copy. Consider a NumPy array, x_list = np.array().
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |