top of page
Search

Module_8

  • Writer: Angela Porcelli
    Angela Porcelli
  • Mar 5, 2021
  • 3 min read

Updated: Mar 6, 2021

Original data: Find or create your own data set. You will then conducted copy/recopy plus four types of iterator functions: import itertools, infinite iterator, itertools.cycle, and itertools.repeat. In your blog, discuss the attributes of copy and iterator.


Deep Copy: I used my data for this assignment, pulling from my pets and 2020 foster pets. The first task was to make a copy and recopy of the data. I first assigned elements to the variable foster_pets. I made a deep copy of foster_pets to ensure changes to the original list (foster_pets) did not contribute to changes in the deep copy. Next, I changed the first element in my original list. To ensure the change to my original list did not alter my deep copies, I printed both lists. foster_pets = ['Squeak', 'Siracha', 'Tilly', 'Andy'] #2020 pets fostered from SPCA foster_mouse = copy.deepcopy(foster_pets) #Deep copy to ensure foster_pets does not change foster_mouse foster_pets[0]=('Mary') #Change the name 'Squeak' to 'Mary' in the foster_pets list print(foster_pets) print(foster_mouse) x=foster_mouse

#Test to ensure changes to original list do not effect deep copies print(x)


While foster_pets showed ['Mary', 'Siracha', 'Tilly', 'Andy'] as the list, foster_mouse and x still showed ['Squeak', 'Siracha', 'Tilly', 'Andy'] as their list.


Soft Copy: I ran another copy, this time choosing a soft copy utilizing the script copy.copy(). I copied the original list to a new variable and then altered the original list, resulting in the change occurring in the soft copy as well. To ensure the original code changes were replicated in copies, I ran another test copy using x as the variable.

import copy

foster_pets = [['Squeak', 'Siracha'], ['Tilly', 'Andy']] #2020 pets fostered from SPCA foster_pocketpets= copy.copy(foster_pets) #Soft copy to alter original foster_pets list and copies foster_pets[1][1] = ['Bun Bun'] #Replace the name 'Andy' to 'Bun Bun' in the original foster_pet list


print(foster_pets) print(foster_pocketpets) x=foster_pocketpets #Test to ensure soft copy properties print(x)



Itertools.chain(): The second task was to use an itertool of my choosing on my data. I choose to use the chain() itertool. First, I assigned sets of elements to various variables, with each set remaining unique. Then I instructed Python to return the sets as a single string containing each element in order of its set's placement in the original code.

Chinchillas = {"Squeak","Siracha"} Cats = {"Bean Sprout", "Chixpea","Lima","Pinto", "Cannoli", "Goblin", "Pumpkin"} Rabbits = {"Bun Bun", "Petunia", "Tilly", "Andy"} Guinea_Pig = {"Gertrude"} Mouse = {"Mary"} result = itertools.chain(Chinchillas,Cats,Rabbits,Guinea_Pig,Mouse) for each in result: print(each)



Infinite Itertools: I choose to use itertools.count as my example of infinite itertools. Sticking with my theme, I wanted a code that would represent the amount of foster animals I could take in during a year. I set the variable integer i to 52, representing 52 weeks in a year. I set the code to count by four since I foster each animal for approximately four weeks. Since I am counting by fours, I also set four as my start number. The printed return showed on which weeks of the year I could anticipate taking in a new foster animal.

import itertools for i in itertools.count(4, 4): if i == 52: break else: print(i, end =" ")



Itertools.cycle(): Another task was to run an itertools.cycle(). An itertools.cycle() will run a list indefinitely. For my cycle, I first assigned a set of elements to the variable "Pets". Then, I instructed Python to cycle the list "Pets". The cycling continued until I closed Visual Studio Code.

Pets={"Cassanova", "Foxy Fenny", "Lightning"} #Pet names for Pets in itertools.cycle(Pets): print(Pets)



Itertools.repeat(): The repeat itertool allows repetition of objects indefinitely unless a specific time argument is provided. For my repeat, I first assigned a set of elements to the variable "Pets". Then, I instructed Python to repeat the object "Pets" five times.

Pets={"Cassanova", "Foxy Fenny", "Lightning"} #pet names for i in itertools.repeat(Pets,5): print(i)

 
 
 

Recent Posts

See All
Final_Project

Problem description: The goal of graduating with a college degree is to have the ability to choose an employer that will provide an...

 
 
 

Comments


Post: Blog2_Post
  • Facebook
  • Twitter
  • LinkedIn

©2021 by LIS 5937 - Python for Data Science Professions. Proudly created with Wix.com

bottom of page