Home > Article > Backend Development > Python programming JSON format conversion, flexible use of else statements and detailed explanation of setdefault method
This article mainly introduces three commonly used techniques in summarizing Pythonprogramming, including JSON format conversion, the use of else statements and the use of the setdefault method. Friends who need it can For reference,
You can see some common tricks in python code. Here is a simple summary.
json StringFormatting
Json strings are often used when developing web applications, but a relatively long json string can The readability is poor, and it is difficult to see the structure inside. At this time, you can use python to print the json string beautifully.
root@Exp-1:/tmp# cat json.txt {"menu": {"breakfast": {"English Muffin": {"price": 7.5}, "Bread Basket": {"price": 20, "desc": "Assortment of fresh baked fruit breads and muffins"}, "Fruit Breads": {"price": 8}}, "drink": {"Hot Tea": {"price": 5}, "Juice": {"price": 10, "type": ["apple", "watermelon", "orange"]}}}} root@Exp-1:/tmp# root@Exp-1:/tmp# cat json.txt | python -m json.tool { "menu": { "breakfast": { "Bread Basket": { "desc": "Assortment of fresh baked fruit breads and muffins", "price": 20 }, "English Muffin": { "price": 7.5 }, "Fruit Breads": { "price": 8 } }, "drink": { "Hot Tea": { "price": 5 }, "Juice": { "price": 10, "type": [ "apple", "watermelon", "orange" ] } } } } root@Exp-1:/tmp#
The wonderful use of else
In some scenarios we need to judge whether we are breaking out of a for loop, and Only handle the situation where break occurs. At this time, our usual approach is to use a flag variable to identify whether it is jumping out of the for loop. As in the example below, check if there are multiples of 17 between 60 and 80.
flag = False for item in xrange(60, 80): if item % 17 == 0: flag = True break if flag: print "Exists at least one number can be pided by 17"
In fact, you can use else at this time to achieve the same effect without introducing new variables
for item in xrange(60, 80): if item % 17 == 0: flag = True break else: print "exist"
setdefault method
dictionary is a python one A very powerful built-in data structure, but it is still inconvenient to use. For example, when there are multiple levels of nesting, we usually write it like this
dyna_routes = {} method = 'GET' whole_rule = None # 一些其他的逻辑处理 ... if method in dyna_routes: dyna_routes[method].append(whole_rule) else: dyna_routes[method] = [whole_rule]
In fact, there is a simpler way of writing that can achieve the same effect.
self.dyna_routes.setdefault(method, []).append(whole_rule)
Or you can use the collections.defaultdict module
import collections dyna_routes = collections.defaultdict(list) ... dyna_routes[method].append(whole_rule)
The above is the detailed content of Python programming JSON format conversion, flexible use of else statements and detailed explanation of setdefault method. For more information, please follow other related articles on the PHP Chinese website!