简单类练习

生成随机数

生成坐标系点

#-*- coding: UTF-8 -*-
import random

class Randomint:
    """
    :param 类主要产生随机
    """
    start = 1
    end = 100

    def __init__(self,start,end):
        self.start = start
        self.end = end

    def objnumlist(self,num:int):
        """
        将随机数生成list并返回
        """
        objnumlist = (random.randint(self.start,self.end) for _ in range(num))

        return objnumlist

    @classmethod
    def numlist(cls,num:int):
        """
        将随机数生成list并返回
        """
        numlist = (random.randint(cls.start,cls.end) for _ in range(num))

        return numlist

class Coordinate:
    """
    类:创建坐标系
    """
    def point(self,iterable:list):
        """
        :param iterable 传入列表
        函数会根据列表中的元素组织坐标系
        """
        locate = []

        for i,v in enumerate(iterable):
            locate.append(v)

            if i%2 == 1:
                yield tuple(locate)
                locate.clear()

    @classmethod
    def outpoint(cls,iterable):
        """
        :param iterable是列表
        """
        yield from cls().point(iterable)


def mainfunc():
    """
    实现功能模块需求
    """
    #实例化调用生成整数的方法,最终形成list
    #num = list(Randomint.numlist(20))
    num = list(Randomint(1,10).objnumlist(20))

    #实例化调用生成坐标的方法,最终形成坐标生成器
    # points = [x for x in Coordinate().outpoint(num)]
    points = list(Coordinate.outpoint(num))

    #打印生成的列表
    print(num)

    #打印生成的坐标系
    print(points)

mainfunc()
运行结果:
[9, 7, 7, 7, 7, 10, 4, 4, 7, 4, 3, 4, 1, 2, 9, 2, 8, 4, 1, 7]
[(9, 7), (7, 7), (7, 10), (4, 4), (7, 4), (3, 4), (1, 2), (9, 2), (8, 4), (1, 7)]

猜你喜欢

转载自blog.csdn.net/xuexiaoyaani/article/details/80201055