Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the 10 magic techniques of Python?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Python's 10 magic skills are which, many novices are not very clear, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.

Although on the face of it, Python seems to be a simple language that anyone can learn, it is true that many people may be surprised to know that a person can master the language. Python is one of the things that is easy to learn, but may be difficult to master. In Python, there are usually many ways to deal with it, but it's easy to do something wrong, or reinvent the standard library and waste time simply because you don't know the module exists.

Unfortunately, the Python standard library is a huge beast, and its ecosystem is absolutely huge. Although there may be 20 gigabytes of Python modules, you can learn some useful techniques using the standard libraries and software packages that are usually associated with scientific computing in Python.

№ 1: reverse string

Although it seems basic, using char loops to reverse strings can be tedious and annoying. Fortunately, Python includes a simple built-in operation to perform this task precisely. To do this, we only need to access the index::-1 on the string.

A = "! dlrow olleH" backward = a [::-1]

№ 2: dimming as a variable

In most languages, in order to put an array into a set of variables, we need to iterate through values or access dark spots by location, as follows:

Firstdim = array [1]

However, in Python, there is a cooler approach. To change the values list to a variable, we can simply set the variable name to equal the array, and the array length is the same:

Array = [5, 10, 15, 20] five, ten, fift, twent = array

№ 3:Itertools

If you plan to spend any time on Python, you will definitely want to be familiar with itertools. Itertools is a module in the standard library that allows you to constantly solve iterative problems. It not only makes it much easier to write complex loops, but also makes your code faster and more concise. This is just an example of the use of Itertools, but there are hundreds:

C = [[1,2], [3,4], [5,6]] # Let's convert this matrix to a 1 dimensional list.import itertools as itnewlist = list (it.chain.from_iterable (c))

№ 4: open the box intelligently

Iteratively unzipping values can be time-consuming and laborious. Fortunately, Python has several cool ways to extract lists! One example is *, which populates unassigned values and adds them to a new list under the variable name.

A, * b, c = [1, 2, 3, 4, 5]

№ 5: enumeration

If you don't know about enumerations, you should probably be familiar with it. Enumeration will allow you to get the index of some values in the list. This is especially useful in data science when using arrays instead of data frameworks.

For iMagol w in enumerate (array): print (iMagne w)

№ 6: name slice

It's easy to split a list in Python! A variety of excellent tools are available, but one feature that is certainly valuable is the ability to name fragments of the list. This is particularly useful for linear algebra in Python.

A = [0,1,2,3,4,5] LASTTHREE = slice (- 3, None) slice (- 3, None, None) print (a [lastest])

№ 7: group adjacency list

Of course it's fairly easy to group adjacent loops in a for loop, especially by using zip (), but this is certainly not the best way to handle it. To make things easier and faster, we can use zip to write a lambda expression that groups our adjacent lists as follows:

A = [1,2,3,4,5,6] group_adjacent = lambda a, k: zip (* ([iter (a)] * k) group_adjacent (a, 3) [(1,2,3), (4,5,6)] group_adjacent (a, 2) [(1,2), (3,4), (5,6)] group_adjacent (a, 1)

№ 8: next () iteration of the generator

In most normal cases of programming, we can use a counter to access an index and get our position number, which will simply be added to the following values:

Array1 = [5,10,15,20] array2 = (x * * 2 for x in range (10)) counter = 0for I in array1: # This code wouldn't work because 'i' is not in array2. # I = array2 [I] I = array2 [counter] # ^ ^ This code would because we are accessing the position of i

However, we can use next () instead. Next uses an iterator that stores the current location in memory and traverses the list in the background.

G = (x * * 2 for x in range (10)) print (next (g)) print (next (g))

№ 9:Counter

Another great module in the standard library is the collection, and what I want to show you today is the counters in the collection. With Counter, we can easily get the count of the list. This is useful for getting the total number of values in the data, getting the empty count of the data, and viewing the unique values of the data. I know what you're thinking.

"Why not just use Pandas?"

This is of course the correct point of view. However, automating with Pandas is undoubtedly more difficult and is just another dependency that needs to be added to the virtual environment when deploying the algorithm. In addition, the counter type in Python has many features that the Pandas series does not have, which makes it more useful in some cases.

A = collections.Counter ([1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 5, 6, 7]) A Counter ({3: 4, 1: 2, 2: 2, 4: 1, 5: 1, 6: 1, 7: 1}) A.most_common (1) [(3,4)] A.most_common (3) [(3,4), (1, 2), (2, 2)]

№ 10: out of the team

Another great thing in the collection module is getting out of the team. Look at all the neat things we can do with this type!

Import collections Q = collections.deque () Q.append (1) Q.appendleft (2) Q.extend ([3,4]) Q.extendleft ([5,6]) Q.pop () Q.popleft () Q.rotate (3) Q.rotate (- 3) print (Q) does it help you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report