如何确定Python中集合变量的大小?

How to determine the size of a set variable in Python?

我有如下代码,其中 table 数据是在 set(cur) 中捕获的。有没有办法在linux/unix中找到这个变量占用的space? (内存或缓冲区space)

cur.execute("select A , B , C from DeptTable")
dept_entries = set(cur)

cur.execute("select A , B , C from EmployeeTable where EmplName in ('A','B')") 
for empl in cur:
  if empl in dept_entries:
    print(empl, 'Yes')
  else:
    print(empl, 'No')

我建议使用 sys.getsizeof:

>>> import sys
>>> sys.getsizeof(dept_entries)
12345
>>> sys.getsizeof(set([1,2,3]))
224
>>> sys.getsizeof(set([1,2,3,4,5]))
736