I have a question about Python list.

Asked 2 years ago, Updated 2 years ago, 18 views

Hello, I have a question about Python.

There are three lists as below.

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**'}]
    ]

Use the for statement here

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

I'm trying to divide it into this way and put it in each list, but it doesn't work as I thought, so I'm writing like this to ask for advice.

python

2022-09-22 16:08

2 Answers

The C value is invalid. I am attaching a sample by modifying it to fit the grammar only.

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**']


2022-09-22 16:08

an extra charge Try it with scalar.

I've been using Scalar for 2 years because of Spark, and I think Python developers are better at learning than Java developers.

Of course, I'm sure you're both unfamiliar with things like fp and mixin.

As you can see from the scalar code below, the expression is the same as the Python answer code.

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**)


2022-09-22 16:08

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.