Related
I'm trying to figure out if there is an efficient way of slicing a 2d-list according to the first element of each inner list, when there are multiple identical first elements.
I haven't used numpy, in fact I've never used it before, but I'm open to it. Honestly, I could come up with a very cumbersome way to do this without it, but I'm hoping for more efficiency.
list_to_slice = [[1, 2], [2, 2], [2, 3], [3, 3]]
My goal in this example is to slice the 2d list according to the first element. The goal output is:
[[1, 2], [2, 2], [2, 3]]
EDIT:
Okay, what I want to do is slice all of the inner list elements of the original list with the first element "2". Everything from there to the beginning of the list. The basic command to do so would be:
list_to_slice[0:3]
I'm trying to see if there is an efficient command anywhere, in numpy or elsewhere, to slice the list according to its content and not its location in the list. Like I said, I could probably find a way to do this, I'm just curious if there is a simpler command available in any libraries out there.
Thanks!
itertools.takewhile is made specifically for this kind of thing. It will turn your list into an itertator that takes values from the list until a condition is not longer true and then stop:
from itertools import takewhile
list_to_slice = [[1, 2], [2, 2], [2, 3], [3, 3]]
sl = takewhile(lambda t: t[0] < 3, list_to_slice) # take items while first element is less than 3
list(sl)
# [[1, 2], [2, 2], [2, 3]]
I think you are trying to use the indexes of the first element to select other items in the list. Below is a straightforward solution.
list_to_slice = [[1, 2], [2, 2], [2, 3], [3, 3]]
targets = list_to_slice[0]
result = [targets] + [list_to_slice[targets[0]]] + [list_to_slice[targets[1]]]
print(result)
>>> [[1, 2], [2, 2], [2, 3]]
A more general purpose solution
def first_slice(list_to_slice):
targets = list_to_slice[0]
result = [targets]
for target in targets:
result.append(list_to_slice[target])
return result
first_slice([[1, 2], [2, 2], [2, 3], [3, 3]])
>>> [[1, 2], [2, 2], [2, 3]]
assume i have
a=[[0,1],[2,1],[0,2],[1,3]]
i would like to have a result like this
b=[[0,1],[0,2],[1,2],[1,3]]
The tricky part is to sort the [2,1] to [1,2] and sort the whole list again. I tried
sorted(a, key=lambda x: (x[0]))
but it give me [[0,1],[0,2],[1,3],[2,1]]
Any suggestion?
Try to sort the sublist first:
print sorted([sorted(i) for i in a])
Update It could be done slightly better, passing a list comprehension to sorted() results in a copy of the list being made, so you can just remove it:
print sorted(sorted(i) for i in a)
Result
[[0, 1], [0, 2], [1, 2], [1, 3]]
As #PM2Ring said, using the lambda maybe return the wrong result, it should be removed.
Can you try the below using comprehension.
>>> b=sorted([sorted(s) for s in a])
>>> b
[[0, 1], [0, 2], [1, 2], [1, 3]]
It should work.
try this:
b=sorted([sorted(small_list) for small_list in a ])
Try this:
>>> [sorted(i) for i in sorted(a, key=lambda x: (min(x), sum(x)))]
[[0, 1], [0, 2], [1, 2], [1, 3]]
Is there an easy way in python (using itertools, or otherwise) to convert a nested iterable f into its corresponding list or tuple? I'd like to save f so I can iterate over it multiple times, which means that if some nested elements of f are generators, I'll be in trouble.
I'll give an example input/output.
>>> g = iter(range(2))
>>> my_input = [1, [2, 3], ((4), 5), [6, g]]
>>> magical_function(my_input)
[1, [2, 3], [[4], 5], [6, [0, 1]]]
It would be fine if the output consisted of tuples, too. The issue is that iterating over g "consumes" it, so it can't be used again.
This seems like it would be best to do by checking if each element is iterable, and calling a recursive function over it if it is iterable. Just as a quick draw-up, I would try something like:
import collections
g = iter(range(2))
my_input = [1, [2, 3], ((4), 5), [6, g]]
def unfold(iterable):
ret = []
for element in iterable:
if isinstance(element, collections.Iterable):
ret.append(unfold(element))
else:
ret.append(element)
return ret
n = unfold(my_input)
print(n)
print(n)
which returns
$ python3 so.py
[1, [2, 3], [4, 5], [6, [0, 1]]]
[1, [2, 3], [4, 5], [6, [0, 1]]]
It's not the prettiest way, and you can find ways to improve it (it puts everything in a list instead of preserving tuples), but here is the general idea I would use.
I've seen some questions here very related but their answer doesn't work for me. I have a list of lists where some sublists are repeated but their elements may be disordered. For example
g = [[1, 2, 3], [3, 2, 1], [1, 3, 2], [9, 0, 1], [4, 3, 2]]
The output should be, naturally according to my question:
g = [[1,2,3],[9,0,1],[4,3,2]]
I've tried with set but only removes those lists that are equal (I thought It should work because sets are by definition without order). Other questions i had visited only has examples with lists exactly duplicated or repeated like this: Python : How to remove duplicate lists in a list of list?. For now order of output (for list and sublists) is not a problem.
(ab)using side-effects version of a list comp:
seen = set()
[x for x in g if frozenset(x) not in seen and not seen.add(frozenset(x))]
Out[4]: [[1, 2, 3], [9, 0, 1], [4, 3, 2]]
For those (unlike myself) who don't like using side-effects in this manner:
res = []
seen = set()
for x in g:
x_set = frozenset(x)
if x_set not in seen:
res.append(x)
seen.add(x_set)
The reason that you add frozensets to the set is that you can only add hashable objects to a set, and vanilla sets are not hashable.
If you don't care about the order for lists and sublists (and all items in sublists are unique):
result = set(map(frozenset, g))
If a sublist may have duplicates e.g., [1, 2, 1, 3] then you could use tuple(sorted(sublist)) instead of frozenset(sublist) that removes duplicates from a sublist.
If you want to preserve the order of sublists:
def del_dups(seq, key=frozenset):
seen = {}
pos = 0
for item in seq:
if key(item) not in seen:
seen[key(item)] = True
seq[pos] = item
pos += 1
del seq[pos:]
Example:
del_dups(g, key=lambda x: tuple(sorted(x)))
See In Python, what is the fastest algorithm for removing duplicates from a list so that all elements are unique while preserving order?
What about using mentioned by roippi frozenset this way:
>>> g = [list(x) for x in set(frozenset(i) for i in [set(i) for i in g])]
[[0, 9, 1], [1, 2, 3], [2, 3, 4]]
I would convert each element in the list to a frozenset (which is hashable), then create a set out of it to remove duplicates:
>>> g = [[1, 2, 3], [3, 2, 1], [1, 3, 2], [9, 0, 1], [4, 3, 2]]
>>> set(map(frozenset, g))
set([frozenset([0, 9, 1]), frozenset([1, 2, 3]), frozenset([2, 3, 4])])
If you need to convert the elements back to lists:
>>> map(list, set(map(frozenset, g)))
[[0, 9, 1], [1, 2, 3], [2, 3, 4]]
to get right down to it, I'm trying to iterate through a list of coordinate pairs in python and delete all cases where one of the coordinates is negative. For example:
in the array:
map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
I want to remove all the pairs in which either coordinate is < 0, leaving:
map = [[2, 3], [7, 1]]
My problem is that python lists cannot have any gaps, so if I loop like this:
i = 0
for pair in map:
for coord in pair:
if coord < 0:
del map[i]
i += 1
All the indices shift when the element is deleted, messing up the iteration and causing all sorts of problems. I've tried storing the indices of the bad elements in another list and then looping through and deleting those elements, but I have the same problem: once one is gone, the whole list shifts and indices are no longer accurate.
Is there something I'm missing?
Thanks.
If the list is not large, then the easiest way is to create a new list:
In [7]: old_map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
In [8]: new_map=[[x,y] for x,y in a_map if not (x<0 or y<0)]
In [9]: new_map
Out[9]: [[2, 3], [7, 1]]
You can follow this up with old_map = new_map if you want to discard the other pairs.
If the list is so large creating a new list of comparable size is a problem, then you can delete elements from a list in-place -- the trick is to delete them from the tail-end first:
the_map = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
for i in range(len(the_map)-1,-1,-1):
pair=the_map[i]
for coord in pair:
if coord < 0:
del the_map[i]
print(the_map)
yields
[[2, 3], [7, 1]]
PS. map is such a useful built-in Python function. It is best not to name a variable map since this overrides the built-in.
You can use list comprehension for this:
>>> mymap = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
>>> mymap = [m for m in mymap if m[0] > 0 and m[1] > 0]
>>> mymap
[[2, 3], [7, 1]]
If you do not have any other references to the map list, a list comprehension works best:
map = [[a,b] for (a,b) in map if a > 0 and b > 0]
If you do have other references and need to actually remove elements from the list referenced by map, you have to iterate over a copy of map:
for coord in map[:]:
if coord[0] < 0 or coord[1] < 0:
map.remove(coord)
Personally, I prefer in-place modification:
li = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
print li,'\n'
N = len(li)
for i,(a,b) in enumerate(li[::-1], start=1):
if a<0 or b<0:
del li[N-i]
print li
->
[[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
[[2, 3], [7, 1]]
If you wish to do this in place, without creating a new list, simply use a for loop with index running from len(map)-1 down to 0.
for index in range(len(map)-1,-1,-1):
if hasNegativeCoord(map[index]):
del(map[index])
Not very Pythonic, I admit.
If the list is small enough, it's more efficient to make a copy containing just the elements you need, as detailed in the other answers.
However, if the list is too large, or for some other reason you need to remove the elements from the list object in place, I've found the following little helper function quite useful:
def filter_in_place(func, target, invert=False):
"remove all elements of target where func(elem) is false"
pos = len(target)-1
while pos >= 0:
if (not func(target[pos])) ^ invert:
del target[pos]
pos -= 1
In your example, this could be applied as follows:
>>> data = [[-1, 2], [5, -3], [2, 3], [1, -1], [7, 1]]
>>> def is_good(elem):
return elem[0] >= 0 and elem[1] >= 0
>>> filter_in_place(is_good, data)
>>> data
[[2, 3], [7, 1]]
(This is just a list-oriented version of filter_in_place, one which supports all base Python datatypes is a bit more complex).
itertools.ifilter()/ifilterfalse() exist to do exactly this: filter an iterable by a predicate (not in-place, obviously).
Better still, avoid creating and allocating the entire filtered list object if at all possible, just iterate over it:
import itertools
l = [(4,-5), (-8,2), (-2,-3), (4,7)]
# Option 1: create a new filtered list
l_filtered = list( itertools.ifilter(lambda p: p[0]>0 and p[1]>0, l) )
# Option 2:
for p in itertools.ifilter(lambda p: p[0]>0 and p[1]>0, l):
... <subsequent code on your filtered list>
You probably want del pair instead.