python 기본 데이터 구조 - 목록, 모듈, 사전, 집합

데이터 구조:
     :     ,   、  、   
     :  、  、  、  、      
      :              , 0    ;eg: string = '  ,  ',string[2]=,string[-1]= 
      :         string[5::-1]      ,   -1
      :       
      :>>> "a"*5
        'aaaaa'
        >>> ["a"]*5
        ['a', 'a', 'a', 'a', 'a']
        >>> ("a",)*5
        ('a', 'a', 'a', 'a', 'a')
        :     
        >>> "H" in "hollo"
        False
         :
                  ,            。:
In [82]: x,y,z = 1,2,3
In [83]: x,y = y,x
In [84]: print(x,y,z)
2 1 3

목록 목록
  :  list = [1,22,3]       
     :

     #         list1       
        list1 = [1, 3, 5, 7, 9]
        list1.append(8)   #       ,  
        list1.extend([2,1,3,9])  #        ,  
        list1.insert(1, 4)  #   ,    
        list1[-1] = 0   #        
        print(list1)
        del list1[-2]
        print(list1.pop(2))   #       ,    ,   -- 
        print(list1)
        list1.remove(1)   #           
        print(list1)
        print(list1.index(5))  #     ,             
        print(list1.count(6))   #        ,        
        list1.reverse() #     
        print(list1)
        list1.sort() #     ,    
        print(list1)
        list1.sort(reverse=True)
        print(list1)
        # list1.sorted() ????
        list2 = list1.copy()   #   
        print(list2)
        list1.clear()  #    
        print(list1)

원조
  :tuple(1,3,5,7,9)
  :1.        ;2.    (1,)
       

문자열
  :str1 = "abcdefg"
  :1.      (  、  、  、  、      、  )
2.       :
    1.   :         :str2 = "hello, %s"
    values = "world!"
    str2%values
        "hello, world!"
    2.   :.format  
		In [14]: "{0:-^20,.3f}".format(25.687925)
        Out[14]: '-------25.688-------'
        In [16]: "{0:-^20,.3e}".format(2545.687925)
        Out[16]: '-----2.546e+03------'
        :  ,   ,   ,    ,   ,      ,  ,       
3.  :
        In [17]: str1 = "hello, world!"
        In [19]: str1.center(20,"-")
        Out[19]: '---hello, world!----'
        In [20]: str1.find("l")
        Out[20]: 2
        In [21]: str1.find("g")
        Out[21]: -1
        In [22]: str1.find("l",3,10)
        Out[22]: 3
        In [23]: str1.find("l",4,10)
        Out[23]: -1
        In [29]: str1.title()
        Out[29]: 'Hello, World!'
        
        In [30]: str1.replace("world","python")
        Out[30]: 'hello, python!'
        
        In [31]: "    hello,    world!    ".strip()
        Out[31]: 'hello,    world!'
        
        In [32]: "    hello,    world!**    ".strip("*")
        Out[32]: '    hello,    world!**    '
        
        In [33]: "    hello,    world!**    ".strip("*!")
        Out[33]: '    hello,    world!**    '
        
        In [34]: "    hello,    world!****".strip("*!")
        Out[34]: '    hello,    world'
        
        In [35]: "    hello,    world!      ".lstrip()
        Out[35]: 'hello,    world!      '
        
        In [36]: "    hello,    world!      ".rstrip()
        Out[36]: '    hello,    world!'
        In [38]: trans = str.maketrans("l","L")
        ...: "  hello,  world!".translate(trans)
        Out[38]: '  heLLo,  worLd!'
    is+      

자전.
  :     ,          ,      
dict1 = {"zhangsan":"18","lisi":"19","wangwu":"17"}
  :

여기에 코드 를 삽입 합 니 다.
clear,copy,items,fromkeys,keys,values,pop,setdefult,update
    In [39]: dict1 = {"zhangsan":"18","lisi":"19","wangwu":"17"}
    In [40]: len(dict1)
    Out[40]: 3
    
    In [44]: dict1["zhangsan"]
    Out[44]: '18'
    
    In [45]: dict1["zhangsan"] = "22"  #        
    
    In [46]: dict1["zhangsan"]
    Out[46]: '22'
    
    In [47]: "liuqi" in dict1
    Out[47]: False  #    
   
    In [49]: dict1["liuqi"] = "21"
    # clear     ,            ,    
    In [50]: x = {}
    
    In [51]: y = x
    
    In [52]: x["key"]="value"
    
    In [53]: y
    Out[53]: {'key': 'value'}
    
    In [54]: x={}
    
    In [55]: y
    Out[55]: {'key': 'value'}
    
    In [56]: x=y
    
    In [57]: x
    Out[57]: {'key': 'value'}
    
    In [58]: x.clear()
    
    #    ,                   -  
    In [59]: y
    Out[59]: {}
    
    In [60]: dict1
    Out[60]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'liuqi': '21'}
    
    In [61]: dict2 = dict1.copy()
    
    In [62]: dict2["zhaojiu"]="24"
    
    In [63]: dict1
    Out[63]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'liuqi': '21'}
    
    In [64]: dict2
    Out[64]:
    {'zhangsan': '22',
     'lisi': '19',
     'wangwu': '17',
     'liuqi': '21',
     'zhaojiu': '24'}
    
    In [67]: {}.fromkeys(["name","age","sex"])  #create a dict has no values
    Out[67]: {'name': None, 'age': None, 'sex': None}
    
    In [68]: print(dict1.get("sunshi"))
    None
    
    In [70]: dict1.items() #            
    Out[70]: dict_items([('zhangsan', '22'), ('lisi', '19'), ('wangwu', '17'), ('liuqi', '21')])
    
    In [71]: dict1.keys()
    Out[71]: dict_keys(['zhangsan', 'lisi', 'wangwu', 'liuqi'])
    
    In [72]: dict1.pop("liuqi") # dilete the key and return the value
    Out[72]: '21'
    
    In [73]: dict1.setdefault("sunshi","25")
    Out[73]: '25'
    
    In [74]: dict1
    Out[74]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'sunshi': '25'}
    
    In [75]: x = {"sunshi","15"}
    
    In [76]: dict1
    Out[76]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'sunshi': '25'}
    
    In [78]: x = {"sunshi":"15"}
    
    In [79]: dict1.update(x)   # use a dict to update the dict
    
    In [80]: dict1
    Out[80]: {'zhangsan': '22', 'lisi': '19', 'wangwu': '17', 'sunshi': '15'}

집합 하 다.
  :   ,    ,         (    ),    
set{"abcdefghijk"}
     :    
    In [86]: set1 = {1,2,3,4,5,7,9,0}
    In [87]: set2 = {4,6,8,10,0}
    
    In [88]: set1&set2
    Out[88]: {0, 4}
    
    In [89]: set1.intersection(set2)
    Out[89]: {0, 4}
    
    In [90]: set1.union(set2)
    Out[90]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    
    In [91]: set1|set2
    Out[91]: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
    
    In [92]: set1-set2
    Out[92]: {1, 2, 3, 5, 7, 9}
    
    In [93]: set2-set1
    Out[93]: {6, 8, 10}
    
    In [94]: set1.difference(set2)
    Out[94]: {1, 2, 3, 5, 7, 9}
    In [95]: set1^set2
    Out[95]: {1, 2, 3, 5, 6, 7, 8, 9, 10}
    #      (<=,>=)
    In [96]: set1.issubset(set2)
    Out[96]: False
    In [97]: set1.issuperset(set2)
    Out[97]: False
    ```


좋은 웹페이지 즐겨찾기