美文网首页
python基础知识点(数据结构相关)

python基础知识点(数据结构相关)

作者: endian | 来源:发表于2018-01-18 18:10 被阅读0次

    数据结构

    python中有四种内置的数据结构列表元组字典集合

    • 列表

      • 列表是一种用于保存一系列有序项目的集合,也就是说,你可以利用列表保存一串项目的序列。应该用方括号括起来

      • code如下:

         shoplist=['apple','mongo','carrot','banana'];
         print('I have',len(shoplist),'items to purchase.');
         print('These items are:',end=' ');
         for item in shoplist:
             print(item,end=' ');
         print('\nI also have to buy rice.');
         shoplist.append('rice');
         print('My shopping list is now',shoplist);
         print('I will sort my list now');
         shoplist.sort();
         print('Sorted shopping list is',shoplist);
         print('The first item I will buy is',shoplist[0]);
         oldItem = shoplist[0];
         del shoplist[0];
         print('I bought the',oldItem);
         print('My shopping list is now',shoplist);
        
    • 元组

      • 元组用于将多个对象保存到一起;元组通常用于保证某一语句或某一用户定义的函数可以安全地采用一组数值,意即元组内的数值不会改变

      • 代码如下:

          zoo=('python','elephant','penguin');
          print('Number of animals in the zoo is',len(zoo));
          
          new_zoo = 'monkey','camel',zoo;
          print('Number of cages in the new zoo is',len(new_zoo));
          print('All animals in new zoo are',new_zoo);
          print('Animals brought from old zoo are',new_zoo[2]);
          print('Last animal brought from old zoo is',new_zoo[2][2]);
          print('Number of animals in the new zoo is',len(new_zoo)-1+len(new_zoo[2]));    
        
    • 字典

      • 可以通过使用符号构成d={key1:value1,key2:value2};这样的形式
      • 代码如下:
            ab={
                'Swaroop':'swaroop@swaroopch.com',
                'Larry':'larry@wall.org',
                'Matsumoto':'matz@ruby-lang.org',
                'Spammer':'spammer@hotmail.com'
            }
            print("dictionaries is",ab);
            print('Swaroop\'s address is',ab['Swaroop']);
            del ab['Spammer'];
            print('\nThere are {} contacts in the addresss-book'.format(len(ab)));
            for name,address in ab.items():
                print('Contact {} at {}'.format(name,address));
                
                #增加一对键值
            ab['Guido']='guido@python.org'
            if 'Guido' in ab:
                print('\nGuido\'s address is',ab['Guido']);
    
    • 集合
      • 是简单对象的无序集合。当集合中的项目存在与否比起次序或其出现次数更加重要时,我们就会使用集合
      • 示例代码:
             bri=set(['brazil','russia','india']);
             'india' in bri
            True
             'usa' in bri
            False
             bric=bri.copy();
        
             bric.add('china');
                 
             bric.issuperset(bri)
                 
            True
             bri.remove('russia')
                 
             bri & bric
                 
            {'india', 'brazil'}
    

    序列概念

    列表、元组与字符串可以看作序列的某种表现形式,序列的主要功能是资格测试(Membership Test),

    也就是(in 或 not in表达式)和索引操作,上面提到的三种形态,同样拥有切片运算符;它能够允许我们

    序列中的某段切片--也就是序列中某一部分

    索引也可以使用负数,在这种情况下,位置计数将从队列的末尾开始。因此,shopList[-1]指的是序列的最

    后一个项目,shopList[-2]指的是将获取序列中倒数第二个项目

    切片操作

        shoplist=['apple','mongo','carrot','banana'];
        print('My shopping list is 1-3now',shoplist[:3]);
        print('My shopping list is 1-3now',shoplist[1:3]);
        print('My shopping list is 1-3now',shoplist[3:]);
        print('My shopping list is 1-3now',shoplist[:-1]);
        print('My shopping list is 1-3now',shoplist[:-2]);
        print('My shopping list is 1-3now',shoplist[:-3]);
        print('My shopping list is 1-3now',shoplist[::-1]);
    

    对应运行结果:

        My shopping list is 1-3now ['apple', 'mongo', 'carrot']
        My shopping list is 1-3now ['mongo', 'carrot']
        My shopping list is 1-3now ['banana']
        My shopping list is 1-3now ['apple', 'mongo', 'carrot']
        My shopping list is 1-3now ['apple', 'mongo']
        My shopping list is 1-3now ['apple']
        My shopping list is 1-3now ['banana', 'carrot', 'mongo', 'apple']
    

    引用

    当你创建了一个对象并将其分配给某个变量时,变量只会查阅(Refer)某个对象,并且它也不会代表对象本身。也就是说,变量名只是指向你计算机内存中存储了相应对象的那一部分。这叫做将名称绑定(Binding)给那一个对象

    • 示例代码

        print('Simple Assignment');
        shoplist=['apple','mango','carrot','banana'];
        #mylist只是指向同一对象的另一种名称
        mylist=shoplist;
        #删除第一项
        del shoplist[0];
        print('shoplist is',shoplist);
        print('mylist is',mylist);
        
        #注意到shoplist和mylist二者都打印出了其中都没有apple的列表
        #他们指向的是同一个对象
        print('Copy by making a full slice');
        mylist=shoplist[:];
        del mylist[0];
        print("shoplist is",shoplist);
        print("mylist is",mylist);
        #注意到现在两份列表已出现不同
        
        #Simple Assignment
        #shoplist is ['mango', 'carrot', 'banana']
        #mylist is ['mango', 'carrot', 'banana']
        #Copy by making a full slice
        #shoplist is ['mango', 'carrot', 'banana']
        #mylist is ['carrot', 'banana']
      

    相关文章

      网友评论

          本文标题:python基础知识点(数据结构相关)

          本文链接:https://www.haomeiwen.com/subject/zigwoxtx.html