python: changing the value of a specific column in 2-d list

I have a 2-d list (list of 2-tuples):

results = [("a", 0.1234), ("b", 0.32345), ("c", 0.54321)]

and I would like to change it to be:

>> [('a', '12.34'), ('b', '32.34'), ('c', '54.32')]

My thought was to do something like this:

results = [(x[0], "%.2f" % (x[1] * 100)) for x in results]

And it works, is there a better way of doing it (according to import this guidelines), as it seems that accessing the value x[0] is not efficient ugly. btw - I can change it to be a list of lists instead of tuples, if that changes anything e.g:

results = [["a", 0.1234], ["b", 0.32345], ["c", 0.54321]]

and respectively:

>> [['a', '12.34'], ['b', '32.34'], ['c', '54.32']]

Answers


You can use tuple unpacking:

[(a, "%.2f" % (b * 100.0)) for a, b in results]

Need Your Help

Not getting spanish characters (ú, í )but getting like (ó , Ã) on UI

java spring utf-8 character-encoding properties-file

I declared three locale conversions in three properties files like Strings.properties, Strings_es_ES.properties and Strings_en_GB.properties ( for US, ES and UK)

Avoid “Hibernate exception Null value was assigned to a property of primitive type setter” without wrappers

java hibernate hibernate-mapping hbmxml

Having a long field on an entity managed by hibernate could be dangerous with null values. When Hibernate tries to set a null into a primitive an exception will be thrown.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.