Tuple list comprehension - python-3.x

I have a list of tuples containing numbers
list_numbers = [(1, 6), (2, 7), (3, 8), (4, 9), (5, 10)]
How do I use list comprehension to get a list of the sum of each item in the tuple?
expected_result = [7, 9, 11, 13, 15]

You can just loop through the list and call the sum() function on each tuple.
sums = [sum(t) for t in list_numbers]
> [7, 9, 11, 13, 15]

Related

Find integer consecutive run starting indices in a list

Example:
nums = [1,2,3,5,10,9,8,9,10,11,7,8,7]
I am trying to find the first index of numbers in consecutive runs of -1 or 1 direction where the runs are >= 3.
So the desired output from the above nums would be:
[0,4,6,7]
I have tried
grplist = [list(group) for group in more_itertools.consecutive_groups(A)]
output: [[1, 2, 3], [5], [10], [9], [8, 9, 10, 11], [7, 8], [7]]
It returns nested lists but does not but that only seems to go in +1 direction. And it does not return the starting index.
listindx = [list(j) for i, j in groupby(enumerate(A), key=itemgetter(1))]
output: [[(0, 1)], [(1, 2)], [(2, 3)], [(3, 5)], [(4, 10)], [(5, 9)], [(6, 8)], [(7, 9)], [(8, 10)], [(9, 11)], [(10, 7)], [(11, 8)], [(12, 7)]]
This does not check for consecutive runs but it does return indices.

Find all combinations of a list in python

The list that I have:
a = [1,2,3]
The output that I want:
combinations = [11, 12, 13, 21, 22, 23, 31, 32, 33]
I have tried:
a = [1,2,3]
all_combinations = []
list1_permutations = itertools.permutations(a, len(a))
for each_permutation in list1_permutations:
zipped = zip(each_permutation, a)
all_combinations.append(list(zipped))
print(all_combinations)
But I am getting the output like:
[[(1, 1), (2, 2), (3, 3)], [(1, 1), (3, 2), (2, 3)], [(2, 1), (1, 2), (3, 3)], [(2, 1), (3, 2), (1, 3)], [(3, 1), (1, 2), (2, 3)], [(3, 1), (2, 2), (1, 3)]]
This might be easiest with a nested list comprehension:
a = [1, 2, 3]
out = [int(f'{i}{j}') for i in a for j in a]
print(out)
Output:
[11, 12, 13, 21, 22, 23, 31, 32, 33]
The same result can be achieved (perhaps more efficiently) with itertools.product:
import itertools
a = [1, 2, 3]
out = [int(f"{a}{b}") for a, b in itertools.product(a, a)]
This should work.
you can use a list comprehension to make all combos because you appear to want to sample with duplicates like '33'
you can use a list gen to do this
you need to treat the items like strings to join them
you need to convert it back to integer if that is what you want as final result
a=[1,2,3]
result = [int(''.join([str(i), str(j)])) for i in a for j in a]
print(result)

Numpy unable to access columns

I'm working on a ML project for which I'm using numpy arrays instead of pandas for faster computation.
When I intend to bootstrap, I wish to subset the columns from a numpy ndarray.
My numpy array looks like this:
np_arr =
[(187., 14.45 , 20.22, 94.49)
(284., 10.44 , 15.46, 66.62)
(415., 11.13 , 22.44, 71.49)]
And I want to index columns 1,3.
I have my columns stored in a list as ix = [1,3]
However, when I try to do np_arr[:,ix] I get an error saying too many indices for array .
I also realised that when I print np_arr.shape I only get (3,), whereas I probably want (3,4).
Could you please tell me how to fix my issue.
Thanks!
Edit:
I'm creating my numpy object from my pandas dataframe like this:
def _to_numpy(self, data):
v = data.reset_index()
np_res = np.rec.fromrecords(v, names=v.columns.tolist())
return(np_res)
The reason here for your issue is that the np_arr which you have is a 1-D array. Share your code snippet as well so that it can be looked into as in what is the exact issue. But in general, while dealing with 2-D numpy arrays, we generally do this.
a = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
You have created a record array (also called a structured array). The result is a 1d array with named columns (fields).
To illustrate:
In [426]: df = pd.DataFrame(np.arange(12).reshape(4,3), columns=['A','B','C'])
In [427]: df
Out[427]:
A B C
0 0 1 2
1 3 4 5
2 6 7 8
3 9 10 11
In [428]: arr = df.to_records()
In [429]: arr
Out[429]:
rec.array([(0, 0, 1, 2), (1, 3, 4, 5), (2, 6, 7, 8), (3, 9, 10, 11)],
dtype=[('index', '<i8'), ('A', '<i8'), ('B', '<i8'), ('C', '<i8')])
In [430]: arr['A']
Out[430]: array([0, 3, 6, 9])
In [431]: arr.shape
Out[431]: (4,)
I believe to_records has a parameter to eliminate the index field.
Or with your method:
In [432]:
In [432]: arr = np.rec.fromrecords(df, names=df.columns.tolist())
In [433]: arr
Out[433]:
rec.array([(0, 1, 2), (3, 4, 5), (6, 7, 8), (9, 10, 11)],
dtype=[('A', '<i8'), ('B', '<i8'), ('C', '<i8')])
In [434]: arr['A'] # arr.A also works
Out[434]: array([0, 3, 6, 9])
In [435]: arr.shape
Out[435]: (4,)
And multifield access:
In [436]: arr[['A','C']]
Out[436]:
rec.array([(0, 2), (3, 5), (6, 8), (9, 11)],
dtype={'names':['A','C'], 'formats':['<i8','<i8'], 'offsets':[0,16], 'itemsize':24})
Note that the str display of this array
In [437]: print(arr)
[(0, 1, 2) (3, 4, 5) (6, 7, 8) (9, 10, 11)]
shows a list of tuples, just as your np_arr. Each tuple is a 'record'. The repr display shows the dtype as well.
You can't have it both ways, either access columns by name, or make a regular numpy array and access columns by number. The named/record access makes most sense when columns are a mix of dtypes - string, int, float. If they are all float, and you want to do calculations across columns, its better to use the numeric dtype.
In [438]: arr = df.to_numpy()
In [439]: arr
Out[439]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])

get multiple tuples from list of tuples using min function

I have a list that looks like this
mylist = [('Part1', 5, 5), ('Part2', 7, 7), ('Part3', 11, 9),
('Part4', 45, 45), ('part5', 5, 5)]
I am looking for all the tuples that has a number closest to my input
now i am using this code
result = min([x for x in mylist if x[1] >= 4 and x[2] >= 4])
The result i am getting is
('part5', 5, 5)
But i am looking for an result looking more like
[('Part1', 5, 5), ('part5', 5, 5)]
and if there are more tuples in it ( i have 2 in this example but it could be more) then i would like to get all the tuples back
the whole code
mylist = [('Part1', 5, 5), ('Part2', 7, 7), ('Part3', 11, 9), ('Part4', 45, 45), ('part5', 5, 5)]
result = min([x for x in mylist if x[1] >= 4 and x[2] >= 4])
print(result)
threshold = 4
mylist = [('Part1', 5, 5), ('Part2', 7, 7), ('Part3', 11, 9), ('Part4', 45, 45), ('part5', 5, 5)]
filtered = [x for x in mylist if x[1] >= threshold and x[2] >= threshold]
keyfunc = lambda x: x[1]
my_min = keyfunc(min(filtered, key=keyfunc))
result = [v for v in filtered if keyfunc(v)==my_min]
# [('Part1', 5, 5), ('part5', 5, 5)]

print list content without duplicate [duplicate]

This question already has answers here:
How can I print the possible combinations of a list in Python?
(3 answers)
Closed 3 years ago.
I have a list containing some contents like this:
numbers = [5,7,9,3,8]
I want to print consecutive elements starting with the first element and see output like this:
5,7
5,9
5,3
5,8
7,9
7,3
7,8
9,3
9,8
3,8
so, in the end, it will print elements without duplicating for any two elements
I have tried this
for e in numbers:
print(numbers[:])
but it gave me
[5, 7, 9, 3, 8]
[5, 7, 9, 3, 8]
[5, 7, 9, 3, 8]
[5, 7, 9, 3, 8]
[5, 7, 9, 3, 8]
so how to solve this
Thank you
You can use combinations from itertools.
>>> from itertools import combinations
>>> numbers = [5, 7, 9, 3, 8]
>>> list(combinations(numbers, 2))
[(5, 7), (5, 9), (5, 3), (5, 8), (7, 9), (7, 3), (7, 8), (9, 3), (9, 8), (3, 8)]
You can use itertools.combinations
from itertools import combinations
numbers = [5,7,9,3,8]
combos = combinations(numbers, 2)
for combo in combos:
print(combo)
(5, 7)
(5, 9)
(5, 3)
(5, 8)
(7, 9)
(7, 3)
(7, 8)
(9, 3)
(9, 8)
(3, 8)

Resources