I am using python 3.5. I am trying to define an array with set of characters. I tried the below.
import array
my_char_array = array.array('c', ['g','e','e','k'])
When I run, I get the following error:
ValueError: bad typecode (must be b, B, u, h, H, i, I, l, L, q, Q, f or d)
Isn't 'c' a valid typecode for characters? Please enlighten me.
According to the docs, 'c' is not anymore available in python 3.5. It was available in python 2.6 as documented here. You may use signed or unsigned types:
'b': signed char'B': unsigned charUsing 'b':
m = array.array('b', [ord('g'), ord('e'), ord('e'), ord('k')])
You could do help(array.array) in your Python Shell.
Arrays represent basic values and behave very much like lists, except the type of objects stored in them is constrained. The type is specified at object creation time by using a type code, which is a single character. The following type codes are defined:
Type code C Type Minimum size in bytes 'b' signed integer 1 'B' unsigned integer 1 'u' Unicode character 2 (see note) 'h' signed integer 2 'H' unsigned integer 2 'i' signed integer 2 'I' unsigned integer 2 'l' signed integer 4 'L' unsigned integer 4 'q' signed integer 8 (see note) 'Q' unsigned integer 8 (see note) 'f' floating point 4 'd' floating point 8
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With