In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the skills of Python development". The content of the explanation is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "what are the skills of Python development".
1. List derivation
You have a list:bag = [1,2,3,4,5]
Now you want to double all the elements and make it look like this: [2, 4, 6, 8, 10]
Most beginners, based on previous language experience, will probably do this.
Bag = [1,2,3,4,5]
For i in range (len (bag)):
Bag [I] = bag [I] * 2
But there is a better way:
Bag = [elem * 2 for elem in bag]
Is it right to be concise? This is called the list derivation of Python.
two。 Traverse the list
It's still the list above. Avoid doing this as much as possible:
Bag = [1,2,3,4,5]
For i in range (len (bag)):
Print (bagi)
Instead, it should be like this:
Bag = [1,2,3,4,5]
For i in bag:
Print (I)
If x is a list, you can iterate over its elements. In most cases you don't need an index of each element, but if you have to, use the enumerate function. It looks like the following:
Bag = [1,2,3,4,5]
For index, element in enumerate (bag):
Print (index, element)
It's very straightforward.
3. Element exchange
If you transferred from java or C to Python, you may be used to this:
A = 5
B = 10
# swap an and b
Tmp = a
A = b
B = tmp
But Python offers a more natural and better way!
A = 5
B = 10
# swap an and b
A, b = b, a
4. Initialization list
If you want a list of 10 integer zeros, you may first think of:
Bag = []
For _ in range (10):
Bag.append (0)
Let's do it another way:
Bag = [0] * 10
Look, how elegant.
Note: if your list contains a list, doing so will result in a shallow copy.
For example:
Bag_of_bags = [[0]] * 5 # [0], [0], [0], [0], [0]]
Bag_of_bags [0] [0] = 1 # [[1], [1], [1], [1]]
Oops! All the lists have changed, and we just want to change the first list.
Change it:
Bag_of_bags = [[0] for _ in range (5)]
# [[0], [0], [0], [0], [0]]
Bag_of_bags [0] [0] = 1
# [[1], [0], [0], [0], [0]]
"premature optimization is the root of all evil."
Ask yourself, is it necessary to initialize a list?
5. Construction string
You will often need to print strings. If there are many variables, avoid the following:
Name = "Raymond"
Age = 22
Born_in = "Oakland, CA"
String = "Hello my name is" + name + "and Isimm" + str (age) + "years old. I was born in" + born_in + "."
Print (string)
Well, how messy does this look? You can use a nice and concise method instead of .format.
Do this:
Name = "Raymond"
Age = 22
Born_in = "Oakland, CA"
String = "Hello my name is {0} and Isimm {1} years old. I was born in {2}." .format (name, age, born_in)
Print (string)
6. Returns tuples (tuple)
Python allows you to return multiple elements in a function, which makes life easier. However, there are common errors such as logging out when unpacking tuples:
Def binary ():
Return 0, 1
Result = binary ()
Zero = result [0]
One = result [1]
It's not necessary. You can change it like this:
Def binary ():
Return 0, 1
Zero, one = binary ()
If you need all elements to be returned, use an underscore _:
Zero, _ = binary ()
It's so efficient!
7. Access Dicts (dictionary)
You will also often write key,value (key, value) to dicts.
If you try to access a key that does not exist in dict, you may be inclined to do this to avoid KeyError errors:
Countr = {}
Bag = [2, 3, 1, 2, 5, 6, 7, 9, 2,7]
For i in bag:
If i in countr:
Countr [I] + = 1
Else:
Countr [I] = 1
For i in range (10):
If i in countr:
Print ("Count of {}: {}" .format (I, please [I]))
Else:
Print ("Count of {}: {}" .format (I, 0))
However, using get () is a better approach.
Countr = {}
Bag = [2, 3, 1, 2, 5, 6, 7, 9, 2,7]
For i in bag:
Countr [I] = countr.get (I, 0) + 1
For i in range (10):
Print ("Count of {}: {}" .format (I, countr.get (I, 0)
Of course, you can use setdefault instead.
This is also done in a simpler but more expensive way:
Bag = [2, 3, 1, 2, 5, 6, 7, 9, 2,7]
Countr = dict ([(num, bag.count (num)) for num in bag])
For i in range (10):
Print ("Count of {}: {}" .format (I, countr.get (I, 0)
You can also use dict deduction.
Countr = {num: bag.count (num) for num in bag}
These two methods are expensive because they traverse the list each time count is called.
8 use the library
Just import the existing library and you can do what you really want to do.
Again, in the previous example, we build a function to count the number of times a number appears in the list. So, there is already a library that can do such a thing.
From collections import Counter
Bag = [2, 3, 1, 2, 5, 6, 7, 9, 2,7]
Countr = Counter (bag)
For i in range (10):
Print ("Count of {}: {}" .format (I, please [I]))
Some reasons for using the library:
The code is correct and tested.
Their algorithms may be optimal, so they can run faster.
Abstraction: they are clear and document-friendly, and you can focus on those that have not yet been implemented.
Finally, it's already there, and you don't have to recreate the wheels.
9. Slice / step in the list
You can specify start points and stop points, like this list [start:stop:step]. We take out the first five elements in the list:
Bag = [0,1,2,3,4,5,6,7,8,9]
For elem in bag [: 5]:
Print (elem)
This is the slice. We specify that the stop point is 5, and before we stop, we will take five elements from the list.
What about the last five elements?
Bag = [0,1,2,3,4,5,6,7,8,9]
For elem in bag [- 5:]:
Print (elem)
Don't you get it? -5 means taking five elements from the end of the list.
If you want to interval the elements in the list, you might do this:
Bag = [0,1,2,3,4,5,6,7,8,9]
For index, elem in enumerate (bag):
If index% 2 = = 0:
Print (elem)
But here's what you should do:
Bag = [0,1,2,3,4,5,6,7,8,9]
For elem in bag [:: 2]:
Print (elem)
# or use ranges
Bag = list (range (0pm 10pm 2))
Print (bag)
This is the step in the list. List [:: 2] means traversing the list and fetching an element in two steps at the same time.
You can use list [::-1] to flip the cool list.
10. Tab or spacebar
In the long run, mixing tab with whitespace will cause a lot of unnecessary trouble, you will see IndentationError: unexpected indent. Whether you choose the tab key or the spacebar, you should always use it in your files and projects.
One reason to use spaces instead of tab is that tab is not the same in all editors. Depending on the editor used, tab may be treated as 2 to 8 spaces.
You can also define tab with spaces when writing code. In this way, you can choose to use a few spaces as tab. Most Python users use four spaces.
Thank you for your reading, the above is the content of "what are the skills of Python development?" after the study of this article, I believe you have a deeper understanding of the skills of Python development, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.