numpy数组

创建数组

import numpy as np

# A one-lane road, represented by an array
# Here is a 1x7 road
road = np.array(['r', 'r', 'r', 'r', 'r', 's', 'r'])

读取数组中的值

# Access the first index and read its value
value = road[0]
print('\n')
print('Value at index [0] = ' +str(value))

# Read the last item in the array
# A negative index moves from the end of the list backwards!
value_end = road[-1]
print('\n')
print('Value at index [-1] = ' +str(value_end))

# Compare first and last values
equal = (value == value_end)
print('\n')
print('Are the first and last values equal? ' +str(equal))

数组迭代

import numpy as np

# A 4x5 robot world of characters 'o' and 'b'
world = np.array([ ['o', 'b', 'o', 'o', 'b'],
                   ['o', 'o', 'b', 'o', 'o'],
                   ['b', 'o', 'o', 'b', 'o'],
                   ['b', 'o', 'o', 'o', 'o'] ])

# Sensor measurement
measurement = ['b', 'o']

# This function takes in the world and the sensor measurement.
# Complete this function so that it returns the indices of the 
# likely robot locations, based on matching the measurement 
# with the color patterns in the world

def find_match(world, measurement):
    
    # Empty possible_locations list
    possible_locations = []
    
    # Store the number of columns and rows in the 2D array
    col = world.shape[1]
    row = world.shape[0]
    
    # Iterate through the entire array
    for i in range(0, row):
        for j in range (0, col):
            # Check that we are within the bounds of the world,
            # since we have to check two values, this means we're at
            # a row index < the number of columns (5) - 1
            # In other words j < 4
            if j < col - 1:
                # Check if a match is found by comparing array contents
                # and checking for equality at world[i][j] and 
                # one row to the right at world[i][j+1]
                
                # Values under and in front of the robot 
                under = world[i][j]
                in_front = world[i][j+1]
                
                if((measurement[0] == under) and (measurement[1] == in_front)):
                    # A match is found!
                    # Append the index that the robot is on
                    possible_locations.append([i,j])
    
    # Return the completed list
    return possible_locations
   

# This line runs the function and stores the output - do not delete 
locations = find_match(world, measurement)

按列索引

#  how would you print COLUMN 0? In numpy, this is easy

import numpy as np

np_grid = np.array([
    [0, 1, 5],
    [1, 2, 6],
    [2, 3, 7],
    [3, 4, 8]
])

# The ':' usually means "*all values*
print(np_grid[:,0])

改变数组形状

# What if you wanted to change the shape of the array?

# For example, we can turn the 2D grid from above into a 1D array
# Here, the -1 means automatically fit all values into this 1D shape
np_1D = np.reshape(np_grid, (1, -1))

print(np_1D)

创建空的二维数组

# We can also create a 2D array of zeros or ones
# which is useful for car world creation and analysis

# Create a 5x4 array
zero_grid = np.zeros((5, 4))

print(zero_grid)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值