<jupyter_start><jupyter_text>例 1：使用NumPy标量类型<jupyter_code>dt = np.dtype(np.int32)
print(dt)<jupyter_output>int32<jupyter_text>例 2：使用Python内置类型> int8, int16, int32, int64 四种数据类型可以使用字符串 'i1', 'i2','i4','i8' 代替<jupyter_code>dt = np.dtype('i4')
print(dt)<jupyter_output>int32<jupyter_text>例 3：标注字节顺序<jupyter_code>dt = np.dtype('<i4')
print(dt)
print(dt.str)<jupyter_output>int32
<i4<jupyter_text>例 4：创建结构化数据类型<jupyter_code>dt = np.dtype([('age', np.int8)]) 
print(dt)<jupyter_output>[('age', 'i1')]<jupyter_text>例 5：将数据类型应用于 ndarray 对象<jupyter_code>dt = np.dtype([('age', np.int8)]) 
a = np.array([(10, ), (20, ), (30, )], dtype = dt) 
print(a)<jupyter_output>[(10,) (20,) (30,)]<jupyter_text>例 6：类型字段名可以用于存取实际的 age 列<jupyter_code>dt = np.dtype([('age', np.int8)]) 
a = np.array([(10, ), (20, ), (30, )], dtype = dt) 
print(a['age'])<jupyter_output>[10 20 30]<jupyter_text>例 7: 定义一个结构化数据类型 student，包含字符串字段 name，整数字段 age，及浮点字段 marks，并将这个 dtype 应用到 ndarray 对象<jupyter_code>import numpy as np
student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')])
print(student)<jupyter_output>[('name', 'S20'), ('age', 'i1'), ('marks', '<f4')]<jupyter_text>例 8：结构化数据类型输出<jupyter_code>student = np.dtype([('name', 'S20'), ('age', 'i1'), ('marks', 'f4')]) 
a = np.array([('abc', 21, 50),('xyz', 18, 75)], dtype = student) 
print(a)
print(a['marks'])<jupyter_output>[(b'abc', 21, 50.) (b'xyz', 18, 75.)]
[50. 75.]
{'fraction_non_alphanumeric': 0.2466143977191732, 'fraction_numerical': 0.06414825374198146, 'mean_word_length': 8.36, 'pattern_counts': {'":': 0, '<': 28, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '160138', 'license': 'MIT', 'n_tokens_mistral': 787, 'n_tokens_neox': 746, 'n_words': 103, 'path': 'code/C2/C2.ipynb', 'repo_name': 'ProgrammingTeaching/Python-', 'size': '1403'}