As the number of c
Category Archives:
---
abstract: 'In
Ruthie has been wo
The New York State
As we prepare to c
How to Write a Win
Category Archives:
Ceramic materials
This invention relQ:
Python convert a tuple into an unique list
I have a tuple like this:
t=(1, 3, 3, 3, 4, 2, 1)
And I want to convert it into a list like this:
unique_list=[1, 2, 3, 4]
I did this:
t.sort()
print(t)
But this just gave me [1, 2, 3, 3, 3, 4, 1]
I want the results to be sorted before I get unique. So how do I do this?
A:
You can use list slicing as a solution
>>> lst = [1, 3, 3, 3, 4, 2, 1]
>>> sorted(lst[0:-1])
[1, 2, 3, 4]
If you want them as a list of tuples use:
>>> sorted(tuple(lst[0:-1]) )
[(1,), (2,), (3,), (4,)]
The difference is if you do not use tuple:
>>> lst[0:-1]
[1, 3, 3, 3, 4, 2, 1]
>>> lst[0:-1].sort()
[1, 3, 3, 3, 4, 2, 1]
Since the last element is smaller it will be overwritten.
A:
One possible solution is:
def find_first_equal(l):
i = 0
for el in l:
while i < len(l) and l[i] != el:
i += 1
return i
t = [1,3,3,3,4,2,1]
b = sorted(t)
s = []
# print(t)
for i in t:
if i == b[0]:
s.insert(find_first_equal(b), i)
else:
s.insert(find_first_equal(b), b[i])
print(s)
Result:
[1, 2, 3, 4]
>>>
EDIT
Based on your comments, I think this is what you're looking for:
t = [1,3,3,3,4,2,1]
b = sorted(t)
s = [i for i, j in zip(b, b[1:] + b[0:1]) if j != i]
print(s)
Result:
[1, 2, 3, 4]
A:
I see. There is a method provided by python. It's unique function. See the doc here.
>>> import itertools
>>> list(itertools.takewhile(lambda x: x[0] != x[-1], (1, 3, 3, 3, 4, 2, 1)))
[1, 2, 3, 4]
I found it here
A:
>>> s = [(1, 2, 3), (4, 5, 1), (2, 5, 1), (2, 2, 3)]
>>> map(set, map(sorted, s))
[set([1, 2, 3]), set([4, 5, 1]), set([2, 5, 1]), set([2, 2, 3])]
You could use a set comprehension to find the unique values. The map will convert your input to a list of tuples (each tuple containing the index of a list) so that the sorted and set operations will be applied to each tuple before converting back to a list.
>>> [(list(item), list(index)) for item in sorted(s) for index in s]
[[1, 2, 3], [4, 5, 1], [2, 5, 1], [2, 2, 3]]
>>> set(map(tuple, map(sorted, s)))
{(2, 5, 1), (1, 2, 3), (4, 5, 1), (2, 2, 3)}
There's a slight performance difference between this method and the list comprehension. So it's up to you.
>>> from timeit import timeit
>>> s = [(1, 2, 3), (4, 5, 1), (2, 5, 1), (2, 2, 3)]
>>> timeit("set(map(tuple, map(sorted, s)))")
0.013599149905
>>> timeit("[(list(item), list(index)) for item in sorted(s) for index in s]", setup="from __main__ import s")
0.105574124799
This timeit test is only valid for python3.
Based on your question edit:
def find_first_equal(t):
i = 0
for i, el in enumerate(t):
while t[i + 1] != el:
i += 1
return i
print([i for i, j in zip(list(range(1, 4)), find_first_equal(s)) if j != i])
This also works
>>> for i, j in zip(list(range(1, 4)), find_first_equal(s)):
... if j != i:
... print(i, j)
...
1 2
>>>
For the sake of completeness, a non-recursive version.
def nonRecursiveFind(t):
itr = iter(t)
for t in itr:
if t[0] == t[-1]:
yield t
elif t[0] != t[-1]:
yield next(itr)
else:
break
A:
Use list comprehensions, sorting, and zip.
>>> tupleOfThree = (1, 3, 3, 3, 4, 2, 1)
>>> [i for i in sorted(set(map(tuple, zip(map(list, tupleOfThree), map(range, tupleOfThree)))))]
[1, 2, 3, 4]
>>> setOfThree = set(map(tuple, zip(map(list, tupleOfThree), map(range, tupleOfThree))))
>>> [i for i in sorted(setOfThree)]
[1, 2, 3, 4]
The following is what was used to create the list from tuples of lists.
>>> map(list, zip(map(list, tupleOfThree), map(range, tupleOfThree)))
[(1, 3, 3, 3, 4), (2, 5, 1), (2, 2, 3)]
map(list, zip(map(list, tupleOfThree), map(range, tupleOfThree))) converts the three lists of tuples back to a single list. zip turns the list of tuples into a list of lists. Using map(range, tupleOfThree) ensures all of the elements are the same type. So map(list, zip(..., map(range, ...))) is actually doing the conversion back to a list of lists. Using map(..., zip(...)) instead would leave us with a list of tuples, which we want to be converted back to a list.
Finally, we can flatten the list using map(list, zip(...)) so that we have a simple list.
Since zip is used twice, you should look into using the itertools.izip function in python 2.x or the builtin zip function in python 3.x, which are functionally identical.