can we remove a entry from the sets.
what are the commands to use for it?
like, my set conatins (6,5) , (6,7), (7,9)...I need to remove the second entry...what shud I do??
my_set.remove((6, 7))
The remove() method can be used to remove items from a set.
You can use remove():
>>> s = set([1, 2, 3])
>>> s.remove(2)
>>> s
set([1, 3])
a -= set([(6, 7)])
:-)
If you've got a list of those tuples you can clear it out like this:
l = [(1,2),(3,4),(5,6)]
[(1, 2), (3, 4), (5, 6)]
l.remove((3,4))
[(1, 2), (5, 6)]
Related
els = [1, 2, 3, 4, ]
print([(v, els[idx + 1]) for idx, v in enumerate(els[::2])])
Why does Python output [(1, 2), (3, 3)] instead of [(1, 2), (3, 4)]?
PS: I know I could do this: [(els[i], els[i + 1]) for i in range(0,len(els),2)] I'm not asking for a solution, I'm asking why is this?
why is this?
Take closer look at enumerate(els[::2]), consider following simple code
els = [1,2,3,4,5,6,7,8,9]
for elem in enumerate(els[::2]):
print(elem)
gives output
(0, 1)
(1, 3)
(2, 5)
(3, 7)
(4, 9)
Observe that numbering was applied after slicing but you are using index to access element of list before slicing and they are not in unison.
So, what I am trying to do, is to sort a list with that contains (num, tuple) I want to sort it first by the second value of the tuple and if 2 are equal, I want to sort it by the num(of the first tuple).
So lets say I have:
l = [(1,(2,3)),(3,(2,1)),(2,(2,1))]
print(l.sort(key=something))
[(2,(2,1)), (3,(2,1)), (1,(2,3))]
I have tried:
l.sort(key=itemgetter(1,0)
Of course it didn't work. Any ideas?
Thank you.
operator.itemgetter works fine:
>>> from operator import itemgetter
>>> l = [(1,(2,3)),(3,(2,1)),(2,(2,1))]
>>> l.sort(key=itemgetter(1,0))
>>> print(l)
[(2, (2, 1)), (3, (2, 1)), (1, (2, 3))]
>>>
I think the problem is that you tried to print the result of list.sort. You cannot do this because it is an in-place method (it always returns None).
>>> l = [(1,(2,3)),(3,(2,1)),(2,(2,1))]
>>> sorted(l, key=lambda t: (t[1][1],t[0]))
[(2, (2, 1)), (3, (2, 1)), (1, (2, 3))]
Or:
>>> from operator import itemgetter
>>> sorted(l, key=itemgetter(1,0))
[(2, (2, 1)), (3, (2, 1)), (1, (2, 3))]
this works too:
l.sort(key=lambda x: x[::-1])
Note that list.sort sorts the list in place, so print(l.sort(key=something)) would print None.
I want to take a list, for instance List = [1,2,2], and generate its permutations. I can do this with:
NewList = [list(itertools.permutations(List))]
and the output is:
[[(1, 2, 2), (1, 2, 2), (2, 1, 2), (2, 2, 1), (2, 1, 2), (2, 2, 1)]]
The Problem: itertools.permutations returns a list of length 1 whose only entry is the list of all permutations of List. That is:
NewList[0] == [(1,2,2),(1,2,2),(2,1,2),(2,2,1),(2,1,2),(2,2,1)]
and
NewList[1] does not exist.
I want the output to be a list where each entry is one of the permutations. That is
NewList[0] == (1,2,2)
NewList[1] == (1,2,2)
NewList[2] == (2,1,2)
...
NewList[5] == (2,2,1)
The Question: Is there a command that will produce the permutations of List in this way? Failing that, is there a way to access the 'individual elements' of [list(itertools.permutations(List))] so I can do things with them?
>>> from itertools import permutations
>>> list(permutations([1,2,2]))
[(1, 2, 2), (1, 2, 2), (2, 1, 2), (2, 2, 1), (2, 1, 2), (2, 2, 1)]
You don't need to put it in a list again. i.e Don't do [list(permutations(...))] (By doing [] you are making a nested list and hence are unable to access the elements using testList[index], though you could do testList[0][index] but it would be better to just keep it as a list of tuples.)
>>> newList = list(permutations([1, 2, 2]))
>>> newList[0]
(1, 2, 2)
>>> newList[3]
(2, 2, 1)
Now you can access the elements by using their indices.
Why can't you do this:
NewList = [list(itertools.permutations(List))][0]
or even just this:
NewList = list(itertools.permutations(List))
By doing [ list(itertools.permutations(List)) ], you put the list of permutations (the one that you want) inside of another list. So the fix would be to remove the outer []'s
I have the following list in Python:
[('1','2','3'),('5','6','7')]
I need to convert the tuples inside the list into integer([(1,2,3),(5,6,7)]) in a functional way.
I can do them for a list using this simple code: map(lambda x:int(x),['1','2','3'])
But how shall i apply the same concept for list of tuples ?
(I know the imperative way of doing this.)
tl = [('1','2','3'),('5','6','7')]
[tuple(int(x) for x in t) for t in tl]
# [(1, 2, 3), (5, 6, 7)]
If you really want the map syntax,
map(lambda t:tuple(map(int, t)), tl)
# [(1, 2, 3), (5, 6, 7)]
How about the following:
[tuple([int(str_int) for str_int in tup]) for tup in list_of_string_tuples]
This hybrid works:
>>> [tuple(map(int,t)) for t in [('1','2','3'),('5','6','7')]]
[(1, 2, 3), (5, 6, 7)]
Is there a python builtin that does the same as tupler for a set of lists, or something similar:
def tupler(arg1, *args):
length = min([len(arg1)]+[len(x) for x in args])
out = []
for i in range(length):
out.append(tuple([x[i] for x in [arg1]+args]))
return out
so, for example:
tupler([1,2,3,4],[5,6,7])
returns:
[(1,5),(2,6),(3,7)]
or perhaps there is proper pythony way of doing this, or is there a generator similar???
I think you're looking for zip():
>>> zip([1,2,3,4],[5,6,7])
[(1, 5), (2, 6), (3, 7)]
have a look at the built-in zip function http://docs.python.org/library/functions.html#zip
it can also handle more than two lists, say n, and then creates n-tuples.
>>> zip([1,2,3,4], [5,6,7,8], [9,10,11,12], [13,14])
[(1, 5, 9, 13), (2, 6, 10, 14)]
zip([1,2,3,4],[5,6,7])
--->[(1,5),(2,6),(3,7)]
args = [(1,5),(2,6),(3,7)]
zip(*args)
--->[1,2,3],[5,6,7]
The proper way is to use the zip function.
Alternativerly we can use list comprehensions and the built-in enumerate function
to achieve the same result.
>>> L1 = [1,2,3,4]
>>> L2 = [5,6,7]
>>> [(value, L2[i]) for i, value in enumerate(L1) if i < len(L2)]
[(1, 5), (2, 6), (3, 7)]
>>>
The drawback in the above example is that we don't always iterate over the list with the minimum length.