生成指定位数的坐标方法-2优化版本--建议使用的版本

核心内容:坐标生成的办法

rs = random.sample(range(0, 9), 4)
print(rs)

import random


def get_numbers(start:int,end:int,number:int):
    """
    函数作用:输入整数起始、结束的数值,以及一个生成多少个数字的number数字
    作用:在start与end的整数之间生成一个number个数字的列表
    :param start:
    :param end:
    :param number:
    :return:
    """
    # 定义一个结合存储结果
    numbers=[]
    while True:
        # 获取随机值[值的范围是上面的函数输入的参数范围]
        tempint=(random.randint(start,end))
        # 保留取一个随机的六位数字0.123456类似这种
        # tempfloat=round((random.random()),6)
        tempf1=random.sample(list("123456789"),6)
        tempf2="".join(tempf1)
        tempfloat =float(tempf2)*0.000001
        # tempfloat=0.119999
        # 如果出现了类似这样的数字0.12345  小数数字部分不足6位的需要补齐小数后6位数字
        # if len(str(tempfloat))<8:
        #     tempfloat=tempfloat+0.000111

        temp=float(tempint)+float(tempfloat)

        # 判断当前数字是否在列表中,如果不再就是添加到列表中
        if temp not in numbers:
            numbers.append(temp)
        # 判断数量是否满足要求
        if len(numbers)==number:
            break
    return numbers


if __name__ == '__main__':
    listrange1=get_numbers(200,900,5)
    print(listrange1)


2.优化版本–可以使用

import random


def get_numbers(start:int,end:int,number:int=6):
    """
    函数作用:输入整数起始、结束的数值,以及一个生成多少个数字的number数字
    作用:在start与end的整数之间生成一个number个数字的列表
    :param start:
    :param end:
    :param number:
    :return:
    """
    # 定义一个结合存储结果
    numbers=[]
    while True:
        # 获取随机值[值的范围是上面的函数输入的参数范围]
        tempint=(random.randint(start,end))
        # 保留取一个随机的六位数字0.123456类似这种
        tempfloat=round((random.random()),6)
        # tempfloat=0.119999
        # 如果出现了类似这样的数字0.12345  小数数字部分不足6位的需要补齐小数后6位数字
        if len(str(tempfloat))<8:
            tempfloat=tempfloat+0.000111

        temp=float(tempint)+float(tempfloat)

        # 判断当前数字是否在列表中,如果不再就是添加到列表中
        if temp not in numbers:
            numbers.append(temp)
        # 判断数量是否满足要求
        if len(numbers)==number:
            break
    return numbers



def callget_numbers():
    itemp=input("请输入横坐标的范围数值,例如在100,200范围内:100,200 :")
    xx=itemp.split(",")
    x1=int(xx[0])    # 横坐标范围的起始值
    x2=int(xx[1])     # 横坐标范围的最终值
    # lihengzuobiao=get_numbers(x1,x2,6)
    lihengzuobiao=get_numbers(x1,x2)
    print(lihengzuobiao)

    # 纵坐标范围
    itempzb=input("请输入纵坐标的范围数值,例如在100,200范围内:100,200 :")
    yy=itemp.split(",")
    y1=int(yy[0])    # 横坐标范围的起始值
    y2=int(yy[1])     # 横坐标范围的最终值
    # lihengzuobiao=get_numbers(x1,x2,6)
    lizongzuobiao=get_numbers(y1,y2)
    print(lizongzuobiao)


def callgetyouhua():
    tempinpu=input("输入横坐标,纵坐标范围,如100,110,12,19:")
    tempsplit=tempinpu.split(",")
    # print(tempsplit)
    x1,x2,y1,y2=tempsplit[0],tempsplit[1],tempsplit[2],tempsplit[3]
    hengzuobiao=get_numbers(int(x1),int(x2))
    zongzuobiao=get_numbers(int(y1),int(y2))
    print("横坐标生成值:",hengzuobiao)
    print("纵坐标生成值:",zongzuobiao)
if __name__ == '__main__':
    # callget_numbers()
    callgetyouhua()


3.优化版本–推荐使用

import random


def get_numbers(start:int,end:int,number:int):
    """
    函数作用:输入整数起始、结束的数值,以及一个生成多少个数字的number数字
    作用:在start与end的整数之间生成一个number个数字的列表
    :param start:
    :param end:
    :param number:
    :return:
    """
    # 定义一个结合存储结果
    numbers=[]
    while True:
        # 获取随机值[值的范围是上面的函数输入的参数范围]
        tempint=(random.randint(start,end))
        # 保留取一个随机的六位数字0.123456类似这种
        # tempfloat=round((random.random()),6)
        tempf1=random.sample(list("123456789"),6)
        tempf2="".join(tempf1)
        tempfloat =float(tempf2)*0.000001
        # tempfloat=0.119999
        # 如果出现了类似这样的数字0.12345  小数数字部分不足6位的需要补齐小数后6位数字
        # if len(str(tempfloat))<8:
        #     tempfloat=tempfloat+0.000111

        temp=float(tempint)+float(tempfloat)

        # 判断当前数字是否在列表中,如果不再就是添加到列表中
        if temp not in numbers:
            numbers.append(temp)
        # 判断数量是否满足要求
        if len(numbers)==number:
            break
    return numbers


def callgetyouhua():
    tempinpu=input("输入横坐标,纵坐标范围,生成多少组坐标数量:如100,110,12,19,20-->生成数量:")
    tempsplit=tempinpu.split(",")
    # print(tempsplit)
    x1,x2,y1,y2,count=tempsplit[0],tempsplit[1],tempsplit[2],tempsplit[3],tempsplit[4]
    hengzuobiao=get_numbers(int(x1),int(x2),int(count))
    zongzuobiao=get_numbers(int(y1),int(y2),int(count))
    print("横坐标生成值:",hengzuobiao)
    print("纵坐标生成值:",zongzuobiao)

if __name__ == '__main__':
    # listrange1=get_numbers(200,900,5)
    # print(listrange1)
    callgetyouhua()


4.完善这种版本信息—推荐使用!

import random

gloaballist=[]   # 大列表,装载坐标信息包括x,y
def get_numbers(start:int,end:int,number:int):
    """
    函数作用:输入整数起始、结束的数值,以及一个生成多少个数字的number数字
    作用:在start与end的整数之间生成一个number个数字的列表
    :param start:
    :param end:
    :param number:
    :return:
    """
    # 定义一个结合存储结果
    numbers=[]
    while True:
        # 获取随机值[值的范围是上面的函数输入的参数范围]
        tempint=(random.randint(start,end))
        # 保留取一个随机的六位数字0.123456类似这种
        # tempfloat=round((random.random()),6)
        # 随机抽样“random.sample”,本例中抽取的是6个字符,然后      # 在拼接在一块
        tempf1=random.sample(list("123456789"),6)
        tempf2="".join(tempf1)
        tempfloat =float(tempf2)*0.000001
        # tempfloat=0.119999
        # 如果出现了类似这样的数字0.12345  小数数字部分不足6位的需要补齐小数后6位数字
        # if len(str(tempfloat))<8:
        #     tempfloat=tempfloat+0.000111

        temp=float(tempint)+float(tempfloat)

        # 判断当前数字是否在列表中,如果不再就是添加到列表中
        if temp not in numbers:
            numbers.append(temp)
        # 判断数量是否满足要求
        if len(numbers)==number:
            break
    return numbers


def callgetyouhua():
    tempinpu=input("输入横坐标,纵坐标范围,生成多少组坐标数量:如100,110,12,19,20-->生成数量:")
    tempsplit=tempinpu.split(",")
    # print(tempsplit)
    x1,x2,y1,y2,count=tempsplit[0],tempsplit[1],tempsplit[2],tempsplit[3],tempsplit[4]
    hengzuobiao=get_numbers(int(x1),int(x2),int(count))
    zongzuobiao=get_numbers(int(y1),int(y2),int(count))
    for i in range(int(count)):

        item=[hengzuobiao[i],zongzuobiao[i]]
        gloaballist.append(item)
    print(gloaballist)

    # print("横坐标生成值:",hengzuobiao)
    # print("纵坐标生成值:",zongzuobiao)

if __name__ == '__main__':
    # listrange1=get_numbers(200,900,5)
    # print(listrange1)
    callgetyouhua()


猜你喜欢

转载自blog.csdn.net/wtt234/article/details/113943355
今日推荐