zoukankan      html  css  js  c++  java
  • 遍历列表,连续10个位置的类型不同

    有若干类型的数据组成的列表,按照分组对列表中的数据重新排列。要求连续10个元素的分组不同。

    item1 = [("a1",1) for _ in range(20)]
    item2 = [("a2",2) for _ in range(20)]
    item3 = [("a3",3) for _ in range(20)]
    item4 = [("a4",4) for _ in range(20)]
    item5 = [("a5",5) for _ in range(20)]
    item6 = [("a6",6) for _ in range(20)]
    item7 = [("a7",7) for _ in range(20)]
    item8 = [("a8",8) for _ in range(20)]
    item9 = [("a9",9) for _ in range(20)]
    item10 = [("a10",10) for _ in range(20)]
    item11 = [("a11",11) for _ in range(20)]
    all_item = item1 + item2 + item3 + item4 + item5 + item6 + item7 + item8 + item9 + item10 + item11
    
    
    from collections import MutableSequence
    class MyList(MutableSequence):
        """A container for manipulating lists of hosts
            保证列表中只有10个元素
        """
    
        def __init__(self, data=None):
            """Initialize the class"""
            super(MyList, self).__init__()
            if (data is not None):
                self._list = list(data)
            else:
                self._list = list()
    
        def __len__(self):
            """List length"""
            return len(self._list)
    
        def __getitem__(self, ii):
            """Get a list item"""
            return self._list[ii]
    
        def __delitem__(self, ii):
            """Delete an item"""
            del self._list[ii]
    
        def __setitem__(self, ii, val):
            # optional: self._acl_check(val)
            self._list[ii] = val
    
        def __str__(self):
            return str(self._list)
    
        def insert(self, ii, val):
            # optional: self._acl_check(val)
    
            self._list.insert(ii, val)
    
        def append(self, val):
            if len(self._list) >= 9:
                self._list.pop(0)
                self._list.insert(len(self._list), val)
            else:
                self.insert(len(self._list), val)
    
    i = 0
    tag = 0
    res = []
    mylist = MyList()
    while len(res)<=100:
        if i >=10 and tag >= 10:
            i = 0
            tag = 0
    
        temp = all_item[i]
        v = temp[0]
        c = temp[1]
        if c not in mylist:
            mylist.append(c)
            res.append(v)
            all_item.pop(i)
            tag += 1
        else:
            i += 1
    print(res)
    print(all_item)
    
  • 相关阅读:
    django 大体流程
    JavaScript概述
    前端css
    前端基础,加标签
    hashlib模块
    MySQL 了解知识点
    MySQL Navicat 使用
    mysql的基本查询语法及方法 多表查询
    MySQL 外键 一对一 一对多 多对多 复制
    It's likely that neither a Result Type nor a Result Map was specified
  • 原文地址:https://www.cnblogs.com/leimu/p/15138616.html
Copyright © 2011-2022 走看看