Python: to deal the case when the returned value is a large structure [closed]

StackOverflow https://stackoverflow.com/questions/23682912

  •  23-07-2023
  •  | 
  •  

문제

if the returned value is a very large structure, like a list with millions of elements, how to deal such cases?

like

x=set[()]
def f(x):
     ....
     ....
     # now x has millions of elements
     return x
도움이 되었습니까?

해결책

  1. you meant

    x = set()

  2. the function acting on x does not need to return x, it can modify the passed value.

  3. there is no duplication of data when you return a list or any other object... just because the value passed is not copied.

  4. you are affected by the premature optimization syndrome since you are trying to solve an optimization problem before testing that the problem actually holds.

Example of function modifying a set:

>>> def f(x):
...   x.add(42)
... 
>>> x = set([13])
>>> x
set([13])
>>> f(x)
>>> x
set([42, 13])
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top