【问题标题】:How to determine the size of a set variable in Python?如何确定 Python 中集合变量的大小?
【发布时间】:2019-12-05 16:01:51
【问题描述】:

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

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')

【问题讨论】:

    标签: python python-3.x linux set size


    【解决方案1】:

    我会推荐使用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
    

    【讨论】:

    • 非常感谢您的回复。我可以知道这个大小是 KB 还是 MB ?
    • 它以字节为单位,要将其转换为 KB 或 MB 或其他任何值,您可以使用答案之一 here
    猜你喜欢
    • 1970-01-01
    • 2013-03-27
    • 1970-01-01
    • 2010-10-06
    • 2015-03-11
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 2014-01-25
    相关资源
    最近更新 更多