Finding size of Variable
Dave Angel
davea at davea.name
Wed Feb 5 08:43:34 EST 2014
Ayushi Dalmia <ayushidalmia2604 at gmail.com> Wrote in message:
> On Wednesday, February 5, 2014 12:59:46 AM UTC+5:30, Tim Chase wrote:
>> On 2014-02-04 14:21, Dave Angel wrote:
>>
>> > To get the "total" size of a list of strings, try (untested):
>>
>> >
>>
>> > a = sys.getsizeof (mylist )
>>
>> > for item in mylist:
>>
>> > a += sys.getsizeof (item)
>>
>>
>>
>> I always find this sort of accumulation weird (well, at least in
>>
>> Python; it's the *only* way in many other languages) and would write
>>
>> it as
>>
>>
>>
>> a = getsizeof(mylist) + sum(getsizeof(item) for item in mylist)
>>
>>
>>
>> -tkc
>
> This also doesn't gives the true size. I did the following:
>
> import sys
> data=[]
> f=open('stopWords.txt','r')
>
> for line in f:
> line=line.split()
> data.extend(line)
>
> print sys.getsizeof(data)
>
Did you actually READ either of my posts or Tim's? For a
container, you can't just use getsizeof on the container.
a = sys.getsizeof (data)
for item in mylist:
a += sys.getsizeof (data)
print a
--
DaveA
More information about the Python-list
mailing list