Put another array value in the Python for statement array.

Asked 2 years ago, Updated 2 years ago, 98 views

aa.py

records1 = [(a,1),(b,2),(c,3)]
records2 = [(a1,4)]

# Each array value has a records2 value as many as the number of records2 value.
i = 0
while i < len(records1):
    records1[i].append(records2)
    i = i + 1

Error:AttributeError: 'tuple' object has no attribute 'append

I want to put records2 value in each array value of records1, but I think the append type is not available. I looked it up in another way, but it didn't work.<

Finally, I'm going to make it into Json form with dict, so I'd appreciate it if you could give me some advice.

the final result

 records1=[(a,1,(a1,4),(b,2,(a1,4),(c,3,(a1,4))]
jsonify(dict(records1))
{
    {
       "a":1,
            {
              "a1":4
            }
    },
    {
        "b":2,
             {
                "a1":4
             }
    },
    {
        "c":3,
             {
                "a1":4
             }
    }
}

I think I didn't ask the right question, so I added it.

 records1 = [(((("a",1),("b",2),("c",3),("a1",(("a",2),("c",3),("a1",4)]
records2 = [(("bb",2),("cc",3),("aa1",4))]
records3 = [(("aa",1),("bb",2),("cc",3),("aa1",4))]

I think it'll be like this.

aa=[dict(record) for record in records1]
print(aa)
[{'a': 1, 'a1': 4, 'c': 3, 'b': 2, **Insert records2**, **Insert records3**}, {'a': 1, 'a1': 4, 'c': 3, 'b': 2,}]
aa = [{'a': 1, 'b': 2, 'c': 3, 'a1': 4,
           "records2":[{'cc': 3, 'aa1': 4, 'bb': 2}], "records3":[{'aa': 1, 'cc': 3, 'aa1': 4, 'bb': 2}] }]
--------------------------------------------------------

[
    {
        "a": 1,
        "a1": 4,
        "b": 2,
        "c": 3,
        "records2": [
            {
                "aa1": 4,
                "bb": 2,
                "cc": 3
            }
        ],
        "records3": [
            {
                "aa": 1,
                "aa1": 4,
                "bb": 2,
                "cc": 3
            }
        ]
    },
    .......

]

I've been trying to code. I uploaded it.

ae=[r + (records2,) for rin records1]
dict(record) for record in awe

[{'a': 1, 'a1': 4, 'c': 3, 'b': 2, (('bb', 1), ('bb', 2), ('cc', 3), ('aa1', 4)): (('aa', 1), ('bb', 2), ('cc', 3), ('aa1', 4))}, {'a': 1, 'a1': 4, 'c': 3, 'b': 2, (('bb', 1), ('bb', 2), ('cc', 3), ('aa1', 4)): (('aa', 1), ('bb', 2), ('cc', 3), ('aa1', 4))}]

python dictionary while-loop

2022-09-21 20:18

2 Answers

In [1]: [r + (records2[0],) for r in records1]
Out[1]: [('a', 1, ('a1', 4)), ('b', 2, ('a1', 4)), ('c', 3, ('a1', 4))]

In [2]: [r + (*records2,) for r in records1]
Out[2]: [('a', 1, ('a1', 4)), ('b', 2, ('a1', 4)), ('c', 3, ('a1', 4))]

It seems that json is not valid.

What are you trying to do?It has to be clear.


2022-09-21 20:18

I've coded it as much as possible, so you can look at it one by one.

records1 = [(("a",1),("b",2),("c",3),("a1",4)),(("a",1),("b",2),("c",3),("a1",4))]
records2 = [(("bb",2),("cc",3),("aa1",4))]
records3 = [(("aa",1),("bb",2),("cc",3),("aa1",4))]

m_r3 = [dict((n, m) for n, m in r) for r in records3]
m_r2 = [{n: m for n, min r} for r in records2] # You can also do this.
m_r1 = [r + (('records2', m_r2), ('records3', m_r3),) for r in records1]
result = [{n: m for n, m in r} for r in m_r1]

m_r2
Out[86]: [{'bb': 2, 'cc': 3, 'aa1': 4}]

m_r3
Out[87]: [{'aa': 1, 'bb': 2, 'cc': 3, 'aa1': 4}]

m_r1
Out[88]: 
[(('a', 1),
  ('b', 2),
  ('c', 3),
  ('a1', 4),
  ('records2', [{'bb': 2, 'cc': 3, 'aa1': 4}]),
  ('records3', [{'aa': 1, 'bb': 2, 'cc': 3, 'aa1': 4}])),
 (('a', 1),
  ('b', 2),
  ('c', 3),
  ('a1', 4),
  ('records2', [{'bb': 2, 'cc': 3, 'aa1': 4}]),
  ('records3', [{'aa': 1, 'bb': 2, 'cc': 3, 'aa1': 4}]))]

result
Out[90]: 
[{'a': 1,
  'b': 2,
  'c': 3,
  'a1': 4,
  'records2': [{'bb': 2, 'cc': 3, 'aa1': 4}],
  'records3': [{'aa': 1, 'bb': 2, 'cc': 3, 'aa1': 4}]},
 {'a': 1,
  'b': 2,
  'c': 3,
  'a1': 4,
  'records2': [{'bb': 2, 'cc': 3, 'aa1': 4}],
  'records3': [{'aa': 1, 'bb': 2, 'cc': 3, 'aa1': 4}]}]


2022-09-21 20:18

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.