Combine two lists in to a single tuple in python - python

I have 2 lists
A=[1,2,3,4]
B=[5,6,7,8]
My expected Tuple should look like
C=(1,2,3,4,5,6,7,8)
How can I combine these lists into my desired output?

It's as simple as:
tuple(A + B)
Which results in:
>>> A = [1, 2, 3, 4]
>>> B = [5, 6, 7, 8]
>>> print(tuple(A + B))
(1, 2, 3, 4, 5, 6, 7, 8)
It wasn't as hard as you thought, was it ? Just have a look at the docs before asking here.

Related

Relative size in list python

I have a list of integers. Each number can appear several times, the list is unordered.
I want to get the list of relative sizes. Meaning, if for example the original list is [2, 5, 7, 7, 3, 10] then the desired output is [0, 2, 3, 3, 1, 4]
Because 2 is the zero'th smallest number in the original list, 3 is one'th, etc.
Any clear easy way to do this?
Try a list comprehension with dictionary and also use set for getting unique values, like below:
>>> lst = [2, 5, 7, 7, 3, 10]
>>> newl = dict(zip(range(len(set(lst))), sorted(set(lst))))
>>> [newl[i] for i in lst]
[0, 2, 3, 3, 1, 4]
>>>
Or use index:
>>> lst = [2, 5, 7, 7, 3, 10]
>>> newl = sorted(set(lst))
>>> [newl.index(i) for i in lst]
[0, 2, 3, 3, 1, 4]
>>>

Take elements from multiple lists

Given multiple lists like the ones shown:
a = [1, 2, 3]
b = [5, 6, 7, 8]
c = [9, 0, 1]
d = [2, 3, 4, 5, 6, 7]
...
I want to be able to combine them to take as many elements from the first list as I can before starting to take elements from the second list, so the result would be:
result = [1, 2, 3, 8, 6, 7]
Is there a particularly nice way to write this? I can't think of a really simple one without a for loop. Maybe a list comprehension with a clever zip.
Simple slicing and concatenation:
a + b[len(a):]
Or with more lists:
res = []
for lst in (a, b, c, d):
res += lst[len(res):]
# [1, 2, 3, 8, 6, 7]
With itertools.zip_longest() for Python 3, works on any number of input lists:
>>> from itertools import zip_longest
>>> [next(x for x in t if x is not None) for t in zip_longest(a,b,c,d)]
[1, 2, 3, 8, 6, 7]
The default fill value is None so take the first none None element in each tuple created with the zip_longest call (you can change the defaults and criteria if None is a valid data value)
With functools.reduce:
from functools import reduce
print(list(reduce(lambda a, b: a + b[len(a):], [a, b, c, d])))
This outputs:
[1, 2, 3, 8, 6, 7]

Is there a way to append/extend a list with another list at a specific index in python?

In other words, I want to accomplish something like the following:
a = [1, 2, 3, 7, 8]
b = [4, 5, 6]
# some magic here to insert list b into list a at index 3 so that
a = [1, 2, 3, 4, 5, 6, 7, 8]
You can assign to a slice of list a like so:
>>> a = [1, 2, 3, 7, 8]
>>> b = [4, 5, 6]
>>> a[3:3] = b
>>> a
[1, 2, 3, 4, 5, 6, 7, 8]
>>>
The [3:3] may look weird, but it is necessary to have the items in list b be inserted into list a correctly. Otherwise, if we were to assign to an index of a, list b itself would be inserted:
>>> a = [1, 2, 3, 7, 8]
>>> b = [4, 5, 6]
>>> a[3] = b
>>> a
[1, 2, 3, [4, 5, 6], 8]
>>>
I tried to find a docs link which explicitly mentions this behavior, but was unsuccessful (please feel free to add one if you can find it). So, I'll just say that doing a[3:3] = b tells Python to take the items in list b and place them in the section of list a represented by [3:3]. Moreover, Python will extend list a as necessary to accommodate this operation.
In short, this is just yet another awesome feature of Python. :)
Try using the sort method as follows:
>>> a = [1,2,3,7,8]
>>> b = [4,5,6]
>>> a = sorted(a+b)
>>> a
[1, 2, 3, 4, 5, 6, 7, 8]

Select unique value in many lists in python

I'd like to select unique value in many lists, but I don't know how to do that:
a = [1,2,3,4,5]
b = [2,3,4,5,6]
c = [5,6,7,8,9]
I'd like to make one new list which is [1,2,3,4,5,6,7,8,9]
I know that using the following could be done, but I am searching quicker way to do that.
for i in (a, b, c):
for j in EachValueInEachList:
NewList.append(j)
list(set(NewList)
By the way, there are thousand of lists in my real program.
Thank you very much.
>>> a = [1,2,3,4,5]
>>> b = [2,3,4,5,6]
>>> c = [5,6,7,8,9]
>>> list(set(a + b + c))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
To avoid creating temporary list, use itertools.chain:
>>> import itertools
>>> list(set(itertools.chain(a, b, c)))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
UPDATE (answer to the comment)
If you have a list of lists, use itertools.chain.from_iterable:
list(set(itertools.chain.from_iterable(a_list_of_lists)))
>>> list(set(a) | set(b) | set(c))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
numpy is best option if long list.
import numpy as np
import time
start_time = time.time()
a = np.array([[1,2,3,4,5], [2,3,4,5,6],[5,6,7,8,9]])
print np.unique(a).tolist()
print time.time() - start_time # Execution time
Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9]
0.000999927520752

Sort list values to get a new order of its index, Python way

Sorry for the vague of my question's title.
My question is, I have a list a = [6, 9, 8, 10, 7, 5, 2, 3, 1, 4]
I need to get the new order b = [4, 2, 3, 5, 1, 6, 10, 8, 7, 9], where the first element of b is 4 because the 4th element of a 10 is the largest number in a. Similarly, the 2nd element in b is 2 because the second large number in a is its second number 9
So, hopefully you got my question: Sort the list a and get the new order b.
Currently, I get it done by using list.sort with some prepare.
tmp = zip(range(1,11), a)
tmp.sort(key=lambda x:(-x[1],x[0]))
b = [x[0] for x in tmp]
I wonder whether there are better python way to achieve my goal?
Thanks for any suggestions~
I would just use the key argument to sort range(1, len(a) + 1) by using a's values.
sorted(range(1, len(a) + 1), key=lambda i: a[i-1], reverse=True)
That's basically the idea, but you can do:
import operator
tmp = sorted(enumerate(a,1),key=itemgetter(1,0),reverse=True)
b = [x[0] for x in tmp]
#In python2.x, the following are equivalent to the list comprehension.
#b = zip(*tmp)[0]
#b = map(itemgetter(0),tmp)
I think that enumerate is a little cleaner than zip with range and itemgetter is a little cleaner than lambda.
You could use sorted and enumerate:
print [el[0] for el in sorted(enumerate(a, start=1), key=lambda L: L[1], reverse=True)]
# [4, 2, 3, 5, 1, 6, 10, 8, 7, 9]
For completeness an alternative using numpy (should you happen to use it any time in the near future):
np.argsort(a)[::-1] + 1
a = [6, 9, 8, 10, 7, 5, 2, 3, 1, 4]
b = [6, 9, 8, 10, 7, 5, 2, 3, 1, 4]
a.sort(reverse = True)
print(a)
print(b)
c = [b.index(y)+1 for y in a ]
print(c)
i have just got this stupid answers...

Categories