Dictionary derivation, list push

Keywords: Python

List derivation and dictionary derivation of python

1. List derivation

A list derivation is a conditional derivation used in conjunction with a loop to return a list, and the entire expression needs to be within [] because the return value is also a list

Requirement: multiply the even number from 0 to 20 by 10, the odd number by 100, and return all the calculations
 Results. The sample code is as follows: (implemented by list derivation)
list2 = [x*10 if x%2 == 0 else x*100 for x in range(0,21) ]
print(list2)
print(type(list2))

result

[0, 100, 20, 300, 40, 500, 60, 700, 80, 900, 100, 1100, 120, 1300, 140, 1500, 160, 1700, 180, 1900, 200]
<class 'list'>

Maybe many children have questions. I know the functions that can be realized by the for loop. Why do I use this to deduce? The efficiency of list derivation is much higher than for loop. It may take only 0.0002 seconds for cpu to execute a print("Hello world") sentence. You may not feel the gap. What if you need to output 100 million Hello world times? Often the details feel success or failure!

2. Dictionary derivation

In the late stage of crawler, when the cookie splits the string to pass the dictionary value, the dictionary derivation is often used, which is simple, convenient and practical.

  • analysis
  1. In string cookies, key is in front of '=', value is after '=', each ';' constitutes a key value pair; multiple key value pairs constitute a dictionary;
  2. Split the string into lists according to ';';
  3. According to the list obtained in the first step, each string will be split again according to '=' during traversal;
  4. According to the split result of the second step, the first element of the list is the key, and the second element of the list is the value;
cookies = "anonymid=jy0ui55o-u6f6zd; depovince=GW; _r01_=1; JSESSIONID=abcMktGLRGjLtdhBk7OVw; " \
          "ick_login=a9b557b8-8138-4e9d-8601-de7b2a633f80; _ga=GA1.2.1307141854.1562980962; " \
          "_gid=GA1.2.201589596.1562980962; _c1=-100; first_login_flag=1; ln_uact=18323008898; " \
          "ln_hurl=http://head.xiaonei.com/photos/0/0/men_main.gif; " \
          "jebe_key=88f1340c-592c-4dd6-a738-128a76559f45%7Cad33b3c730fcdc8df220648f0893e840%7C1562981108370%7C1" \
          "%7C1562981106763; jebe_key=88f1340c-592c-4dd6-a738-128a76559f45%7Cad33b3c730fcdc8df220648f0893e840" \
          "%7C1562981108370%7C1%7C1562981106765; jebecookies=793eb32e-92c6-470d-b9d0-5f924c335d30|||||; " \
          "_de=E77807CE44886E0134ABF27E72CFD74F; p=a00d65b1f779614cd242dc719e24c73e0; " \
          "t=292ba8729a4151c1a357e176d8d91bff0; societyguester=292ba8729a4151c1a357e176d8d91bff0; id=969937120; " \
          "xnsid=1700b2cc; ver=7.0; loginfrom=null; wp_" \
          "fold=0 "
dicts = {cookie.split("=")[0]: cookie.split("=")[1] for cookie in cookies.split(";")}
print(dicts)

result

{'anonymid': 'jy0ui55o-u6f6zd', ' depovince': 'GW', ' _r01_': '1', ' JSESSIONID': 'abcMktGLRGjLtdhBk7OVw', ' ick_login': 'a9b557b8-8138-4e9d-8601-de7b2a633f80', ' _ga': 'GA1.2.1307141854.1562980962', ' _gid': 'GA1.2.201589596.1562980962', ' _c1': '-100', ' first_login_flag': '1', ' ln_uact': '18323008898', ' ln_hurl': 'http://head.xiaonei.com/photos/0/0/men_main.gif', ' jebe_key': '88f1340c-592c-4dd6-a738-128a76559f45%7Cad33b3c730fcdc8df220648f0893e840%7C1562981108370%7C1%7C1562981106765', ' jebecookies': '793eb32e-92c6-470d-b9d0-5f924c335d30|||||', ' _de': 'E77807CE44886E0134ABF27E72CFD74F', ' p': 'a00d65b1f779614cd242dc719e24c73e0', ' t': '292ba8729a4151c1a357e176d8d91bff0', ' societyguester': '292ba8729a4151c1a357e176d8d91bff0', ' id': '969937120', ' xnsid': '1700b2cc', ' ver': '7.0', ' loginfrom': 'null', ' wp_fold': '0 '}

Posted by seaten on Wed, 17 Jun 2020 19:56:14 -0700