introd u ction to iterators
play

Introd u ction to iterators P YTH ON DATA SC IE N C E TOOL BOX ( - PowerPoint PPT Presentation

Introd u ction to iterators P YTH ON DATA SC IE N C E TOOL BOX ( PAR T 2 ) H u go Bo w ne - Anderson Data Scientist at DataCamp Iterating w ith a for loop We can iterate o v er a list u sing a for loop employees = ['Nick', 'Lore', 'Hugo'] for


  1. Introd u ction to iterators P YTH ON DATA SC IE N C E TOOL BOX ( PAR T 2 ) H u go Bo w ne - Anderson Data Scientist at DataCamp

  2. Iterating w ith a for loop We can iterate o v er a list u sing a for loop employees = ['Nick', 'Lore', 'Hugo'] for employee in employees: print(employee) Nick Lore Hugo PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  3. Iterating w ith a for loop We can iterate o v er a string u sing a for loop for letter in 'DataCamp': print(letter) D a t a C a m p PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  4. Iterating w ith a for loop We can iterate o v er a range object u sing a for loop for i in range(4): print(i) 0 1 2 3 PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  5. Iterators v s . iterables Iterable E x amples : lists , strings , dictionaries , � le connections An object w ith an associated iter() method Appl y ing iter() to an iterable creates an iterator Iterator Prod u ces ne x t v al u e w ith next() PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  6. Iterating o v er iterables : ne x t () word = 'Da' it = iter(word) next(it) 'D' next(it) 'a' next(it) StopIteration Traceback (most recent call last) <ipython-input-11-2cdb14c0d4d6> in <module>() -> 1 next(it) StopIteration: PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  7. Iterating at once w ith * word = 'Data' it = iter(word) print(*it) D a t a print(*it) No more v al u es to go thro u gh ! PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  8. Iterating o v er dictionaries pythonistas = {'hugo': 'bowne-anderson', 'francis': 'castro'} for key, value in pythonistas.items(): print(key, value) francis castro hugo bowne-anderson PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  9. Iterating o v er file connections file = open('file.txt') it = iter(file) print(next(it)) This is the first line. print(next(it)) This is the second line. PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  10. Let ' s practice ! P YTH ON DATA SC IE N C E TOOL BOX ( PAR T 2 )

  11. Pla y ing w ith iterators P YTH ON DATA SC IE N C E TOOL BOX ( PAR T 2 ) H u go Bo w ne - Anderson Data Scientist at DataCamp

  12. Using en u merate () avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver'] e = enumerate(avengers) print(type(e)) <class 'enumerate'> e_list = list(e) print(e_list) [(0, 'hawkeye'), (1, 'iron man'), (2, 'thor'), (3, 'quicksilver')] PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  13. en u merate () and u npack avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver'] for index, value in enumerate(avengers): print(index, value) 0 hawkeye 1 iron man 2 thor 3 quicksilver for index, value in enumerate(avengers, start=10): print(index, value) 10 hawkeye 11 iron man 12 thor 13 quicksilver PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  14. Using z ip () avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver'] names = ['barton', 'stark', 'odinson', 'maximoff'] z = zip(avengers, names) print(type(z)) <class 'zip'> z_list = list(z) print(z_list) [('hawkeye', 'barton'), ('iron man', 'stark'), ('thor', 'odinson'), ('quicksilver', 'maximoff')] PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  15. z ip () and u npack avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver'] names = ['barton', 'stark', 'odinson', 'maximoff'] for z1, z2 in zip(avengers, names): print(z1, z2) hawkeye barton iron man stark thor odinson quicksilver maximoff PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  16. Print z ip w ith * avengers = ['hawkeye', 'iron man', 'thor', 'quicksilver'] names = ['barton', 'stark', 'odinson', 'maximoff'] z = zip(avengers, names) print(*z) ('hawkeye', 'barton') ('iron man', 'stark') ('thor', 'odinson') ('quicksilver', 'maximoff') PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  17. Let ' s practice ! P YTH ON DATA SC IE N C E TOOL BOX ( PAR T 2 )

  18. Using iterators to load large files into memor y P YTH ON DATA SC IE N C E TOOL BOX ( PAR T 2 ) H u go Bo w ne - Anderson Data Scientist at DataCamp

  19. Loading data in ch u nks There can be too m u ch data to hold in memor y Sol u tion : load data in ch u nks ! Pandas f u nction : read_csv() Specif y the ch u nk : chunk_size PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  20. Iterating o v er data import pandas as pd result = [] for chunk in pd.read_csv('data.csv', chunksize=1000): result.append(sum(chunk['x'])) total = sum(result) print(total) 4252532 PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  21. Iterating o v er data import pandas as pd total = 0 for chunk in pd.read_csv('data.csv', chunksize=1000): total += sum(chunk['x']) print(total) 4252532 PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  22. Let ' s practice ! P YTH ON DATA SC IE N C E TOOL BOX ( PAR T 2 )

  23. Congrat u lations ! P YTH ON DATA SC IE N C E TOOL BOX ( PAR T 2 ) H u go Bo w ne - Anderson Data Scientist at DataCamp

  24. What ’ s ne x t ? List comprehensions and generators List comprehensions : Create lists from other lists , DataFrame col u mns , etc . Single line of code More e � cient than u sing a for loop PYTHON DATA SCIENCE TOOLBOX ( PART 2)

  25. Let ' s practice ! P YTH ON DATA SC IE N C E TOOL BOX ( PAR T 2 )

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend