Related
I've been deleting all occurrences unintentionally. I would like to keep at least one set of occurrences.
For example, I have [[1,2,3],[1,3,2],[4,5,6],[5,6,4]] and the desired output would be akin to [[1,2,3],[4,5,6]].
s = 1,2,3,4,5,6
c = [[1,2,3],[4,5,6],[4,6,5]]
remove_sets = []
for a in range(0, len(c)):
for b in permutations(c[a], 3):
# my idea is that if list(b) != c[a]
# it should not delete all occurences.
if list(b) != c[a]:
if list(b) in c:
remove_sets.append(list(b))
# delete those occurences.
for cc in range(0, len(remove_sets)):
if remove_sets[cc] in c:
del c[c.index(remove_sets[cc])]
Unintended Result/Output
[[1,2,3]]
My desired output would be
[[1,2,3],[4,5,6]]
Question
Is there a function for removing these duplicate sets where order is switched around?
groupby works if your duplicate sublists are already adjacent and you need to squeeze them into single units:
>>> from itertools import groupby
>>> [next(v) for _, v in groupby(c, sorted)]
[[1, 2, 3], [4, 5, 6]]
Caling sorted ignores order when grouping, so we can skim the first item from each group to obtain your result.
Otherwise, for the general case, using a dictionary comprehension like
>>> list({tuple(sorted(x)): x for x in c}.values())
[(1, 2, 3), (4, 6, 5)]
works but it only selects the last-seen item in c. If you reverse-iterate c, you'll get the first-seen:
>>> list({tuple(sorted(x)): x for x in c[::-1]}.values())[::-1]
[(1, 2, 3), (4, 5, 6)]
Be aware that sorted is O(n(log(n))), resulting in an overall complexity of
longest_len = max(map(len, c))
O(n * longest_len * log(longest_len))
If you need to scale to large inner lists, consider collections.Counter instead of sorted.
Using map can do operations of current element in list:
l = [1,2,3,4,5,6];
print(list(map(lambda x: x*2, l)))
# gives [2, 4, 6, 8, 10, 12]
In above, multiply by 2 is done for all elements in l. But how to mulitiply by 2 an element in l, only if previous value in l is odd? Can use map for this?
For example to get:
[1,4,3,8,5,12] % only 2, 4 and 6 from l are multiplyied by 2, because before them there are odd numbers 1,3,5.
You can use map if you do it on an enumerated version:
print(list(map(lambda index,x: x*2 if index > 1 and l[index-1] & 1 else x, enumerate(l))))
However, as you might have noticed, that's really not very readable. It's better to just use a list comprehension or a for loop:
print([x*2 if index > 1 and l[index-1] & 1 else x
for index, x in enumerate(l)])
You can use zip in combination with map:
print(l[:1] + list(map(lambda x: x[1]*2 if x[0] & 1 else x[1], zip(l, l[1:]))))
Note that I had to explicitly prepend the first element of the list because it has no previous element to test.
You can zip the list along with a sliced copy of the list to pair all of the items:
>>> l = [1, 2, 3, 4, 5, 6]
>>> zip(l, l[1:])
[(1, 2), (2, 3), (3, 4), (4, 5), (5, 6)]
You can use something callable to track your previous value. Depending on how abstract you want this:
prev = [0]
def callback(x):
val = ((prev[0] % 2) + 1) * x
prev[0] = x
return val
print list(map(callback, l))
I realise that it was probably just a trivial example, but thought it worth mentioning that in your example case the condition "if the previous value is odd" is the same as "if the current value is even" (at least for your sample input). In which case I would just use
print([x if x&1 else x*2 for x in l])
For the more general case and assuming the condition may be more complex than just "previous item is odd", I would take a few lines to express the solution clearly. Python's generators are a good fit:
>>> def double_if_prev_odd(l):
... prev_odd = False # initial condition for 1st element
... for x in l:
... yield x**2 if prev_odd else x
... prev_odd = x&1
...
>>> list(double_if_prev_odd(l))
[1, 4, 3, 16, 5, 36]
This question already has answers here:
How do I make a flat list out of a list of lists?
(34 answers)
Closed 6 months ago.
I have a list like this:
[[(video1,4)], [(video2,5),(video3,8)], [(video1,5)], [(video5, 7), (video6,9)]...]
each item in this list may contain a single data pair, or a tuple, I want to change this list into
[(video1,4),(video2,5),(video3,8),(video1,5),(video5,7),(video6,9)...]
then do this:
for item in list:
reqs = reqs + item[1]
b.append(item[0])
c = set(b)
I don't know how to change the list structure, or how to do the same calculation based on the original list?
There is a very simple way of doing this with list comprehensions. This example has been documented in the python documentation here
>>> # flatten a list using a listcomp with two 'for'
>>> vec = [[1,2,3], [4,5,6], [7,8,9]]
>>> [num for elem in vec for num in elem]
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Here is the solution that you would want to implement. As per your example, this is the simplest solution
In [59]: your_list = [[('video1',4)], [('video2',5),('video3',8)], [('video1',5)], [('video5', 7), ('video6',9)]]
In [60]: improved_list = [num for elem in your_list for num in elem]
In [61]: improved_list
Out[61]:
[('video1', 4),
('video2', 5),
('video3', 8),
('video1', 5),
('video5', 7),
('video6', 9)]
If this list is singly-nested (list of lists) you can do this, which I use a lot:
flat_list = sum(list_of_lists, [])
This works due to the fact that sum simply adds up the lists, and adding up lists works in python as expected :)
Note: This is inefficient and some say unreadable.
If you just want to flatten the list, just use itertools.chain.from_iterable: http://docs.python.org/library/itertools.html#itertools.chain.from_iterable
To flatten one level, you can use itertools.chain.from_iterable():
flattened_list = itertools.chain.from_iterable(my_list)
Here's another one (no libraries):
def plus(a,b): return a + b
reduce(plus, your_list)
Try this:
from itertools import chain
c = set()
reqs = 0
for vid, number in chain(*your_list):
c.add(vid)
reqs += number
Also see related post Flattening a shallow list in Python.
There should be negligible performance increase from using chain.from_iterable(list) rather than chain(*list), but it's true that the former looks cleaner.
to extract all tuples from a data structure ...
def get_tups(y):
z = []
if y:
for x in y:
if isinstance(x, tuple):
z.append(x)
else:
z.extend(get_tups(x))
return z
maybe ...
I have a list of tuples (each tuple item is a pair of integers) and I would like to add a constant value to each tuple in the list.
For example
[(x0,y0),(x1,y1),...] -> [(x0+xk,y0+yk),(x1+xk,y1+yk)....]
xk,yk are constants
How do I do this
Thanks
Use numpy, e.g.,
>>> import numpy as np
>>> a = np.array([[1,2],[2,3]])
>>> print a
[[1 2]
[2 3]]
>>> print a + 2
[[3 4]
[4 5]]
>>>> l = [(1,2), (3,4)]
>>>> for i, e in enumerate(l):
.... l[i] = (e[0]+xk, e[1]+yk)
As always, untested. ;-)
If you don't need to do it in place, it's even simpler
>>>> l = [(e[0]+xk, e[1]+yk) for e in l]
You can't add a constant to a tuple because tuples are immutable.
You can however create a new tuple from the old one by incrementing it's values. See jae's answer for the basics of how to do this.
You should note however that you will be creating a lot of new tuples in this loop and this may not be a very efficient way of handling this. You should investigate numpy (as suggested by nikow) or perhaps using lists or coordinate objects instead of tuples.
an example to add things to tuple
>>> a=(1,2)
>>> a+=(1,)
>>> a
(1, 2, 1)
you can adapt it to your requirement
Solution:
l = [(i[0]+k[0], i[1]+k[1]) for i in l]
Test code:
l = [(1,2), (3,4)]
k = (10, 100)
l = [(i[0]+k[0], i[1]+k[1]) for i in l]
assert l == [(11, 102), (13, 104)]
This question already has answers here:
How do I iterate through two lists in parallel?
(8 answers)
Closed 5 years ago.
I have a list of Latitudes and one of Longitudes and need to iterate over the latitude and longitude pairs.
Is it better to:
A. Assume that the lists are of equal lengths:
for i in range(len(Latitudes)):
Lat,Long=(Latitudes[i],Longitudes[i])
B. Or:
for Lat,Long in [(x,y) for x in Latitudes for y in Longitudes]:
(Note that B is incorrect. This gives me all the pairs, equivalent to itertools.product())
Any thoughts on the relative merits of each, or which is more pythonic?
This is as pythonic as you can get:
for lat, long in zip(Latitudes, Longitudes):
print(lat, long)
Another way to do this would be to by using map.
>>> a
[1, 2, 3]
>>> b
[4, 5, 6]
>>> for i,j in map(None,a,b):
... print i,j
...
1 4
2 5
3 6
One difference in using map compared to zip is, with zip the length of new list is
same as the length of shortest list.
For example:
>>> a
[1, 2, 3, 9]
>>> b
[4, 5, 6]
>>> for i,j in zip(a,b):
... print i,j
...
1 4
2 5
3 6
Using map on same data:
>>> for i,j in map(None,a,b):
... print i,j
...
1 4
2 5
3 6
9 None
Good to see lots of love for zip in the answers here.
However it should be noted that if you are using a python version before 3.0, the itertools module in the standard library contains an izip function which returns an iterable, which is more appropriate in this case (especially if your list of latt/longs is quite long).
In python 3 and later zip behaves like izip.
in case your Latitude and Longitude lists are large and lazily loaded:
from itertools import izip
for lat, lon in izip(latitudes, longitudes):
process(lat, lon)
or if you want to avoid the for-loop
from itertools import izip, imap
out = imap(process, izip(latitudes, longitudes))
Iterating through elements of two lists simultaneously is known as zipping, and python provides a built in function for it, which is documented here.
>>> x = [1, 2, 3]
>>> y = [4, 5, 6]
>>> zipped = zip(x, y)
>>> zipped
[(1, 4), (2, 5), (3, 6)]
>>> x2, y2 = zip(*zipped)
>>> x == list(x2) and y == list(y2)
True
[Example is taken from pydocs]
In your case, it will be simply:
for (lat, lon) in zip(latitudes, longitudes):
... process lat and lon
for Lat,Long in zip(Latitudes, Longitudes):
This post helped me with zip(). I know I'm a few years late, but I still want to contribute. This is in Python 3.
Note: in python 2.x, zip() returns a list of tuples; in Python 3.x, zip() returns an iterator.
itertools.izip() in python 2.x == zip() in python 3.x
Since it looks like you're building a list of tuples, the following code is the most pythonic way of trying to accomplish what you are doing.
>>> lat = [1, 2, 3]
>>> long = [4, 5, 6]
>>> tuple_list = list(zip(lat, long))
>>> tuple_list
[(1, 4), (2, 5), (3, 6)]
Or, alternatively, you can use list comprehensions (or list comps) should you need more complicated operations. List comprehensions also run about as fast as map(), give or take a few nanoseconds, and are becoming the new norm for what is considered Pythonic versus map().
>>> lat = [1, 2, 3]
>>> long = [4, 5, 6]
>>> tuple_list = [(x,y) for x,y in zip(lat, long)]
>>> tuple_list
[(1, 4), (2, 5), (3, 6)]
>>> added_tuples = [x+y for x,y in zip(lat, long)]
>>> added_tuples
[5, 7, 9]