ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • python filter 사용
    파이썬 2021. 4. 26. 18:35

     

    test_string = """
    [{
        'segmentation': [
            [216.7, 211.89, 216.16, 217.81, 215.89, 220.77, 215.89, 223.73, 217.77, 225.35, 219.12, 224.54, 219.12, 220.5, 219.66, 217.27, 219.93, 212.7, 220.46, 207.85, 219.66, 203.01, 218.85, 198.43, 217.77, 195.74, 216.7, 194.93, 215.62, 190.62, 215.62, 186.59, 214.27, 183.89, 211.85, 184.16, 211.85, 187.66, 210.24, 187.66, 209.16, 184.97, 207.81, 183.36, 205.12, 186.59, 205.12, 189.28, 201.08, 192.78, 199.74, 195.2, 196.78, 200.04, 196.51, 203.01, 198.12, 205.43, 197.32, 209.2, 196.78, 213.23, 197.05, 218.89, 199.74, 221.85, 201.62, 225.35, 201.62, 233.69, 201.08, 236.11, 202.97, 236.38, 204.85, 236.11, 204.58, 232.34, 203.78, 228.85, 205.39, 233.15, 207.81, 235.57, 208.62, 234.23, 206.74, 231.27, 205.12, 228.04, 206.74, 222.39, 208.35, 219.96, 210.77, 217.54, 211.85, 221.85, 214.54, 223.73, 212.93, 217.54, 212.93, 215.66, 215.89, 212.96, 216.16, 212.16]
        ],
        'area': 759.3375500000002,
        'iscrowd': 0,
        'image_id': 324158,
        'bbox': [196.51, 183.36, 23.95, 53.02],
        'category_id': 18,
        'id': 10673
    }, {
        'segmentation': [
            [223.48, 251.26, 230.81, 246.74, 234.48, 247.6, 241.8, 247.6, 247.41, 243.72, 248.7, 244.15, 252.15, 249.54, 252.15, 254.71, 249.78, 255.79, 247.19, 260.32, 243.1, 263.33, 235.77, 263.33, 224.56, 262.47, 223.91, 259.24, 224.13, 254.5]
        ],
        'area': 409.74355,
        'iscrowd': 0,
        'image_id': 324158,
        'bbox': [223.48, 243.72, 28.67, 19.61],
        'category_id': 41,
        'id': 638724
    }, {
        'segmentation': [
            [228.43, 247.9, 229.63, 206.62, 224.24, 191.07, 220.65, 179.7, 207.49, 169.53, 202.71, 163.55, 205.7, 133.04, 218.86, 121.68, 213.47, 104.33, 225.44, 96.55, 236.8, 106.12, 236.8, 116.29, 254.15, 127.06, 263.72, 150.39, 274.49, 166.54, 271.5, 177.31, 266.12, 181.5, 257.14, 159.96, 254.75, 177.91, 261.93, 192.27, 262.53, 216.79, 261.33, 234.14, 268.51, 249.1, 247.57, 246.11, 245.78, 249.69, 229.03, 248.5]
        ],
        'area': 5999.544500000001,
        'iscrowd': 0,
        'image_id': 324158,
        'bbox': [202.71, 96.55, 71.78, 153.14],
        'category_id': 1,
        'id': 2162813
    }]
    """

     

    이러한 test_string 이 있는 경우,

     

    여기서 'category_id' 가 1 인 경우만 추출하고 싶은 경우 사용하는 함수가 filter 라는 함수 이다.

     

    우선, test_string을 json 화 해야 객체 다루기 편하므로, 다음과 같이 작은 따옴표를 쌍 따옴표로 변경 후,

    json_strings 변수로 변경합니다.


    json_strings = json.loads(test_string.replace('\'', '\"'))

    후에 다음처럼 list 와 filter를 사용합니다.

     

    test = list(filter(lambda x: x['category_id'] == 1, json_strings))

    test = list(filter(lambda x: x['category_id'] == 1, json_strings))
    print('test:', test)

     

     

    다음과 같은 결과를 확인 하실 수 있습니다.

    test: [{'segmentation': [[228.43, 247.9, 229.63, 206.62, 224.24, 191.07, 220.65, 179.7, 207.49, 169.53, 202.71, 163.55, 205.7, 133.04, 218.86, 121.68, 213.47, 104.33, 225.44, 96.55, 236.8, 106.12, 236.8, 116.29, 254.15, 127.06, 263.72, 150.39, 274.49, 166.54, 271.5, 177.31, 266.12, 181.5, 257.14, 159.96, 254.75, 177.91, 261.93, 192.27, 262.53, 216.79, 261.33, 234.14, 268.51, 249.1, 247.57, 246.11, 245.78, 249.69, 229.03, 248.5]], 'area': 5999.544500000001, 'iscrowd': 0, 'image_id': 324158, 'bbox': [202.71, 96.55, 71.78, 153.14], 
    'category_id': 1, 'id': 2162813}]

    '파이썬' 카테고리의 다른 글

    크롤러 만들기: session 처리 필요한 경우  (0) 2021.07.21
    파이썬에 대해서  (0) 2019.05.26
Designed by Tistory.