zip function help with tuples

2024/9/29 5:27:14

I am hoping someone can help me with a problem I'm stuck with. I have a large number of tuples (>500) that look like this:

(2,1,3,6)  
(1,2,5,5)  
(3,0,1,6)  
(10,1,1,4)  
(0,3,3,0)  

A snippet of my code reads:

sum1 = (A,B,C,D) # creates a tuple of sums of (A,B,C,D)  
mysum = map(sum, zip(A, B, C, D))
print(mysum)

I realize the above code is not correct. I am trying to find a way to add all the values A together, all the values of B together, all the values of C together, and all the values of D together using the zip function. For example, I would like to print something that looks like this:

Asum = 16  
Bsum = 7  
Csum = 13  
Dsum = 21  

Can anyone help please? Thanks very much for your time.

Answer
>>> zip((1,2,3),(10,20,30),(100,200,300))
[(1, 10, 100), (2, 20, 200), (3, 30, 300)]>>> [sum(x) for x in zip((1,2,3),(10,20,30),(100,200,300))]
[111, 222, 333]

To do this with an arbitrarily large set of tuples:

>>> myTuples = [(1,2,3), (10,20,30), (100,200,300)]
>>> [sum(x) for x in zip(*myTuples)]
[111, 222, 333]

sidenote: in python3, note that zip returns a lazy iterable, which you can always explicitly turn into a list like any other kind of iterable: list(zip(...))

(thanks to Seganku for catching mistake in examples in an edit which was thrice rejected by other editors)

https://en.xdnf.cn/q/71252.html

Related Q&A

Mini batch training for inputs of variable sizes

I have a list of LongTensors, and another list of labels. Im new to PyTorch and RNNs so Im quite confused as to how to implement minibatch training for the data I have. There is much more to this data,…

Python gTTS, is there a way to change the speed of the speech

It seems that on gTTS there is no option for changing the speech of the text-to-speech apart from the slow argument. I would like to speed up the sound by 5%. Any suggestion on how I can do it? Best.t…

Change QLabel text dynamically in PyQt4

My question is: how can I change the text in a label? The label is inside a layout, but setText() does not seem to work - maybe I am not doing it right. Here is my code:this is the Main windows GUI, t…

Setting figure size to be larger than screen size in matplotlib

Im trying to create figures in matplotlib that read nicely in a journal article. I have some larger figures (with subfigures) that Id like to take up nearly an entire page in portrait mode (specificall…

Tensorflow 0.7.1 with Cuda Toolkit 7.5 and cuDNN 7.0

I recently tried to upgrade my Tensorflow installation from 0.6 to 0.7.1 (Ubuntu 15.10, Python 2.7) because it is described to be compatible with more up-to-date Cuda libraries. Everything works well i…

How to export tensor board data?

In the tensorborads README.md, it ask me to do like this:How can I export data from TensorBoard?If youd like to export data to visualize elsewhere (e.g. iPython Notebook), thats possible too. You can…

Releasing Python GIL while in C++ code

Ive got a library written in C++ which I wrap using SWIG and use in python. Generally there is one class with few methods. The problem is that calling these methods may be time consuming - they may han…

How to include the default TEMPLATE_CONTEXT_PROCESSORS in the new TEMPLATES setting in Django 1.10

Im upgrading a project to Django 1.10 and it has code like the following:from django.conf.global_settings import TEMPLATE_CONTEXT_PROCESSORS as TCPTEMPLATE_CONTEXT_PROCESSORS = TCP + (django.template.c…

Selecting best range of values from histogram curve

Scenario :I am trying to track two different colored objects. At the beginning, user is prompted to hold the first colored object (say, may be a RED) at a particular position in front of camera (marked…

dash_bootstrap_components installed succesfully but no recognised

I have my dash working perfectly. I have installed dash_bootstrap_components to give style to my dash. I wrote pip install dash-bootstrap-components and was perfectly installed. But when I run the app,…