In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "what are the useful Python skills". In the operation of practical cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Continuous function call
Def add (x): class AddNum (int): def _ call__ (self, x): return AddNum (self.numerator + x) return AddNum (x) print add (2) (3) (5) # 10 print add (2) (3) (4) (5) (6) (7) # 27 # javascript version var add = function (x) {var addNum = function (x) {return add (addNum + x) }; addNum.toString = function () {return x;} return addNum;} add (2) (3) (5) / / 10 add (2) (3) (3) (4) (5) (6) / / 27
Default value trap
> def evil (v = []): V.append (1)... Print v. > > evil () [1] > evil () [1,1]
Read and write csv files
Import csv with open ('data.csv',' rb') as f: reader = csv.reader (f) for row in reader: print row # writes import csv with open ('data.csv',' wb') as f: writer = csv.writer (f) writer.writerow (['name',' address'') to the csv file 'age']) # single-line data = [(' xiaoming', 'china','10'), (' Lily', 'USA',' 12')] writer.writerows (data) # multiple-line write
Number system conversion
> int ('1000, 2) 8 > int (' Aguilar, 16) 10
Format json
Echo' {"k": "v"}'| python-m json.tool
List flattening
List_ = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] [k for i in list_ for k in i] # [1, 2, 3, 4, 5, 8, 9] import numpy as np print np.r_ [[1, 2, 3], [4, 5, 6], [7, 8, 9] import itertools print list (* [[1, 2, 3], [4, 5, 6] [7,8,9]) sum (list_, []) flatten = lambda x: [y for l in x for y in flatten (l)] if type (x) is list else [x] flatten (list_)
List merger
> a = [1,3,5,7,9] > > b = [2,3,4,5,6] > > c = [5,6,7,8,9] > > list (set (). Union (a, b, c)) [1, 2, 3, 4, 5, 6, 7, 8, 9]
The two letters that appear the most
From collections import Counter c = Counter ('hello world') print (c.most_common (2)) # [(' lumped, 3), ('oiled, 2)]
Use with caution
Eval ("_ _ import__ ('os'). System (' rm-rf /')", {})
Permutation matrix
Matrix = [[1,2,3], [4,5,6]] res = zip (* matrix) # res = [(1,4), (2,5), (3,6)]
List derivation
[item**2 for item in lst if item% 2] map (lambda item: item**2, filter (lambda item: item% 2, lst) > list (map (str, [1, 2, 3, 4, 5, 6, 7, 8, 9]) [1, 2, 3, 3, 9]) [1, 2, 3, 3, 4, 4, 5, 6, 7, 8, 9]
Permutation and combination
> for p in itertools.permutations ([1,2,3,4]):. Print''.join (str (x) for x in p)... 1234 1243 1324 1342 1423 1432 2134 2143 2314 2341 2413 2431 3142 3214 3241 3412 3421 4123 4132 4213 4231 4312 4321 > > for c in itertools.combinations ([1,2,3,4,5], 3):. Print'. Join (str (x) for x in c)... 123 124 125 134 135 145 234 235 245 345 > > for c in itertools.combinations_with_replacement ([1, 2, 3], 2): Print''.join (str (x) for x in c)... 11 12 13 22 23 33 > for p in itertools.product ([1,2,3], [4,5]): (1,4) (1,5) (2,4) (2,5) (3,4) (3,5)
Default dictionary
> m = dict () > > m ['a'] Traceback (most recent call last): File "", line 1 In KeyError:'a'> m = collections.defaultdict (int) > m ['a'] 0 > m ['b'] 0 > > m = collections.defaultdict (str) > m ['a']'> > m ['b'] + ='a' > > m ['b']'a' > > m = collections.defaultdict (lambda:'[default value]') > > m ['a']'[default value]'> > m ['b']'[default value]''
Reverse dictionary
> m = {'averse: 1,' baked: 2, 'caged: 3,' dashed: 4} > m {'k for k: 4, 'averse: 1,' baked: 2, 'crested: 3} > > {v: k for k, v in m.items ()} {1:' ajar, 2: 'baked, 3:' clocked, 4:'d'} what are the useful Python techniques? Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.