파이썬 list 관련 질문드립니다.

조회수 495회

안녕하세요, python 관련 질문드립니다.

아래와 같은 3개의 list가 있습니다.

A = ['AA/AA', 'BB/BB', 'CC/CC']

B = [
        [{'web_links': [{'name': 'hi'}], 'ref': '**hello1**'}, 
         {'web_links': [{'name': 'hi'}], 'ref': '**hello2**'}, 
         {'web_links': [{'name': 'hi'}], 'ref': '**hello3**',}, 
         {'web_links': [{'name': 'hi'}], 'ref': '**hello4**'}], 
        [{'web_links': [{'name': 'hi'}], 'ref': '**hello1**'}, 
         {'web_links': [{'name': 'hi'}], 'ref': '**hello2**'}, 
         {'web_links': [{'name': 'hi'}], 'ref': '**hello3**'}, 
         {'web_links': [{'name': 'hi'}], 'ref': '**hello4**'}], 
        [{'web_links': [{'name': 'hi'}], 'ref': '**hello1**'}, 
         {'web_links': [{'name': 'hi'}], 'ref': '**hello2**'}, 
         {'web_links': [{'name': 'hi'}], 'ref': '**hello3**'}, 
         {'web_links': [{'name': 'hi'}], 'ref': '**hello4**'}]
    ]

C = [
        [{'created': '2020-01-23 04:32:45.000000000'}], 'ref': '**initial**'}], 
        [{'created': '2020-01-23 04:32:45.000000000'}], 'ref': '**initial**'}], 
        [{'created': '2020-01-23 04:32:45.000000000'}], 'ref': '**initial**'}]
    ]

여기서 for 문을 이용하여

['AA/AA', 'hello1, hello2, hello3, hello4', 'initial']
['BB/BB', 'hello1, hello2, hello3, hello4', 'initial']
['CC/CC', 'hello1, hello2, hello3, hello4', 'initial']

이런식으로 구분해서 각각의 list에 넣으려 하는데 생각처럼 잘 안되네요, 혹시 조언을 구할 수 있을까 해서 이렇게 글을 남깁니다.

  • 흠... 원하시는 결과모양의 1~3행 모두 2~3열 값이 똑같은데... 뭐를 어떻게 짝지어야 되는 것인지요? 엽토군 2020.1.28 00:35

2 답변

  • 좋아요

    2

    싫어요
    채택 취소하기

    C 값이 잘못되어 있습니다. 문법에만 맞게 수정하여 샘플 첨부합니다.

    A = ['AA/AA', 'BB/BB', 'CC/CC']
    
    B = [
            [{'web_links': [{'name': 'hi'}], 'ref': '**hello1**'}, 
             {'web_links': [{'name': 'hi'}], 'ref': '**hello2**'}, 
             {'web_links': [{'name': 'hi'}], 'ref': '**hello3**',}, 
             {'web_links': [{'name': 'hi'}], 'ref': '**hello4**'}], 
            [{'web_links': [{'name': 'hi'}], 'ref': '**hello1**'}, 
             {'web_links': [{'name': 'hi'}], 'ref': '**hello2**'}, 
             {'web_links': [{'name': 'hi'}], 'ref': '**hello3**'}, 
             {'web_links': [{'name': 'hi'}], 'ref': '**hello4**'}], 
            [{'web_links': [{'name': 'hi'}], 'ref': '**hello1**'}, 
             {'web_links': [{'name': 'hi'}], 'ref': '**hello2**'}, 
             {'web_links': [{'name': 'hi'}], 'ref': '**hello3**'}, 
             {'web_links': [{'name': 'hi'}], 'ref': '**hello4**'}]
        ]
    
    C = [
            {'created': '2020-01-23 04:32:45.000000000', 'ref': '**initial**'}, 
            {'created': '2020-01-23 04:32:45.000000000', 'ref': '**initial**'}, 
            {'created': '2020-01-23 04:32:45.000000000', 'ref': '**initial**'}
        ]
    
    pack = zip(A, B, C)
    result = [[t[0], ', '.join(d['ref'] for d in t[1]), t[2]['ref']] for t in pack]
    for l in result: print(l)
    
    ['AA/AA', '**hello1**, **hello2**, **hello3**, **hello4**', '**initial**']
    ['BB/BB', '**hello1**, **hello2**, **hello3**, **hello4**', '**initial**']
    ['CC/CC', '**hello1**, **hello2**, **hello3**, **hello4**', '**initial**']
    
    • 너무 감사합니다! ilalf 2020.1.28 20:04
  • 번외. 스칼라로 해보기.

    스칼라를 spark 때문에 2년전부터 사용하고 있는데 자바 개발자보다는 파이썬 개발자가 더 습득하기 좋은 것 같습니다.

    물론 fp(함수형 프로그래밍)나 mixin 같은 것은 양쪽 다 생소하겠구요.

    아래 스칼라 코드를 보면 아시겠지만 표현력이 파이썬 답변 코드와 똑같은 수준입니다.

    val A = Seq("AA/AA", "BB/BB", "CC/CC")
    
    val B = Seq(
                Seq(Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello1**"), 
                    Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello2**"),
                    Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello3**"), 
                    Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello4**")), 
            Seq(Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello1**"), 
                    Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello2**"),
                    Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello3**"), 
                    Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello4**")), 
            Seq(Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello1**"), 
                    Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello2**"),
                    Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello3**"), 
                    Map("web_links" -> Seq(Map("name"-> "hi")), "ref"-> "**hello4**"))
        )
    
    val C = Seq(
                Map("created"-> "2020-01-23 04:32:45.000000000", "ref"-> "**initial**"), 
                Map("created"-> "2020-01-23 04:32:45.000000000", "ref"-> "**initial**"), 
                Map("created"-> "2020-01-23 04:32:45.000000000", "ref"-> "**initial**")
        )
    
    val result = for(t <- (A, B, C).zipped.toList) yield (t._1, (for(d <- t._2) yield d("ref")).mkString(", "), t._3("ref"))
    for(l <- result) println(l)
    
    (AA/AA,**hello1**, **hello2**, **hello3**, **hello4**,**initial**)
    (BB/BB,**hello1**, **hello2**, **hello3**, **hello4**,**initial**)
    (CC/CC,**hello1**, **hello2**, **hello3**, **hello4**,**initial**)
    

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)