一学就废|Python基础碎片,range()函数

        range()函数返回给定范围内的数字序列。它最常见的用途是使用 Python 循环在数字序列上迭代序列。例如:

for i in range(5):
    print(i, end=" ")
print()

0 1 2 3 4 

range()函数的语法 

语法:range(start, stop, step)
参数:
start:[可选] 序列的起始值

stop:序列结束值之后的下一个值

step步长:[可选] 整数值,表示序列中任意两个数字之间的差异

返回:返回一个表示数字序列的对象

range 函数有什么用

        range()允许用户在给定范围内生成一系列数字。根据用户传递给函数的参数数量,用户可以决定该系列数字将在哪里开始和结束,以及一个数字和下一个数字之间的差异有多大。

        Python range()函数可以通过 3 种方式进行初始化。

・range(停止)接受一个参数。

・range(开始,停止)接受两个参数。

・range(开始,停止,步长)接受三个参数。

        以下示例range (stop) 接受一个参数:

# printing first 6
# whole number
for i in range(6):
    print(i, end=" ")
print()

0 1 2 3 4 5 

        以下示例range (start, stop)接受两个参数:

# printing a natural
# number from 5 to 20
for i in range(5, 20):
    print(i, end=" ")

5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

        以下示例ange (start, stop, step)接受三个参数:

for i in range(0, 10, 2):
    print(i, end=" ")
print()

0 2 4 6 8

使用正数增加步长

# incremented by 4
for i in range(0, 30, 4):
    print(i, end=" ")
print()

0 4 8 12 16 20 24 28

使用负数增加步长

# incremented by -2
for i in range(25, 2, -2):
    print(i, end=" ")
print()

25 23 21 19 17 15 13 11 9 7 5 3 

        range()函数不支持浮点数。即用户不能在其任何参数中使用浮点数或非整数。用户只能使用整数。

# using a float number
for i in range(3.3):
    print(i)

for i in range(3.3):
TypeError: 'float' object cannot be interpreted as an integer

连接两个 range()函数

        两个 range()函数的结果可以使用 itertools 模块的 chain()方法连接起来。chain()方法用于一个接一个地打印其参数中提到的可迭代目标中的所有值。

from itertools import chain

# Using chain method
print("Concatenating the result")
res = chain(range(5), range(10, 20, 2))

for i in res:
    print(i, end=" ")


Concatenating the result
0 1 2 3 4 10 12 14 16 18 

使用索引值访问 range()

        range () 函数返回一个数字序列作为其对象,可以通过索引值访问该对象。其对象支持正索引和负索引。

ele = range(10)[0]
print("First element:", ele)

ele = range(10)[-1]
print("\nLast element:", ele)

ele = range(10)[4]
print("\nFifth element:", ele)

First element: 0
Last element: 9
Fifth element: 4

列表遍历中使用range()

fruits = ["apple", "banana", "cherry", "date"]

for i in range(len(fruits)):
    print(fruits[i])


apple
banana
cherry
date

Python中,你可以使用`numpy`库来创建数字数组,然后利用`scipy`库中的`dispersement`模块或者自定义函数来实现随机切割数组成指定数量的碎片。以下是个简单的示例: ```python import numpy as np from scipy.optimize import linear_sum_assignment # 创建个二维数字矩阵 matrix = np.random.rand(5, 5) # 可以根据需要调整大小 # 指定要切割成的碎片数量 num_pieces = 3 def cut_matrix(matrix, num_pieces): # 首先,计算每个碎片的理想形状,假设我们希望它们大致相等 ideal_shape = matrix.shape[0] // num_pieces, matrix.shape[1] // num_pieces # 将矩阵转换为列表便于操作 matrix_list = list(matrix.flatten()) # 使用线性规划找到最佳分配方案 row_ind, col_ind = linear_sum_assignment(np.abs(np.subtract.outer(range(num_pieces), range(len(matrix_list))))) # 切割并重构矩阵 pieces = [matrix_list[i:j] for i, j in zip(row_ind, col_ind + row_ind)] # 恢复每块到原始形状(如果有余数可能会有填充) pieces = [np.reshape(piece, ideal_shape if piece.size == ideal_shape[0]*ideal_shape[1] else (piece.size//ideal_shape[1], *ideal_shape)) for piece in pieces] return pieces pieces = cut_matrix(matrix, num_pieces) ``` 这个例子中,`linear_sum_assignment`函数用于找到将碎片数量与矩阵元素关联的最佳方式。注意,这种方法假定了碎片尽可能接近理想形状,并且不会完全平均分布,因为实际尺寸可能会导致碎片比其他小。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码路刺客

您的鼓励是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值