How can sum two nested list in this situation

2024/7/7 6:45:24

Given list a, b

a=[[[1.1,-2.1],[-0.6,4.2]],[[3.9,1.3],[-1.3,1.2]]]b=[[-1.1,4.3],[-1.4,2.4]]

If I just want to sum the list [[1.1,-2.1],[-0.6,4.2]] in the list a (not the whole list a) with the list [-1.1,4.3] in list b.

For instance 1.1+(-1.1) then (-2.1)+4.3 and so on. After that store back to an empty list. Can I do this with for loop? In this case, the final output will be[[0, 2.2],[-1.7, 8.5]]

Answer

Use:

import numpy as np
c = (np.array(a[0]) + b[0]).tolist()

Output:

>>> c
[[0.0, 2.1999999999999997], [-1.7000000000000002, 8.5]]

Update Using for loop:

 c = []for row in a[0]: c.append([])for x, y in zip(row, b[0]):c[-1].append(x+y)

Update: Using list comprehension

c = [[x + y for x,y in zip(row, b[0])] for row in a[0]]

Output:

>>> c
[[0.0, 2.1999999999999997], [-1.7000000000000002, 8.5]]
https://en.xdnf.cn/q/120058.html

Related Q&A

Create check digit function

Im trying to create check digits and append them after the original UPCs. Heres the sample data Because there are leading 0s, I have to read the data as strings first: import pandas as pd …

Getting count of permutations in a faster way

Using this code to get count of permutations is slow on big numbers as the partition part takes long time to calculate all the partitions for a number like 100 and because of all the partitions in the …

How to find number of vowels in each word of string? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…

Python error: AttributeError: str object has no attribute read

My full code:import requests as req import json Bin = int(300000) BinMax = int(600000) File = open("C:/Users/admin/Desktop/PS Now Generaetors/Bins.txt", a)while bin != BinMax:json1 = req.get(…

list elements sum of one list first element to last element of second list

How to sum first element of one list to last element of second list if both list contains same amount of elements in python. Source code (should be kind of like this): def update(l1,l2,l3):for i in ran…

Append float data at the end of each line in a text file

I have a .txt file and Im trying to add float number at the end of each line with incremented value than the last line. This is what I have in the .txt file:>520.980000 172.900000 357.440000 >320…

Change and Sort list in list in specific order in python

Hello guys I have this type of data in list in list array = [ ["PRODUCT NAME PACK","BAIGAM KOT","FIAZ BAGH","OLD ANARKALI","SULTAN PURA","TEZAB AA…

How to interact with the reCAPTCHA Solve the challenge button using Selenium and Python

Im trying to interact with the recaptcha Solve the challenge button on image verification popup using Selenium and Python. The xpath looks correct in dev tools but using Selenium unable to interact wit…

How to convert an adjacency matrix to an adjacency list with python?

I have an adjacency matrix like: [[ 0., 15., 0., 7., 10., 0.],[ 15., 0., 9., 11., 0., 9.],[ 0., 9., 0., 0., 12., 7.],[ 7., 11., 0., 0., 8., 14.],[ 10., 0., 12., …

how to plot a box plot of a column of a data frame in two groups in matplotlib

I have the following data frame:value total_spend margin1 29493.14 10203.371 27551.22 19003.072 22881.26 15142.681 15254.77 12337.221 11873.95 9424.231 11382.89 8767.83…