ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • 얕은 복사 vs 깊은 복사
    공부/Python 2020. 7. 26. 02:14

    Q. : data의 // 기준으로 첫번째 두번째는 문자, 세번째 네번째는 숫자인 원소만 출력하게 하는 코드입니다.

    if 문에서 result에 append 하고 normal 을 확인해보면 제 생각과 다르게 출력이 됩니다.

    data = ["A//a//1//10"
    ,"   B     //b//2//20",
    "C//c//3//30",
    "D   //     //    d//40",
    "E //5//e//50"]
    
    stripdata = []
    result = []
    
    for i in data:
    
        #stripdata = []
        temp = i.split('//')
    
        for j in range(0, len(temp)):
            stripdata.append(temp[j].strip())
    
        if stripdata[0].isalpha() and stripdata[1].isalpha() and stripdata[2].isdigit() and stripdata[3].isdigit():
            result.append(stripdata)
        print(result)
        stripdata.clear()
    
    print(result)
    
    [['A', 'a', '1', '10']]
    [['B', 'b', '2', '20'], ['B', 'b', '2', '20']]
    [['C', 'c', '3', '30'], ['C', 'c', '3', '30'], ['C', 'c', '3', '30']]
    [['D', '', 'd', '40'], ['D', '', 'd', '40'], ['D', '', 'd', '40']]
    [['E', '5', 'e', '50'], ['E', '5', 'e', '50'], ['E', '5', 'e', '50']]
    [[], [], []]
    

    A. : 파이썬의 얕은 복사 문제입니다!

    data = ["A//a//1//10"
    ,"   B     //b//2//20",
    "C//c//3//30",
    "D   //     //    d//40",
    "E //5//e//50"]
    
    stripdata = []
    result = []
    
    for i in data:
    
        temp = i.split('//') # //로 나누기
    
        for j in range(0, len(temp)):
            stripdata.append(temp[j].strip()) # 빈칸제거
    
        if stripdata[0].isalpha() and stripdata[1].isalpha() and stripdata[2].isdigit() and stripdata[3].isdigit():
            rightdata = stripdata.copy()
            result.append(rightdata)
        stripdata.clear()
    
    print(result)
    [['A', 'a', '1', '10'], ['B', 'b', '2', '20'], ['C', 'c', '3', '30']]

    '공부 > Python' 카테고리의 다른 글

    [Python] 가상환경에 pip 설치한 패키지 제거  (0) 2020.08.06
Designed by Tistory.