Closed . This question needs to be more focused . It is not currently accepting answers.
If the "list names" are always in the same order, you can do this using a list comprehension with zip
and sum
:
>>> data = [[('Lista-A', 1), ('Lista-X', 1), ('Lista-Z', 4)], [('Lista-A', 2), ('Lista-X', 0), ('Lista-Z', 1)], [('Lista-A', 5), ('Lista-X', 1), ('Lista-Z', 0)], [('Lista-A', 0), ('Lista-X', 1), ('Lista-Z', 4)]]
>>> [(col[0][0], sum(x for _, x in col)) for col in zip(*data)]
[('Lista-A', 8), ('Lista-X', 3), ('Lista-Z', 9)]
zip(*data)
is a standard way to iterate over "columns" from a list of "rows". Assuming the columns do line up, then each column contains all the numbers to be summed for the same "list name". The sum
function adds the numbers; the columns contain tuples where we want to sum the numbers in the second component, hence the destructuring assignment _, x
.
5. Data Structures — Python 3.3.7 documentation, They are two examples of sequence data types (see Sequence Types — list, tuple, range). Since Python is an evolving language, other sequence� Lists and tuples are arguably Python’s most versatile, useful data types.You will find them in virtually every nontrivial Python program. Here’s what you’ll learn in this tutorial: You’ll cover the important characteristics of lists and tuples.
This should work
from collections import defaultdict
input = [[('Lista-A', 1), ('Lista-X', 1), ('Lista-Z', 4)], [('Lista-A', 2), ('Lista-X', 0), ('Lista-Z', 1)], [('Lista-A', 5), ('Lista-X', 1), ('Lista-Z', 0)], [('Lista-A', 0), ('Lista-X', 1), ('Lista-Z', 4)]]
d = defaultdict(int)
for i in input:
for j in i:
d[j[0]] += j[1]
res = list(d.items())
5. Data Structures — Python v3.1.5 documentation, list.append(x): Add an item to the end of the list; equivalent to a[len(a):] = [x]. If no index is specified, a.pop() removes and returns the last item in the list. If the expression would evaluate to a tuple, it must be parenthesized. We saw that lists and strings have many common properties, such as indexing� A comparitive study of Lists, Tuples, and Sets should help in better understanding of these objects as they look kind of similar. Few prerequisites for this post, are that You should have Python3 on your system and you know how to access the Python interpreter or how to run Python scripts, and you have good understanding of fundamental data types such as Strings, Integers, and finally
Not the most elegant "pythonic" way, but using your code
new_lista = {}
for lista in p:
for i in lista:
new_lista[i[0]] = new_lista.get(i[0], 0) + i[1]
p1 = [(k,v) for k,v in new_lista.items()]
Python: Generate a list and tuple with comma-separated numbers , Python: Generate a list and tuple with comma-separated numbers In general, we can define a list as an object that contains multiple data items Tuples are like lists, except they are immutable (i.e. you cannot change numbers = input(" Enter numbers seprated by , : ") # you can add str(input()) method You can also use the sum () function to add a constant value for the sum total of the list. The syntax of the sum () function is sum (iterable, start). So here, start means a value that can be summed up to the list and iterable means list, tuple, dict, etc., p=[2,3,5]
A horrible one-liner after the import, but it does the trick!
>>> from functools import reduce
>>> reduce(lambda x, y: dict((k, v + y[k]) for k, v in x.items()), [dict(i) for i in input])
{'Lista-A': 8, 'Lista-X': 3, 'Lista-Z': 9}
List in Python : lists, range, slice, append, len, etc in Python, It means that it will give a sequence with the numbers in arithmetic progression with a difference of 2 as done in the above example. We can also have a different � Python can also implicitly convert a value to another type. In the example below, the evaluation of 1 <= 0 is a false statement, so the output of the evaluation will be False. The number 1 can be converted to True as a bool type, while 0 converts to False.
11. Lists — How to Think Like a Computer Scientist: Learning with , Lists and strings — and other collections that maintain the order of their items — are Finally, a list with no elements is called an empty list, and is denoted []. And we can add elements to a list by squeezing them into an empty slice at the Of course, for immutable objects (i.e. strings, tuples), there's no problem — it is just� list_num = [1,2,3,4] tup_num = (1,2,3,4) print(list_num) print(tup_num) Output: [1,2,3,4] (1,2,3,4) Above, we defined a variable called list_num which hold a list of numbers from 1 to 4 .The list is surrounded by brackets []. Also, we defined a variable tup_num; which contains a tuple of number from 1 to 4.
9. Lists — How to Think Like a Computer Scientist: Learning with , Lists and strings — and other things that behave like ordered sets — are called sequences. numbers[-1] is the last element of the list, numbers[-2] is the second to last, Lists that contain consecutive integers are common, so Python provides a And we can add elements to a list by squeezing them into an empty slice at� Tuples are sequences, just like lists. The differences between tuples and lists are, the tuples cannot be changed unlike lists and tuples use parentheses, whereas lists use square brackets. Creating a tuple is as simple as putting different comma-separated values. Optionally you can put these comma-separated values between parentheses also.
Lists and Tuples in Python – Real Python, You'll cover the important characteristics of lists and tuples in Python 3. You'll A list can contain any number of objects, from zero to as many as your computer's x[1][1] is yet another sublist, so adding one more index accesses its elements:. I'm making a recipe handling program using Python and TkInter. I have a list that contains tuples => (Ingredient, Quantity). I need to use a label to place it onto the screen.
Comments please edit your post and include your attempt with for cycle Done! Don't know if it helps because I don't really know what to put under the for cylcle "I don't know how to code this" is not a Stack Overflow question. There's nothing in your code that attempts to sum the list elements. On topic, how to ask, and ... the perfect question apply here. If you don't know how to identify and sum things, consider working through tutorials on grouping, sorting, and summation. If you do know, then please make an honest try and post that. I agree with @Prune, I suggest learning more about programming in general. I was trying to do something more simple using just "for cycle", but thanks!! @AnaLuizaTavares You have a nested list. Using a single for
"cycle" will not help you much...