+ 4
[Python]how to get the item in an mixed array?
here is my code inPython3 from array import array scores=array('d') scores.append(97) scores.append(98) print(scores[0]) the result will be 97.0 and for scores[1]will be 98.0 then how can I print the str 'd'
6 Respuestas
+ 5
'd' isn't an item in the array, it is the typecode for the array ('d': double). `scores.typecode` would get it but if you want 'd' to be an item you might as well use a normal list as it can store multiple types ['d', 97, 98]
+ 4
array.itemsize is the number of bytes reserved for each array element (this depends on the type)
You can use array.buffer_info() to retrieve the memory address and the total count of elements.
See more in standard docs:
https://docs.python.org/3/library/array.html
In Python we typically use lists instead of array, they are much easier to handle. You may only want to use array if you have to deal with low level programming and C interfaces.
+ 2
thx a lot. and when I use scores.itemsize get 8, but scores.append some other items the itemsize is still 8.
is the itemsize like len(list), to count the items in an array?
+ 2
one of the best questions in this forum since long.
... just btw.
0
I would learn phyton
0
Thx to you all, and the example is from the video by Christopher Harrison, Microsoft, the whole video can be found in YouTube, Python for beginners.