当前位置:网站首页>Geek University cloud native training camp

Geek University cloud native training camp

2022-06-24 21:03:00 m0_ sixty-seven million one hundred and sixty-eight thousand se

### download: Geek University - Cloud native training camp

About python Tips

The difference between the 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 code

We want to find some models , That is to say models and object_detections That is, this element exists in models There is no such thing as object_detections

models_set = set(models)

object_detections_set = set(object_detections)

segmentation = list(models_set.symmetric_difference(object_detections_set))

print(segmentation)

Copy code

Calculation python The amount of memory occupied by the object

About any data structure ( As listing 、 Dictionary or any object ) To store values or records , Check the size of the memory occupied by the data structure . It is a good practice to view how much memory the data structure uses sys.getsizeof Returns the size of the object , The unit is byte .

import sys

object_detections = ['fastRCNN', 'YOLO', 'mask-rcnn']

print("size of list = ",sys.getsizeof(object_detections))

#Memory: 4076 kilobyte(s)

Copy code

List to heavy

In most cases , Requirements remove repeating elements from the list . Usually we use them set This automatic de duplication confluence , stay set You are not allowed to have repeated elements in .

listNumbers = [1,2,2,5,5,5,6,7,8]

print("Original= ", listNumbers)

listNumbers = list(set(listNumbers))

print("After removing duplicate= ", listNumbers)

Copy code

Original=  [1, 2, 2, 5, 5, 5, 6, 7, 8]

After removing duplicate=  [1, 2, 5, 6, 7, 8]

Copy code

How to effectively compare two lists

Usually we need to compare whether the two lists are the same ,

from collections import Counter

one = [1, 5, 3, 7, 9]

two = [9, 1, 3, 5, 7]

print(" Compare whether the two lists are the same ", Counter(one) == Counter(two))

Copy code

The object is hashable Be able to use collections.Counter 

Can also use sorted() Stop sorting and see if it's the same

The detection list contains only one element

Check whether a list can contain only one element , Here's a little trick , That is to calculate the number of elements presented , Application count Can it be the same length as the list , If the same , To clarify that all elements in the list are divergent .

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 code

It takes time to calculate the function

Usually we need to understand how long our functions take , To weigh the performance of a function .

import time startTime = time.time() 

# Your code

endTime = time.time() 

totalTime = endTime - startTime 

print("Total time required to execute code is= ", totalTime)

原网站

版权声明
本文为[m0_ sixty-seven million one hundred and sixty-eight thousand se]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211322311508.html