Change language

Lec 11.2 Counter, list comprehension and string format #python

Lec 11.2 Counter, list comprehension and string format #python

okay so since we are talking about uh loading  txt file into python so i just also want to introduce some very useful uh tools and functions  that in python the first one is called counter so counter is a very great function that can  help us to count the number of items that appears in a list or within a string so this  is the counter function is in the collections module so if you remember that  the modules uh normally we need to import the modules into python and next we can use  that counter method or the counter function so it will return a dictionary where the elements  are stored as keys and also counts are stored as values okay so for example if we have lets see uh  a a and also string b in the list and if we bring that one to this counter function the return  result will be a dictionary where the key will be each item and the value will be the number of  times that they showed up in this list or in this screen so the a will be 2 and the b will have the  value of 1 okay in this case and the counter also has most common method so it will retain a  list of the top n elements so in this case the n is defined within this method okay so  five sum in this case if you want top one result will be just a only  okay because in this example there are two as in this string so if you  want top two and you will have a and also b okay uh so lets see one example that in python  okay so first lets import the counter so uh lets say from collections lets import  c is capitalized counter okay so now within this module we just import the function because  we just need that function so we can use from that module import counter and normally by default  the connections should be installed in most python editors okay so lets say that the count result  ethos so now we just use this count function and here lets just type at least lets say we  have um just type a list lets say we have several as and we have two of the letter  b and also one of the letter c okay so now lets print this result okay so  now you can see we have a dictionary that uh a has two there are two  three as two bs and also one c and we can also use that most common  so lets say were on print dot most common and so here we want the most common two  okay so we just want the the most common two and see how it look like okay so you can  see the most common will retain a list okay where uh each item is in a tuple okay  so um so here you can see it will return a list and each item is in the temple and  now we can use a for loop to iterate other um other table so fall result in  this and we can print the result okay so now we have the the word and  also account the word and also count that in each single tuple and since we know that there  are two items in each tuple so we can actually unpack this tuple so we see that for word  and also count and we can actually print word and also count all right so lets  do it so we have word a and also count is three and would be the count is  two so if we see most common one and we only have a all right so that is a  example thats using the counter function okay so here comes to my to our first exercise  so we have we have the gmu news that you txt fell so can you find out the top 10 most  common words in the gmu txt file okay so think about that with by combining the  counter function and also the open function and also waste and statement so can you find out the  top 10 most common words from the jmu txt file so you can pause a video here and also you can  try to figure out it on your own and also after lets say a few minutes so you can resume  the video and also you can see my solution okay so hopefully you you have you  have tried your own solution so here im going to demo my solution so first we have  to open that jmu word so we say waste open gmu install news.txt and in the  read mode as jmu and score news okay and lets lets get the  news content so lets news content it calls jmu news object dot read  okay and if we print the news content right we have this string okay we have this very  very long string and next so we want to convert this string into a list okay so we want convert  this uh string into a word list and you may asking that okay so the count function also works for  string so why dont you count the string directly because we are looking for the most common  words so if we see lets say result e cos count so if we apply the count directly onto  this string how will the result look like okay so it will count each single character as  an item and that is not what we want okay so what we want is that we want to count the single word  okay so we want lets see we want have a word list which it costs you know this content and we know  that to come that as a word list we can use that split function so we split and now if we print  that word list so now you can say we put each single word as an item in this list and the next  we can um count the word number of the words that in this word list so here lets see the result it  costs now we apply the counter to this word list okay so now if we print the result  you can see that end has been used uh 18 times 4 has been used seven times okay so  now we are close to our uh final uh result so however i was asking that i just want the  top 10 words okay so you can use the most common one measured to return the top 10 words so  lets say for word and also count in result.most common 10 colon lets print word and also  count okay so now lets write you can see its very nice so we have the top 10 the most  common words so and fall to the of okay and also uh number four okay and the president etcetera  okay so that answered our first exercise so lets uh say okay so this is exercise one all right okay so now lets i also want  to introduce another um uh master that is very also very common in python thats called  list comprehension so this comprehension will take iterable object such as strings or list and pass  each item to do something that we can create a new list okay so the syntax is like the new list  equals and we put that into this square bracket okay and we see that for each single item  okay for each single item in this iterable item for in this uh e3 ball object so  we can do something on each single item all right so that is a syntax so  so this may sound like a little bit complicated so lets see an example that in  python okay so lets say we define a number a number list equals lets say one two three four okay so  here lets say we want to generate a new list by using this list comprehension so thats  here i want to say that uh for i in this number of the list so we can do something okay for each  single item so that i will say i plus one okay so define action first and say for each single  item in that list so now it you will see that i and the first item is one well uh plus one  and thats where it costs two that will be the first item in this new list and two plus  one equals 3 3 will be the second item in this list and followed by 3 plus 1 equals 4 that is  third item and finally 4 plus 1 equals 5 so 5 will be the last item so now lets print this new  list here you can see we have this new list 92345 okay so that is a list comprehension so  on this may sound like a little bit tricky initially however that is very useful that  later that we we will use that a lot in python okay so now back to our uh jmu news so my second  question is that can you convert all the words into little cases by using the list comprehension  okay and you can pause the video here and also try it on your own and later on you can resume  the video and also we all do it together all right so here this is my solution so uh lets say we first we just copy this one  from our previous exercise so exercise two okay uh so we open the gmu news and also  we load that one into this gmu news content and we get this word list okay so now we can  use this list comprehension we see that lower case list in course so lets create a list so  we say for word in this word and store list so here we are going to define the actions on those  on each single word so we want to convert that into lower cases so if you remember that for  string we do have the lower function a method okay after doing that now if  we print this low case list so now we will convert all the word into lower  cases okay and if you are interested you can also just copy the code previously and now  you can count the most common word okay uh in this lowercase list so lets  see if we still have the same result okay so now in this case you can see  and actually is the most popular one and followed by four the two etc okay so that is  also a list comprehension and finally i will also want to talk about the string format okay so  string format is a way that uh you can you can control what you want to display in your string  the final output that basically means that strings that contain this replacement field that are  strong are surrounded by this curly brackets okay so anything outside of the curly  bracket is considered those literal text and which will be copied unchanged to the output  okay so lets see one example lets say that print now we define a string within this string we have  this first and a second pair of the curly bracket and its calm is and dot format so now  we provide okay so what will be used to replace those two curly bracket so the first  will be tom will be used here the second one is this one so 60 000 will be replaced  here so the output will be time income is dollar okay so that will be the output of the string  format so this is the very useful that especially in the next semester so when we are talking  about sql so we will use stream format a lot and in string format you can also specify the  order of this replacement field so for example if you tap number one for the first curly bracket  and also number zero for the second bracket so the first item will go to the curly bracket that  contains zero and also the second item will go to this curly bracket that contains one remember  that in python the index starts from zero okay so lets just simply try those two  string format uh in python say print okay so lets type curly bracket is so now we type the dollar okay period okay so  this we just finished a string that has two plates that need to be filled uh need to be replaced  so that after string dot format so first well use tom and second one will be a  number so that is sixty thousand okay so if you write you can see tom is now  replace the first pair of this curly bracket and if you see another one so lets see uh  jim now you can see jim is now replaced here and you can also specify the  orders five semi if you want the first item go here and the second item go here  and you can do that so now if you write you can see sixty thousand in the second item so that  will replace this curly bracket and james is a first item that will replace this curly bracket  so if you switch the position in this format okay and you will have the  right sentence being printed out

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically