I have the following three lists of unequal lengths:
a = [2.13, 5.48,-0.58]
b = [4.17, 1.12, 2.13, 3.48,-1.01,-1.17]
c = [6.73, 8, 12]
d = [(2.13,2.13),(5.48,-1.17),(-0.58,4.17)]
e = [(4.17,12),(2.13,6.73)]
I need to create a combination_abc = [ (x,y,z) for x in a
for y in b
for z in c] such that (x,y) is not equal to d and (y,z) is not equal to e
If I understood you correct, just add if-statement into your list comprehension:
[(x, y, z) for x in a for y in b for z in c if (x, y) not in d and (y, z) not in e]
Also you can use itertools.product for simplicity:
from itertools import product
[(x, y, z) for x, y, z in product(a, b, c) if (x, y) not in d and (y, z) not in e]
Related
I want a list of 4X4 matrices that follow this form:
a # An array of shape [n,3]
[[cos(a[0]),-sin(a[0]),0,a[1]],
[sin(a[0]),cos(a[0]),0,a[2]],
[0,0,1,0],
[0,0,0,1]])
How about:
import numpy as np
n = 1000000
a = np.random.rand(n, 3)
c = np.cos(a[:, 0])
s = np.sin(a[:, 0])
dx = a[:, 1]
dy = a[:, 2]
z = np.zeros(n)
o = np.ones(n)
out = np.array([
[c, -s, z, dx],
[s, c, z, dy],
[z, z, o, z],
[z, z, z, o],
]).transpose(2, 0, 1)
I created a function with 3 parameters input: x y z. I want to loop over them.
x is a dataframe with one column
y same
z asks for a dataframe with multiple columns
I tried this:
result = [f(x,y,z) for x,y,z in zip(df1["1com"], df2["1com"], df3["3com"])]
Df 1,2,3 have the same index length.
This doensnt work because the method list comp doesn't allow for multiple columns like this. I tried a bunch of things with out succes.
btw I found the list comprehension method here: How to iterate over rows in a DataFrame in Pandas
You could zip with individual columns of the multi-column DataFrame:
import pandas as pd
df1 = pd.DataFrame({"col_1": [1, 2, 3]})
df2 = pd.DataFrame({"col_1": [4, 5, 6]})
df3 = pd.DataFrame({"col_1": [7, 8, 9], "col_2": [10, 11, 12]})
def f(w, x, y, z):
return sum([w, x, y, z])
result = [
f(w, x, y, z)
for w, x, y, z
in zip(
df1["col_1"], df2["col_1"],
df3["col_1"], df3["col_2"] # list all required df3 columns individually
)
]
print(result)
Output:
[22, 26, 30]
Or you could join the DataFrames into a single one first:
df = df1.join(df2, lsuffix="_df1").join(df3, lsuffix="_df2")
print(df)
result = [
f(w, x, y, z)
for idx, (w, x, y, z)
in df.iterrows()
]
print(result)
Output:
col_1_df1 col_1_df2 col_1 col_2
0 1 4 7 10
1 2 5 8 11
2 3 6 9 12
[22, 26, 30]
Or you could convert df3 to a list of Series and "pivot" it using zip like below.
def f(x, y, z):
return x, y, z
result = [
f(x, y, z)
for x, y, z
in zip(
df1["col_1"],
df2["col_1"],
zip(*[df3[c] for c in df3.columns]))
]
print(result)
Output:
[(1, 4, (7, 10)), (2, 5, (8, 11)), (3, 6, (9, 12))]
I have two tensors, x and y, of shape [B, D]. I want to do something like the following code
B, D = x.shape
x = tf.expand_dims(x, 1) # [B, 1, D]
y = tf.expand_dims(y, -1) # [B, D, 1]
z = x * y # [B, D, D]
z = tf.reshape(z, (B, D**2))
Is there a function in Tensorflow that already does this?
I want to write a quick calculation in python, not sure how to approach this:
values = [(0, 4, .3), (6, 2, 3), (3, 7, 2.2), (5, 5, .5), (3, 5, .8), (7, 7, .3)]
say we use (x, y, z) to represent tuples in values
criteria = [(a, b, c, d)]
if (a <= x < b) and (c <= y < d),
then sum up all the z in the values.
My result is just to show the sum of z that satisfy the above criteria
You could use list comprehension to achieve that in one line.
# Criteria
(a, b, c, d) = (-1, 10, 0, 10)
# Output
zs = [z for (x, y, z) in values if a <= x < b and c <= y < d]
print(sum(zs))
Not fully sure why you specified the criteria as an array, but I think that example is easy to extend in case you want to check for multiple criterias.
I have an expression with several variables, let's say something like below:
import numpy as np
import sympy as sym
from sympy import Symbol, Add, re, lambdify
x = sym.Symbol('x')
y = sym.Symbol('y')
z = sym.Symbol('z')
F = x+ y +z
I have three lists for the variables like below:
x = [3, 2 ,3]
y = [4, 5 , 6]
z = [7, 10 ,3]
I want to evaluate my function for the each element of my variables.
I know I can define something like below:
f_dis = lambdify([x, y, z], x + y + z, 'numpy')
d = f_dis(3, 4, 7)
print ( "f_dis =", d)
which give me 14 as the desired result. But how can I pass the x, y, and z as three lists (instead of writing the elements separately) and get a result like below:
[14, 17, 12]
It seems using lambdify is a more efficient way to evaluate a function, based on this note:
https://www.sympy.org/scipy-2017-codegen-tutorial/notebooks/22-lambdify.html
Thanks.
import sympy as sp
x = sp.Symbol('x')
y = sp.Symbol('y')
z = sp.Symbol('z')
X = [3, 2 ,3]
Y = [4, 5 , 6]
Z = [7, 10 ,3]
values = list(zip(X, Y, Z))
f_dis = sp.lambdify([x, y, z], x + y + z, 'numpy')
ans = [f_dis(*value) for value in values]
for d in ans:
print ( "f_dis =", d)
this will give you:
f_dis = 14
f_dis = 17
f_dis = 12