I have the following sublist format:
x = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]]
y = [[11, 22, 33, 44], [55, 66, 77, 88], [99,100, 111, 122]...]
Above is a sublist containing the information of the "x" and "y" coordinates (the length can be changed accordingly).
Now I like take two sets and make pair of coordinates as following:
x_y = [[(1,11),(2,22),(3,33),(4,44)],[(5,55),(6,66),(7,77),(8,88)],[(9,99),(10,100),(11,111),(12,122)...]
any help would be appreciated. Thanks.
You could use zip twice:
x_y = [zip(a, b) for a, b in zip(x, y)]
print(x_y)
Output:
[[(1, 11), (2, 22), (3, 33), (4, 44)], [(5, 55), (6, 66), (7, 77), (8, 88)], [(9, 99), (10, 100), (11, 111), (12, 122)]]
I have the following sublist format:
mylist_x_y = [[1, 2, 3], [10, 20, 30]]
the [1, 2, 3] are X coordinates and the [10, 20, 30] Y coordinates.
Now I like them to be the following tuple format:
mylist_x_y_new = [(1, 10), (2, 20), (3, 30)]
Use zip with the list's elements:
mylist_x_y_new = zip(*mylist_x_y)
print(mylist_x_y_new)
Output:
[(1, 10), (2, 20), (3, 30)]
I have this list:
list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7]
And I wat to get rid of duplicate values. The code for the map function is taken from here.
this is the complete testing code:
list1 = [1, 1, 1, 3, 3, 3, 56, 6, 6, 6, 7]
list2 = []
map(lambda x: not x in list2 and list2.append(x), list1)
print(list2)
list2 = []
[list2.append(c) for c in list1 if c not in list2]
print(list2)
list2 = []
for c in list1:
if c not in list2:
list2.append(c)
print(list2)
In Python 2.7 is prints:
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
In Python 3.4 it prints:
[]
[1, 3, 56, 6, 7]
[1, 3, 56, 6, 7]
Why the map function returns an empty list in Python3?
Because in python-3.x a map is not evaluate immediately. It works as a generator where elements are generated on the fly by need: this can be more efficient since it is possible you for instance only need the first three elements so why would you calculate all elements? So as long as you do not materialize the output of map in a way, you have not really calculated the map.
You can for instance use list(..) to force Python to evaluate the list:
list(map(lambda x: not x in list2 and list2.append(x), list1))
In that case python-3.x will generate the same result for list2.
For summation purposes, I created a list of tuples, where multiple items in the list have the same first variable.
for example:
x = [('m32',[1,2,3]),('m32',[2,3,4]),('m32',[4,5,6]),('m33',[1,2,3]),('m33',[2,3,4]),('m33',[4,5,6]),('m34',[1,2,3]),('m34',[2,3,4]),('m34',[4,5,6])....]
I want to add the individual values of the second items in the tuples (i.e 1+2+4, 2+3+5, 3+4+6) for all values that have the same first item (i.e. 'm32').
In other words, for all items labeled 'm32', I want to be able to add the other values.
How can I slice/index this to loop through and perform the summation?
Some tricky zip magic, along with itertools.groupby to group the matching first items together:
>>> x = [('m32',[1,2,3]),('m32',[2,3,4]),('m32',[4,5,6]),('m33',[1,2,3]),('m33',[2,3,4]),('m33',[4,5,6]),('m34',[1,2,3]),('m34',[2,3,4]),('m34',[4,5,6])]
>>> from itertools import groupby
>>> from operator import itemgetter
>>> for k,g in groupby(x,key=itemgetter(0)):
... print (k,[sum(i) for i in zip(*zip(*g)[1])])
...
('m32', [7, 10, 13])
('m33', [7, 10, 13])
('m34', [7, 10, 13])
A breakdown of how it works:
g is the group of items with matching keys. zip(*g) transposes the matrix, bringing the keys and values together:
>>> for k,g in groupby(x,key=itemgetter(0)):
... print zip(*g)
...
[('m32', 'm32', 'm32'), ([1, 2, 3], [2, 3, 4], [4, 5, 6])]
[('m33', 'm33', 'm33'), ([1, 2, 3], [2, 3, 4], [4, 5, 6])]
[('m34', 'm34', 'm34'), ([1, 2, 3], [2, 3, 4], [4, 5, 6])]
Getting the 2nd items:
>>> for k,g in groupby(x,key=itemgetter(0)):
... print zip(*g)[1]
...
([1, 2, 3], [2, 3, 4], [4, 5, 6])
([1, 2, 3], [2, 3, 4], [4, 5, 6])
([1, 2, 3], [2, 3, 4], [4, 5, 6])
Transposing again to match up the items to sum:
>>> for k,g in groupby(x,key=itemgetter(0)):
... print zip(*zip(*g)[1])
...
[(1, 2, 4), (2, 3, 5), (3, 4, 6)]
[(1, 2, 4), (2, 3, 5), (3, 4, 6)]
[(1, 2, 4), (2, 3, 5), (3, 4, 6)]
And adding them up:
>>> for k,g in groupby(x,key=itemgetter(0)):
... print [sum(i) for i in zip(*zip(*g)[1])]
...
[7, 10, 13]
[7, 10, 13]
[7, 10, 13]
The answer given by Mark is great, and probably much more efficient that the one I'll post you. But I still want to post my answer because you are probably new to python and it will be easy for you to understand it.
For this kind of scripts you only need some imagination and basic python notions:
dictionary={}
for name, numbers in x:
if name in dictionary:
current_list=dictionary[name]
for i in range(3):
current_list[i]+=numbers[i]
else:
dictionary[name]=numbers
print(dictionary)
Note that the output is a dictionary:
{'m32': [7, 10, 13], 'm33': [7, 10, 13]}..
I hope it help you!
Is there a groovier way of doing this? That is, create a new list from the sum of groups of 3 numbers from the original list.
myList = [1,2,3,4,5,6,7,8,9]
newList = []
while (myList.size > 0) {
newList.add(myList.pop() + myList.pop() + myList.pop())
}
println newList.reverse()
[6, 15, 24]
How about this:
myList.collate(3).collect {it.sum()}
or with just a fine use of spread operator *
myList.collate(3)*.sum()
You can group the list into sublists of 3 elements with collate:
groovy:000> myList = [1,2,3,4,5,6,7,8,9]
===> [1, 2, 3, 4, 5, 6, 7, 8, 9]
groovy:000> myList.collate(3)
===> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Then sum each sublist; the sum can be done with inject:
groovy:000> myList.collate(3)*.inject(0) { sum, i -> sum + i }
===> [6, 15, 24]
or just use this convenience method sum
groovy:000> myList.collate(3)*.sum()
===> [6, 15, 24]