Combine lists into a meshgrid parameter - python-3.x

I'm using this function that works well to give me a numpy array of all possible combinations of arrays
arrs = np.array(np.meshgrid([1,2,3], [4,5,6], [7,8,9])).T.reshape(-1,3)
But what I would like to do is take the 3 lists and create the meshgrid parameter like this:
lst1=[1,2,3]
lst2=[4,5,6]
lst3=[7,8,9]
arrs = np.array(np.meshgrid(lst)).T.reshape(-1,3)
But how can I create lst from the 3 lists? If I do this
lst=[lst1,lst2,lst3]
all I get is an error. Thank you.

You need to unpack the lst:
# notice the *
arrs = np.array(np.meshgrid(*lst)).T.reshape(-1,3)

Related

How can I subtract two elements (type float) in a row using map and a lambda function

I have this curiosity that im trying to kill but i dont know how.
Im trying to subtract to two elements in a row in a list with the type float using map and a lambda function.
li = [12.12,14.11,43.32]
Im doing this but it appears type float not subscriptable.
x = map(lambda y: y[1] - y[0], li)
You can't without doing preprocessing of li (without outright abusing map just for the sake of using it). map is meant to operate on a single element at a time. To use it, you need to make sure that that single element has all the data you need to produce a new element.
Zipping the list with a shifted version of itself works to achieve this:
li = [12.12,14.11,43.32]
shifted = iter(li)
next(shifted) # Drop the first element
neighbors = zip(li, shifted)
map(lambda pair: pair[0] - pair[1], neighbors)
Although, lambda isn't necessary here:
from operator import sub
from itertools import starmap
li = [12.12,14.11,43.32]
shifted = iter(li)
next(shifted)
neighbors = zip(li, shifted)
starmap(sub, neighbors)
starmap and sub make this far cleaner

How a Python code to store integer in list and then find the sum of integer stored in the List

List of integer value passed through input function and then stored in a list. After which performing the operation to find the sum of all the numbers in the list
lst = list( input("Enter the list of items :") )
sum_element = 0
for i in lst:
sum_element = sum_element+int(i)
print(sum_element)
Say you want to create a list with 8 elements. By writing list(8) you do not create a list with 8 elements, instead you create the list that has the number 8 as it's only element. So you just get [8].
list() is not a Constructor (like what you might expect from other languages) but rather a 'Converter'. And list('382') will convert this string to the following list: ['3','8','2'].
So to get the input list you might want to do something like this:
my_list = []
for i in range(int(input('Length: '))):
my_list.append(int(input(f'Element {i}: ')))
and then continue with your code for summation.
A more pythonic way would be
my_list = [int(input(f'Element {i}: '))
for i in range(int(input('Length: ')))]
For adding all the elements up you could use the inbuilt sum() function:
my_list_sum = sum(my_list)
lst=map(int,input("Enter the elements with space between them: ").split())
print(sum(lst))

How to get value from numpy array with dynamic steps?

So i have numpy array like this
x = [1,2,3,4,5]
if i want to get this
x = [1,2,4]
how to produce this result?
I understand the i:j:k syntax but it only have one step but what i want to achieve is dynamic step

How could I generate a combinatorial of the lines of an array with several columns?

I want to get all possible combinations of n lines of an array.
I don't care what order they are, so it's not a permutacion.
Example:
I have an array:
[(1,2,3), (4,5,6), (7,8,9)]
Question:
I want to find all combinations of two lines:
[(1,2,3), (4,5,6)]
[(1,2,3), (7,8,9)]
…
Thank you so much!!
Use itertools.combinations and cast the combinations to a list, see the docs:
from itertools import combinations
lst = [(1,2,3), (4,5,6), (7,8,9)]
for x in combinations(lst, 2):
print(list(x))
Try this in combination with itertools
import itertools
test = np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])
for i in range(0, len(test)):
for j in sorted(np.arange(len(test)-1,i,-1)):
print(test[i])
print(test[j])
print(list(itertools.product(test[i], test[j])))

Getting a list item with the max evaluation in a list of tuples in Python

Given this list of tuples:
my_tuples = [(1,2), (3,4)]
and the following evaluation function:
def evaluate(item_tuple):
return item_tuple[0] * 2
Question: how can I get the list item (tuple) that has the highest evaluation value? (I'm guessing I can use a list comprehension for this)
def max_item(tuples_list, evaluation_fn):
'''Should return the tuple that scores max using evaluation_fn'''
# TODO Implement
# This should pass
assertEqual((3,4), max_item(my_tuples, evaluate))
Correct me if I'm wrong, you want the list of tuples sorted by the result of multiplying one of the values inside the tuple with x (in your example above it would be the first value of the tuple multiplied by 2).
If so, you can do it this way:
from operator import itemgetter
sorted(l, key=itemgetter(0 * 2), reverse=True)
I managed to do it this way:
def max_item(tuples_list, evaluation_fn):
zipped = zip(map(evaluation_fn, tuples_list), tuples_list)
return max(zipped, key=lambda i:i[0])[1]
I don't know if there's a simpler (more pythonic?) way to solve it though.
Edit
I figured how I could use a list comprehension to make it more succinct/readable:
def max_item(tuples_list, evaluation_fn):
return max([(evaluation_fn(i), i) for i in tuples_list])[1]

Resources