This is the 17th day of my participation in the First Challenge 2022
Today I would like to share some tips on using Python. I have been using Python for almost 2 years from AI. I have also encountered problems and tried some solutions. Feel the following introduction of solutions are more practical, but also hope to help you.
The difference between two lists
We now have two lists, Models and object_Detections
models = ['fastRCNN'.'YOLO'.'mask-rcnn'.'deeplab'.'FCN']
object_detections = ['fastRCNN'.'YOLO'.'mask-rcnn']
Copy the code
We want to find some models, which is models and object_Detections, where this element is present in models but not object_Detections
models_set = set(models)
object_detections_set = set(object_detections)
segmentation = list(models_set.symmetric_difference(object_detections_set))
print(segmentation)
Copy the code
Calculate the amount of memory used by Python objects
Whenever any data structure (such as a list, dictionary, or any object) is used to store values or records, check the amount of memory the data structure takes up. A good practice to see how much memory is being used by a data structure is to use sys. getSizeof to return the sizeof the object, in bytes.
import sys
object_detections = ['fastRCNN'.'YOLO'.'mask-rcnn']
print("size of list = ",sys.getsizeof(object_detections))
#Memory: 4076 kilobyte(s)
Copy the code
List to heavy
In many cases, duplicate elements need to be removed from the list. We usually use a set that is automatically deduplicated. No duplicate elements are allowed in a set.
listNumbers = [1.2.2.5.5.5.6.7.8]
print("Original= ", listNumbers)
listNumbers = list(set(listNumbers))
print("After removing duplicate= ", listNumbers)
Copy the code
Original= [1.2.2.5.5.5.6.7.8]
After removing duplicate= [1.2.5.6.7.8]
Copy the code
How do I compare two lists effectively
Usually we need to compare two lists to see if they are the same,
from collections import Counter
one = [1.5.3.7.9]
two = [9.1.3.5.7]
print("Compare if two lists are the same.", Counter(one) == Counter(two))
Copy the code
- Object that Hashable can use
collections.Counter
- You can also use
sorted()
Sort them and see if they’re the same
The check list contains only one element
To check whether a list contains only one element, use a little trick: count the number of occurrences of an element. If count is the same length as the list, all elements in the list are consistent.
a = [3.3.3.3]
print("All element are duplicate in listOne", a.count(a[0= =])len(a))
b = [3.3.3.2]
print("All element are duplicate in listTwo", b.count(b[0= =])len(b))
Copy the code
Computing function time
In general, we need to know the elapsed time of our function to measure its performance.
import time startTime = time.time()
# Your code
endTime = time.time()
totalTime = endTime - startTime
print("Total time required to execute code is= ", totalTime)
Copy the code
Deconstruction dictionary
If you are familiar with the new ES6 features of JS, you should be familiar with the following syntax. Deconstruction is sort of taking the elements out of the container one by one.
networkOne = {1: 'AlexNet'.2: "ResNet".3:"DenseNet"}
networkTwo= {2: 'ViT'.4: "Transformer"}
networkThird = {**networkOne, **networkTwo}
print(networkThird)
Copy the code
{1: 'AlexNet'.2: 'ViT'.3: 'DenseNet'.4: 'Transformer'}
Copy the code