1. Structure stringYou will often need to print strings. If you have many variables, avoid the following: name = "Raymond" age = 22 born_in = "Oakland, CA" string = "Hello my name is " + name + "and I'm " + str(age) + " years old. I was born in " + born_in + "." print(string) How messy does this look? You can use a nice and simple alternative to .format. as follows: name = "Raymond" age = 22 born_in = "Oakland, CA" string = "Hello my name is {0} and I'm {1} years old. I was born in {2}.".format(name, age, born_in) print(string) 2. Return tuple def binary(): return 0, 1 result = binary() zero = result[0] one = result[1] This is not necessary, you can just do 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! 3. Access Dict dictionary You will also often write If you try to access a 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, countr[i])) else: print("Count of {}: {}".format(i, 0)) However, using 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 also use There is also a simpler but more expensive method: bag = [2, 3, 1, 2, 5, 6, 7, 9, 2, 7] {2: 3, 3: 1, 1: 1, 5: 1, 6: 1, 7: 2, 9: 1}: countr = dict([(num, bag.count(num)) for num in bag]) for i in range(10): print("Count of {}: {}".format(i, countr.get(i, 0))) We can also use countr = {num: bag.count(num) for num in bag} Both of these methods are expensive because they traverse the list every time 4. Use the libraryExisting libraries can be imported to do exactly what you want. Let's go back to the previous example and create a function to count the number of times a number appears in a list. Well, there used to be a library that could do this. 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, countr[i])) Some reasons to use libraries:
In the end, it was all there before, you don't have to reinvent the wheel. 5. Slicing/stepping through lists We can specify bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for elem in bag[:5]: print(elem) This is slicing. We specify What if the last 5 elements? bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for elem in bag[-5:]: print(elem) Don’t you understand? -5 means take 5 elements from the end of the list. If you want to perform a distance operation on the elements in a 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 you should do it like this: bag = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] for elem in bag[::2]: print(elem) 6. Use rangesbag = list(range(0,10,2)) print(bag) This is the stepping in the list. You can do cool reverse operations on a list with This is the end of this article about You may also be interested in:
|
<<: CSS style reset and clear (to make different browsers display the same effect)
>>: Analysis and summary of the impact of MySQL transactions on efficiency
I recently encountered a problem at work. There i...
This article example shares the specific code of ...
Application software generally has such business ...
Table of contents 1. Tool Introduction 2. Workflo...
Table of contents 1. v-on directive 1. Basic usag...
Table of contents Mysql master-slave synchronizat...
Introduction When talking about distribution, we ...
Using mock.js in Vue project Development tool sel...
1. Modify MySQL login settings: # vim /etc/my.cnf...
Table of contents 1. Example scenario 1.1. Set th...
This article shares with you two methods of setti...
Table of contents 1. Pull the image 2. Run the im...
Achieve results step 1. Initial index.html To bui...
mysql-5.7.19-winx64 installation-free version con...
Table of contents 1. Communication method between...