Differences

This shows you the differences between two versions of the page.

Link to this comparison view

ewis:laboratoare:03 [2021/03/24 13:51]
alexandru.predescu [Working with real data]
ewis:laboratoare:03 [2023/03/22 17:51] (current)
alexandru.predescu [Task]
Line 82: Line 82:
 === Use of lambda with filter() === === Use of lambda with filter() ===
  
-The ''​filter()''​ function in Python takes in a function and a list as arguments. This offers an elegant way to filter out all the elements of a sequence for which the function returns True. Here is a small program that returns the odd numbers from an input list:+The ''​filter()''​ function in Python takes in a function and a list as arguments. This offers an elegant way to filter out all the elements of a sequence for which the function returns True (reference: [[https://​www.w3schools.com/​python/​ref_func_filter.asp|Python filter() Function]]). Here is a small program that returns the odd numbers from an input list:
  
 <code python> <code python>
Line 91: Line 91:
 print(final_list) ​ print(final_list) ​
 </​code>​ </​code>​
 +
 +<note tip>
 +You can write this using list comprehension too: ''​final_list = [x for x in li if x%2 != 0]''​
 +</​note>​
 +
  
 === Use of lambda with map() === === Use of lambda with map() ===
  
-The ''​map()''​ function in Python takes in a function and a list as argument. The function is called with a lambda function and a list and a new list is returned which contains the modified items. Here is a small program that returns the double of a list:+The ''​map()''​ function in Python takes in a function and a list as argument. The function is called with a lambda function and a list and a new list is returned which contains the modified items (reference: [[https://​www.w3schools.com/​python/​ref_func_map.asp|Python map() Function]]). Here is a small program that returns the double of a list:
  
 <code python> <code python>
Line 103: Line 108:
 print(final_list) ​ print(final_list) ​
 </​code>​ </​code>​
 +
 +<note tip>
 +You can write this using list comprehension too: ''​final_list = [x*2 for x in li]''​
 +</​note>​
  
 === Use of lambda with reduce() === === Use of lambda with reduce() ===
-The ''​reduce()''​ function in Python takes in a function and a list as argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a repetitive operation over the pairs of the list. This is a part of ''​functools''​ module. Here is a small program that returns the sum of a list:+The ''​reduce()''​ function in Python takes in a function and a list as argument. The function is called with a lambda function and a list and a new reduced result is returned. This performs a rolling computation to sequential ​pairs of the list (reference: [[https://​www.tutorialsteacher.com/​python/​python-reduce-function#:​~:​text=The%20reduce()%20function%20is,​it%20returns%20a%20single%20value.|Python reduce function]]). This is a part of ''​functools''​ module. Here is a small program that returns the sum of a list:
  
 <code python> <code python>
Line 116: Line 125:
 </​code>​ </​code>​
  
-<​note>​**T1 ​(4p)** Having a list, filter the even numbers and get the sum of the squared elements.+This is shorter than: 
 + 
 +<code python>​ 
 +li = [5, 8, 10, 20, 50, 100]  
 +sum = 0 
 +for val in li: 
 +    sum = sum + val 
 +print (sum) 
 +</​code>​ 
 + 
 +<​note>​**T1** Having a list, filter the even numbers and get the sum of the squared elements.
   *Example list (random generator)   *Example list (random generator)
 <code python> <code python>
Line 182: Line 201:
 </​code>​ </​code>​
  
-<​note ​tip>+<​note ​warning>
 This would take a long time to print. There has to be a better way of working with data. This would take a long time to print. There has to be a better way of working with data.
 </​note>​ </​note>​
Line 194: Line 213:
 </​code>​ </​code>​
  
-<​note ​tip+<​note ​important
-To install pandas, you can use the package manager from the terminal, as shown in [[https://​ocw.cs.pub.ro/​courses/​ewis/​laboratoare/​01|Lab 1]]: py -3 -m pip install pandas+To install pandas, you can use the package manager from the terminal, as shown in [[https://​ocw.cs.pub.ro/​courses/​ewis/​laboratoare/​01|Lab 1]]: 
 + 
 +py -3 -m pip install pandas
 </​note>​ </​note>​
  
Line 208: Line 229:
 </​code>​ </​code>​
  
-<​note>​There are many more operations that you can perform with Pandas, which might be useful for working with data: [[https://​pandas.pydata.org/​pandas-docs/​stable/​user_guide/​10min.html|10 minutes to pandas]]</​note>​+<​note ​tip>There are many more operations that you can perform with Pandas, which might be useful for working with data: [[https://​pandas.pydata.org/​pandas-docs/​stable/​user_guide/​10min.html|10 minutes to pandas]]</​note>​
  
 Let's try to sort the titles by the release year and print the results into a text file (your task). Let's try to sort the titles by the release year and print the results into a text file (your task).
Line 246: Line 267:
  
 ==== Task ==== ==== Task ====
 +**T1 (2p)** Lab task presented above
  
-1. Use the functions presented in this lab to read text from a data file and count the number of occurrences for each word. Print the words sorted by the number of occurrences ​**(3p)**+**T2 (3p)** ​Use the functions presented in this lab to read text from a data file and count the number of occurrences for each word. Print the words sorted by the number of occurrences. ​
  
   *Text file: {{:​ewis:​laboratoare:​random_text.txt|download}}   *Text file: {{:​ewis:​laboratoare:​random_text.txt|download}}
   *Hint: You may use a dictionary   *Hint: You may use a dictionary
   *Check [[ewis:​laboratoare:​02|Lab 2]] for working with dictionaries   *Check [[ewis:​laboratoare:​02|Lab 2]] for working with dictionaries
-  *Sorting ​dictionary by values: <code python>​sorted_dict = sorted(dict.items(),​ key=lambda e: e[1], reverse=True)</​code>​+  *Sorting dictionary ​items by values ​as described [[https://​www.programiz.com/​python-programming/​methods/​built-in/​sorted|here]]: <code python>​sorted_dict = sorted(dict.items(),​ key=lambda e: e[1], reverse=True)</​code>​
  
  
-2. Print the Netflix titles into a text file **(3p)**+**T3 (2p)** ​Print the Netflix titles into a text file. 
  
-3. Print the Netflix titles and release years into a text file, using the CSV format, sorted by release year **(4p)**+**T4 (3p)** ​Print the Netflix titles and release years into a text file, using the CSV format, sorted by release year. 
  
 ==== Resources ==== ==== Resources ====
  
-{{:​ewis:​laboratoare:​lab3:​netflix_titles.csv.zip|netflix titles database}} +  * {{:​ewis:​laboratoare:​lab3:​netflix_titles.csv.zip|Netflix Titles Database}} 
- +  * [[https://​www.kaggle.com/​shivamb/​netflix-shows|Netflix Titles Database]] 
 +  * [[https://​en.wikipedia.org/​wiki/​Comma-separated_values|About CSV]] 
 +  * [[https://​en.wikipedia.org/​wiki/​UTF-8|UTF-8]] 
 +  * [[https://​www.w3schools.com/​python/​pandas/​pandas_csv.asp|Pandas]] 
 +  * [[https://​pandas.pydata.org/​pandas-docs/​stable/​user_guide/​10min.html|10 minutes to pandas]] 
 +  * [[https://​en.wikipedia.org/​wiki/​Newline|Newline]]
  
  
  
  
ewis/laboratoare/03.1616586696.txt.gz · Last modified: 2021/03/24 13:51 by alexandru.predescu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0