Categories
Python Answers

How to sort two lists which reference each other in the exact same way with Python?

Sometimes, we want to sort two lists which reference each other in the exact same way with Python.

In this article, we’ll look at how to sort two lists which reference each other in the exact same way with Python.

How to sort two lists which reference each other in the exact same way with Python?

To sort two lists which reference each other in the exact same way with Python, we can use the zip and sorted functions.

For instance, we write

list1, list2 = (list(t) for t in zip(*sorted(zip(list1, list2))))

to call zip with with 2 lists to create a list with tuples with items in the 2 lists at the same position.

Then we call sorted to sort the items in the tuples list.

Next, we call zip again with the sorted tuples as the argument to create a tuple with 2 tuples with the items extracted from the tuples at each position and put into a tuple.

Then we unpack the lists by assigning them back to list1 and list2.

Conclusion

To sort two lists which reference each other in the exact same way with Python, we can use the zip and sorted functions.

Categories
Python Answers

How to get POST and GET variables in Python?

Sometimes, we want to get POST and GET variables in Python.

In this article, we’ll look at how to get POST and GET variables in Python.

How to get POST and GET variables in Python?

To get POST and GET variables in Python, we can use the cgi library.

For instance, we write

import cgi

form = cgi.FieldStorage()
print(form["username"])

to get the form field values with

cgi.FieldStorage()

in a dict.

Then we get the value of the form data with field name username with

form["username"]

It should be set when we submit a value from an input like

<input type="text" name="username">

Conclusion

To get POST and GET variables in Python, we can use the cgi library.

Categories
Python Answers

How to convert JSON string to dict using Python?

Sometimes, we want to convert JSON string to dict using Python.

In this article, we’ll look at how to convert JSON string to dict using Python.

How to convert JSON string to dict using Python?

To convert JSON string to dict using Python, we can use the json.loads method.

For instance, we write

import json

# ...

d = json.loads(j)
print(d['glossary']['title'])

to load the j JSON string into a dict with the json.loads method and assign the returned dict to d.

Then we can get the value we’re looking for from the dict d.

Conclusion

To convert JSON string to dict using Python, we can use the json.loads method.

Categories
Python Answers

How to call Python max function using ‘key’ and lambda expression?

Sometimes, we want to call Python max function using ‘key’ and lambda expression.

In this article, we’ll look at how to call Python max function using ‘key’ and lambda expression.

How to call Python max function using ‘key’ and lambda expression?

To call Python max function using ‘key’ and lambda expression, we can call max with the list and the key argument set to a function that returns the value that we want to sort by.

For instance, we write

def func(p):
   return p.total_score

m = max(players, key=func)

to call max with the players list and the key set to the func function.

In func, we return the total_score function from the p object that’s in the players list to sort by the total_score value from each object in players and get the item with the max value of total_score.

Conclusion

To call Python max function using ‘key’ and lambda expression, we can call max with the list and the key argument set to a function that returns the value that we want to sort by.

Categories
Python Answers

How to convert a PIL Image into a numpy array with Python?

Sometimes, we want to convert a PIL Image into a numpy array with Python.

In this article, we’ll look at how to convert a PIL Image into a numpy array with Python.

How to convert a PIL Image into a numpy array with Python?

To convert a PIL Image into a numpy array with Python, we call numpy.array with the PIL image.

For instance, we write

pix = numpy.array(pic)

to call numpy.array with the pic PIL image object to return an array with the pixel data of pic.

Conclusion

To convert a PIL Image into a numpy array with Python, we call numpy.array with the PIL image.