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

7th Oct 2019, 12:56 AM
lia
lia - avatar
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]
7th Oct 2019, 1:32 AM
jtrh
jtrh - avatar
+ 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.
7th Oct 2019, 3:32 AM
Tibor Santa
Tibor Santa - avatar
+ 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?
7th Oct 2019, 2:37 AM
lia
lia - avatar
+ 2
one of the best questions in this forum since long. ... just btw.
7th Oct 2019, 6:30 AM
Oma Falk
Oma Falk - avatar
0
I would learn phyton
7th Oct 2019, 2:46 PM
Ngo Bassomo Marie Megane
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.
8th Oct 2019, 3:56 AM
lia
lia - avatar