Wednesday, May 18, 2011
Monday, March 14, 2011
Flattening abitrarily nested lists in Python
Lists are one of the three most important data structures in Python, other being dict and str, (tuple is the younger cousin of list). The lists can be arbitrarily nested with any kind of data inside them, e.g.
['a', [1, 2], (3, 4, 5, [7, 8, ['gnu', 'linux']]), ['a', 'b', 'c'], [['d', 'e', 'f'], 12, [13, [14, [15,]]]]]
The objective is flatten nested lists of this kind to a single list. It can be simply stated as to remove all square brackets (and parenthesis) and retain only square brackets at the end, while maintaining the order of the items as they appear in the nested list. One of the simple solution can be:
Run this on the example and you get:
['a', 1, 2, 3, 4, 5, 7, 8, 'gnu', 'linux', 'a', 'b', 'c', 'd', 'e', 'f', 12, 13, 14, 15]
Can this be made more Pythonic and efficient. Any ides?
['a', [1, 2], (3, 4, 5, [7, 8, ['gnu', 'linux']]), ['a', 'b', 'c'], [['d', 'e', 'f'], 12, [13, [14, [15,]]]]]
The objective is flatten nested lists of this kind to a single list. It can be simply stated as to remove all square brackets (and parenthesis) and retain only square brackets at the end, while maintaining the order of the items as they appear in the nested list. One of the simple solution can be:
def flatten(l):
'''Flatten a arbitrarily nested lists and return the result as a single list.
'''
ret = []
for i in l:
if isinstance(i, list) or isinstance(i, tuple):
ret.extend(flatten(i))
else:
ret.append(i)
return ret
'''Flatten a arbitrarily nested lists and return the result as a single list.
'''
ret = []
for i in l:
if isinstance(i, list) or isinstance(i, tuple):
ret.extend(flatten(i))
else:
ret.append(i)
return ret
Run this on the example and you get:
['a', 1, 2, 3, 4, 5, 7, 8, 'gnu', 'linux', 'a', 'b', 'c', 'd', 'e', 'f', 12, 13, 14, 15]
Can this be made more Pythonic and efficient. Any ides?
Tuesday, December 14, 2010
mplayer: volume trick
When you want to view a movie or listen to a song which has a squeaky audio the easy option is to use VLC player to set the volume to the max of 400%.
MPlayer by default gives no option for the volume boost. However you can simply make the volume go to any level you want. You just need to set the following options in the ~/.mplayer/config file or the config file in the mplayer install directory on Windows:
Here 400 signifies that maximum volume can be 400%. You can specify any value for this so have custom control over maximum value of the volume.
MPlayer by default gives no option for the volume boost. However you can simply make the volume go to any level you want. You just need to set the following options in the ~/.mplayer/config file or the config file in the mplayer install directory on Windows:
softvol=yes
softvol-max=400
Here 400 signifies that maximum volume can be 400%. You can specify any value for this so have custom control over maximum value of the volume.
Monday, November 22, 2010
A Better Education System
Right now our education system is a class/standard based education system. Means for for the given year a student has to study the given number of subjects and if he/she doesn't pass one of the subjects he/she has to repeat the year. Also the students are graded on the basis of the cumulative percentage of the marks they get.
In this big range of figures what is hidden is that a student might not be interested/weak/slow-learner for a given subject. This takes toll from other subjects which he is good at. Its better that he gives exams when he thinks he is ready. Rather giving it at fixed time of the year.
Another opposite of the above is that a given student is good/strong/fast-learner at a given subject and still he has to stick with rest of the class and wait for the final exams.
Why not students have a choice to give the exams when they are ready. This might look like a havoc on the system. But if we consider that not all students will be giving the exams at the same time, for a given time it will be some fraction, (consider GRE, TOEFL etc.)
Consider another option students have to stick with the class/standard based system even if he is good enough to solve the problems of a given subject from higher classes/standard. Rather than having some subjects required to pass to move to the next class/standard its better if we have class/standard associated with each subject and a student has to clear the exams of that subject to be awarded certificate of learning that subject.
Consider the case of colleges asking for the students to have passed class 10+2 with some subjects. Presently students has to pass the whole 10+2. Why not simply the college asking for certificate of 10+2 for the required subjects.
Here is an infographic:
In this big range of figures what is hidden is that a student might not be interested/weak/slow-learner for a given subject. This takes toll from other subjects which he is good at. Its better that he gives exams when he thinks he is ready. Rather giving it at fixed time of the year.
Another opposite of the above is that a given student is good/strong/fast-learner at a given subject and still he has to stick with rest of the class and wait for the final exams.
Why not students have a choice to give the exams when they are ready. This might look like a havoc on the system. But if we consider that not all students will be giving the exams at the same time, for a given time it will be some fraction, (consider GRE, TOEFL etc.)
Consider another option students have to stick with the class/standard based system even if he is good enough to solve the problems of a given subject from higher classes/standard. Rather than having some subjects required to pass to move to the next class/standard its better if we have class/standard associated with each subject and a student has to clear the exams of that subject to be awarded certificate of learning that subject.
Consider the case of colleges asking for the students to have passed class 10+2 with some subjects. Presently students has to pass the whole 10+2. Why not simply the college asking for certificate of 10+2 for the required subjects.
Here is an infographic:
![]() |
| Subjects in pipeline |
Friday, July 09, 2010
Zoho: Beyond Google Apps
For those of you using Google apps (with Gmail account or as a Google App user), Google may be the best in search but not in all spheres.
If you are the one who is constantly using the web apps for daily purposes and want something better than the basic feature set take a look at the website:
www.zoho.com
It provides the complete suite. All the applications complementary to the Google docs, spreadsheets, forms and presentation are available. They are packed with a lot more features. And the good thing about it is you can login nto it using the Google account.
Give it a try and you won't be disappointed.
If you are the one who is constantly using the web apps for daily purposes and want something better than the basic feature set take a look at the website:
www.zoho.com
It provides the complete suite. All the applications complementary to the Google docs, spreadsheets, forms and presentation are available. They are packed with a lot more features. And the good thing about it is you can login nto it using the Google account.
Give it a try and you won't be disappointed.
![]() |
| Zoho services |
Saturday, March 13, 2010
One More Step To Prevent Your Privacy
We generate large amount of data while we are connected to the Internet. It not only records with whom we talk, chat or share content but also about the habits we have. The data about us being put into the corporate use.
For those who are uninitiated, its worthwhile to note that your login account is not the only thing by which you are being tracked. The advertisement serving websites also put up cookies on your computer to identify you. To record your clicking habits and serve you the advertisements that recognize what are the things that you are more inclined to buy.
One option to get away from this to install AdBlock in Firefox (and similar addon or plugin in other browsers) to turn off the ads alltogether but this hurts the websites that give you the free content.
Google with the option to opt-out of the profiling of the user habits and serve the independent ads only (link). However, this has not been so much publicised and very small percentage of people have done so. Additionally a plugin is also available to install with your browser to make the opt out permanent.
The best option is to visit National Advertising Initiative and opt-out of the target advertising from all kinds of ads serving services.
For those who are uninitiated, its worthwhile to note that your login account is not the only thing by which you are being tracked. The advertisement serving websites also put up cookies on your computer to identify you. To record your clicking habits and serve you the advertisements that recognize what are the things that you are more inclined to buy.
One option to get away from this to install AdBlock in Firefox (and similar addon or plugin in other browsers) to turn off the ads alltogether but this hurts the websites that give you the free content.
Google with the option to opt-out of the profiling of the user habits and serve the independent ads only (link). However, this has not been so much publicised and very small percentage of people have done so. Additionally a plugin is also available to install with your browser to make the opt out permanent.The best option is to visit National Advertising Initiative and opt-out of the target advertising from all kinds of ads serving services.
Subscribe to:
Posts (Atom)



