以下代码是基于python3.5.0写的
import numpy
# 读取文件内容
world_alcohol = numpy.genfromtxt(
"world_alcohol.txt", delimiter=
",",dtype=
"float",skip_header=
1)
print(
type(world_alcohol))
print(world_alcohol)
vector = numpy.array([
1, 2, 3, 4])
#构造数组
print(vector.shape)
#打印数组形状
matrix = numpy.array([[
5, 10, 15]
, [
20, 25, 30]])
#构造矩阵
print(matrix.shape)
#打印矩阵形状
numbers = numpy.array([
1, 2, 3, 4])
# 查看数组中元素类型
print(numbers.dtype)
uruguay_other_1986 = world_alcohol[
1,4]
# 读取第1行第4个元素
third_country = world_alcohol[
2,2]
# 读取第2行第2个元素
vector = numpy.array([
5, 10, 15, 20])
print(vector[
0:
3])
# 查找数组前三个值
matrix = numpy.array([
[
5, 10, 15]
,
[
20, 25, 30]
,
[
35, 40, 45]])
print(matrix[:
,1])
# :代表所有的意思,查找所有行第一列的值[10 25 40]
matrix = numpy.array([
[
5, 10, 15]
,
[
20, 25, 30]
,
[
35, 40, 45]])
print(matrix[:
,0:
2])
# 查找所有行,前两列的值[[ 5 10],[20 25],[35 40]]
matrix = numpy.array([
[
5, 10, 15]
,
[
20, 25, 30]
,
[
35, 40, 45]])
print(matrix[
1:
3,0:
2])
# 查找2到3行,前两列的值[[20 25],[35 40]
转载请注明原文地址: https://ju.6miu.com/read-15764.html