5.3. PikaStdData 数据结构

PikaStdData 数据结构库提供了 List (列表),Dict(字典)数据结构。

5.3.1. 安装

1.

  1. PikaStdLib==v1.11.0

1.

5.3.2. 导入

在 main.py 中加入

  1. #main.pyimport PikaStdData

5.3.3. class List():

List 类提供了 List 列表功能,由 List 类创建对象,即可创建一个列表。 如:

  1. import PikaStdDatalist = PikaStdData.List()

5.3.3.1. List 类的方法

  1. # add an arg after the end of list def append(self, arg: any): pass # get an arg by the index def __getitem__(self, i: int) -> any: pass # set an arg by the index def __setitem__(self, i: int, arg: any): pass # get the length of list def len(self) -> int: pass

__setitem__() 方法的索引不能够超出 List 的长度,如果要添加列表的成员,需要使用 append()方法。

5.3.3.2. 使用 ‘[]‘ 中括号索引列表

list[1] = a等效于 list.__setitem__(1, a)a = list[1]等效于a = list.__getitem__(1)

5.3.3.3. 使用 for 循环遍历 List

List 对象支持 for 循环遍历 例:

  1. import PikaStdDatalist = PikaStdData.List()list.append(1)list.append('eee')list.append(23.44)for item in list: print(item)

5.3.4. class Dict():

Dict 类提供了 Dict 字典功能,由 Dict 类创建对象,即可创建一个字典。 如:

  1. import PikaStdDatadict = PikaStdData.Dict()

5.3.4.1. Dict 类的方法

  1. # get an arg by the key def __getitem__(self, key: str) -> any: pass # set an arg by the key def __setitem__(self, key: str, arg: any): pass # remove an arg by the key def remove(self, key: str): pass

5.3.4.2. 使用 ‘[]‘ 中括号索引字典

dict['x'] = a等效于 dict.__setitem__('x', a)a = dict['x']等效于a = dict.__getitem__('x')

5.3.4.3. 使用 for 循环遍历 Dict

Dict 对象支持 for 循环遍历 例:

  1. import PikaStdDatadict = PikaStdData.Dict()dict['a'] = 1dict['b'] = 'eee'dict['c'] = 23.44for item in dict: print(item)

5.3.5. class ByteArray(List)

[注意]: PikaStdData 的版本至少需要 v1.5.3 ByteArray 类提供了 ByteArray 字节数组功能,由 ByteArray 类创建对象,即可创建一个字节数组。 如:

  1. import PikaStdDatabytes = PikaStdData.ByteArray(b'test')

用例:

  1. >>> bytes = PikaStdData.ByteArray(b'test')>>> for byte in bytes:... print(byte)... 116101115116>>> bytes.append(0xff)>>> bytes.append(0x0f)>>> print(bytes[4])255>>> print(bytes[5])15