python: list

 

   #去重
    A = ['geovindu','刘杰','江山','河水','刘杰','geovindu','张三','李五']
    B = []
    for i in A:
        if i not in B:
            B.append(i)
    print(B)
    C = set(A)
    print(list(C))

    # 移动位置 从index=3 要移的index 指定的位置index 1
    index=1  #新位置
    oldindex=3 #旧位置
    show = ['2A', '3B', '1C', '4E', '5A']
    temp = show[oldindex]
    for k in range(oldindex, index, -1): #移到index 1
        show[k] = show[k - 1]
    show[index] = temp
    print(show)
    print('*********')
    #移到index 0
    index=0 #新位置
    oldindex=3 #旧位置
    show = ['2A', '3B', '1C', '4E', '5A']
    temp = show[oldindex]
    for k in range(oldindex,index, -1):
        show[k] = show[k - 1]
    show[index] = temp
    print(show)
    print('*********')
    # 移除
    wait = [3001, 4001, 5001, 2001, 2002, 3002, 2003, 4002, 2004, 2005]
    delindex = 2 #位置
    for k in range(delindex, len(wait) - 1):
        wait[k] = wait[k + 1]
    wait[len(wait) - 1] = 0
    print(wait)
    print('*********')
    # 添加一个  添加位置 index 4
    index=4 #新位置
    wait = [3001, 4001, 5001, 2001, 2002, 3002, 2003, 4002, 2004, 2005]
    wait = wait + [0]
    for k in range(len(wait) - 1, index, -1):
        wait[k] = wait[k - 1]
    wait[index] = 9001
    print(wait)
    print('*********')
    # 添加一个 添加位置index 3
    eca = ["Tony", "Paul", "Kelly", "Wendy", "Jack"]
    eca = eca + ['']
    index=3 #新位置
    nn = len(eca);
    for k in range(nn - 1, index, -1):
        eca[k] = eca[k - 1]
    eca[index] = "viky"
    print(eca)

 

 

# encoding: utf-8
# 版权所有 2024 涂聚文有限公司
# 许可信息查看:
# 描述:
# Author    : geovindu,Geovin Du 涂聚文.
# IDE       : vs code python 3.11
# Datetime  : 2024/6/15 18:56
# User      : geovindu
# Product   : Visual Studio Code
# Project   : pyBaiduAi
# File      : DuList.py
# explain   : 学习

import sys
import os
import io


class DuList(object):
    """
    操作列表
    """
    def __init__(self) -> None:
        pass

    @staticmethod
    def MomveItem(oldindex:int,newindex:int,show:list) -> list:
        """
        操作列表指定的某个元素的索引位置移到指定的新位置
        :param oldindex: 旧位置索引值
        :param newindex: 新位置索引值
        :param show: 要移位位置的列表
        :return:返回移位置的列表
        """

        #index=1  #新位置
        #oldindex=3 #旧位置
        #show = ['2A', '3B', '1C', '4E', '5A']
        temp = show[oldindex]  #获取指定索引值原元素
        for k in range(oldindex, newindex, -1): #移到index 1
            show[k] = show[k - 1]
        show[newindex] = temp
        return show
    

    @staticmethod
    def DelItem(delindex:int,show:list) -> list:
        """
        操作删除列表指定的索引值的某个元素
        :param oldindex: 位置索引值  
        :param show: 要移位位置的列表
        :return:返回删除后的列表
        """
        # 移除
        #show = [3001, 4001, 5001, 2001, 2002, 3002, 2003, 4002, 2004, 2005]
        #delindex = 2 #位置
        for k in range(delindex, len(show) - 1):
            show[k] = show[k + 1]
        show[len(show) - 1] = 0
        show.pop(len(show)-1)
        return show

    @staticmethod
    def AddItem(index:int,value,show:list) -> list:
        """
        列表添加指定索引位置的元素
        :param index: 添加位置索引值  
        :param value: 添加值  
        :param show: 列表
        :return:返回添加后的列表
        """
        # 添加一个  添加位置 index 4
        # index=4 #新位置
        #show = [3001, 4001, 5001, 2001, 2002, 3002, 2003, 4002, 2004, 2005]
        if(type(value)==int):
            show = show + [0]
        elif(type(value)==str):
            #show = show + [''] 
            show = show + [None]
        else:
            show = show + [None]

        for k in range(len(show) - 1, index, -1):
            show[k] = show[k - 1]
        show[index] = value
        return show
    

  

 

调用:

# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    #print_hi('PyCharm,python language')

    show = ['2A', '3B', '1C', '4E', '5A']
    DuList.MomveItem(3,1,show)
    print(show)
    wait = [3001, 4001, 5001, 2001, 2002, 3002, 2003, 4002, 2004, 2005]
    DuList.DelItem(2,wait)
    print(wait)
    wait = [3001, 4001, 5001, 2001, 2002, 3002, 2003, 4002, 2004, 2005]
    newwait=DuList.AddItem(2,8008,wait)
    print(newwait)

  

 

 

vs 2022 要考虑文件编码,微软的IDE 一般是GB2312.所以还要人工设置一下。

 

posted @ 2024-07-06 19:16  ®Geovin Du Dream Park™  阅读(3)  评论(0编辑  收藏  举报