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

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

  •  23-07-2023
  •  | 
  •  

Domanda

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
È stato utile?

Soluzione

  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])
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top