Dictionary in python :
Dictionary is basically key value binding .
As String is enclosed within " "
As List is enclosed within [ ]
As tuple is enclosed within ( )
Here dictionary ,it is enclosed within { }
In String , list tuple , also we have this same idea ,,, but there key is always an integer ... so we call it index
So Stringname or listname or tuplename [1] means , item at index 1
Dictionary gives scope for having having even non integer keys
To declare a Dictionary : d = { }
>>d['a'] = 'alpha'
>>d['o']='omega'
>>d['g']='gamma'
this is the way to assign value in a dictionary .
Now, to retrieve a value in String or list or tuple , what will u do ,? name[index]
similarly here >>d['a'] displays 'alpha'
The advantage of having key and corresponding value storage is that , it is fast at retrieval using key ,,
this is the advantage of hashtable
But don't know why it is faster in this way .........
If you give d['x'] , it flings an error , as the key x is not there in dictionary d .
So , what can be the way to bypass , this even if the key is not there , but still , not flag an error ,,
this can be used in future to check if a key exists in a dictionary ..
Ya, we have a 2 solutions
1) d.get['x] returns value null if key x is not found and returns the actual corresponding value if the key exists
>>d.get['a'] returns 'alpha'
2) As , we do in lists is ,a =[1,2,3]
to find if 2 is a value inside it ,, we say 2 in a , returns true if 2 value is there (note that here we r searching by value , so this time is longer )
Here also same thing
'a' in d returns true if key 'a' exists , false if it doesn't exist
To see the list of keys at once in a dictionary
d.keys()
To see the list of values at once in a dictionary
d.values()
To get values ,,, just loop over
for k in d.keys() :
print d[k]
to have it ordered
for k in sorted(d.keys()):
print d[k]
To see list of key value pair .
d.items() ...............returns
[('a','alpha') , ('o','omega'),('g','gamma')]
looping over ? ya , easy
for tuple in d.items() :
print tuple
File operation in Python :
f = open(filename , 'r')the second argument indicates , whether we r opening file for reading or 'w' denotes 'writing .. similary we have 'rU'
f is here the file handler ,,, by this line we just open the file and give it to f ,, from f we can read it
now we can iterate each line ,,as soon as it sees a new line in f ( which is a reflection of file filename which is open ), it stops ,reading ,
like this we iterate over every line
for line in f:
print line
prints each line but with double line space between
this is because print itself has a new line and the new line in file adds to it ,,
so to eliminate it replace print statement by
print line,
the trailing comma removes this problem as it removes the new line in the print
finally , we have to close the open file ,,as a open file unnecessarily draws memory
f.close()
if u omit this line , when the process ends the file is closed automatical
but in case of softwares which r continuously running , the process also runs , so here if we don't close, it remains open the whole period of time , when software runs ,and that too unnecessarily
what is the advantage of having this concept of reading line by line ? why not read at once
when we read , big files say 90 Gb ,, reading at once gives problem of locking heavy memory
when we read line by line only a small part of memory is locked for its work ,
f.readlines()
gives o/p as a list of lines
['line1' ,'line2','line3'...]
it even prints it in the same way as shown above
f.read()
reads entire file into a single string
so has same view as we open a file in editor in gui
No comments:
Post a Comment