乘以 (X, Y) 组合的列表
Multiplying a list of (X, Y) combinations
我正在尝试乘以我生成的组合并从中创建一个新列表。
这是我目前的情况:
import pandas as pd
import itertools
data1 = [[0.1],[0.2],[0.3],[0.5]]
df = pd.DataFrame(data1, columns = ['weights'])
for x in combinations(df['weights'], 2):
print(x)
>>>
(0.1, 0.2)
(0.1, 0.3)
(0.1, 0.5)
(0.2, 0.3)
(0.2, 0.5)
(0.3, 0.5)
##I want to make a new list that shows the product of each combinations,
## example: for every (x,y) combo, do x*y and make a new list called z
预期输出应产生一个新列表:
0.02
0.03
0.05
0.06
0.1
0.15
你可以通过简单的列表理解来完成,你不必使用 itertools
:
inlist = [(0.1, 0.2),
(0.1, 0.3),
(0.1, 0.5),
(0.2, 0.3),
(0.2, 0.5),
(0.3, 0.5)]
z = [round(elem[0]*elem[1],2) for elem in inlist]
print(z)
输出:
[0.02, 0.03, 0.05, 0.06, 0.1, 0.15]
但是,如果你想使用 itertools
,你可以使用 starmap
函数来实现:
from itertools import starmap
inlist = [(0.1, 0.2),
(0.1, 0.3),
(0.1, 0.5),
(0.2, 0.3),
(0.2, 0.5),
(0.3, 0.5)]
z = list(starmap(lambda i,j: round(i*j,2), inlist))
print(z)
输出:
[0.02, 0.03, 0.05, 0.06, 0.1, 0.15]
您可以使用任何可迭代对象来进行这些组合。
如果您是 Python 的新手,此答案试图阐明一些选项。
给定
一个reducing函数mul
和一个可迭代的数据:
def mul(a, b): # 1
"""Return a rounded multiple."""
return round(a * b, 2)
data = [0.1, 0.2, 0.3, 0.5] # 2
代码
准备一个可迭代的——一个可以循环的线性容器。示例:
选项 1 - 迭代器
iterable = it.combinations(data, 2)
选项 2 - pandas DataFrame
df = pd.DataFrame(data, columns=["weights"])
iterable = it.combinations(df["weights"], 2)
演示
[mul(x, y) for x, y in iterable] # 3
# [0.02, 0.03, 0.05, 0.06, 0.1, 0.15]
详情
- 此函数包含您要应用的操作。
- 一个简单的可迭代数据(例如
list
)。注意,不需要嵌套。
- 使用任意迭代来减少组合对
选项 3 - pandas 操作
或者,一旦您在 pandas 中有数据,您可以坚持使用它:
combs = list(it.combinations(data, 2))
df = pd.DataFrame(combs, columns=["a", "b"])
df
# a b
# 0 0.1 0.2
# 1 0.1 0.3
# 2 0.1 0.5
# 3 0.2 0.3
# 4 0.2 0.5
# 5 0.3 0.5
df.prod(axis=1).round(2).tolist()
# [0.02, 0.03, 0.05, 0.06, 0.1, 0.15]
我建议选择纯 Python 或 pandas
(选项 1 或 3)。
我正在尝试乘以我生成的组合并从中创建一个新列表。
这是我目前的情况:
import pandas as pd
import itertools
data1 = [[0.1],[0.2],[0.3],[0.5]]
df = pd.DataFrame(data1, columns = ['weights'])
for x in combinations(df['weights'], 2):
print(x)
>>>
(0.1, 0.2)
(0.1, 0.3)
(0.1, 0.5)
(0.2, 0.3)
(0.2, 0.5)
(0.3, 0.5)
##I want to make a new list that shows the product of each combinations,
## example: for every (x,y) combo, do x*y and make a new list called z
预期输出应产生一个新列表:
0.02
0.03
0.05
0.06
0.1
0.15
你可以通过简单的列表理解来完成,你不必使用 itertools
:
inlist = [(0.1, 0.2),
(0.1, 0.3),
(0.1, 0.5),
(0.2, 0.3),
(0.2, 0.5),
(0.3, 0.5)]
z = [round(elem[0]*elem[1],2) for elem in inlist]
print(z)
输出:
[0.02, 0.03, 0.05, 0.06, 0.1, 0.15]
但是,如果你想使用 itertools
,你可以使用 starmap
函数来实现:
from itertools import starmap
inlist = [(0.1, 0.2),
(0.1, 0.3),
(0.1, 0.5),
(0.2, 0.3),
(0.2, 0.5),
(0.3, 0.5)]
z = list(starmap(lambda i,j: round(i*j,2), inlist))
print(z)
输出:
[0.02, 0.03, 0.05, 0.06, 0.1, 0.15]
您可以使用任何可迭代对象来进行这些组合。
如果您是 Python 的新手,此答案试图阐明一些选项。
给定
一个reducing函数mul
和一个可迭代的数据:
def mul(a, b): # 1
"""Return a rounded multiple."""
return round(a * b, 2)
data = [0.1, 0.2, 0.3, 0.5] # 2
代码
准备一个可迭代的——一个可以循环的线性容器。示例:
选项 1 - 迭代器
iterable = it.combinations(data, 2)
选项 2 - pandas DataFrame
df = pd.DataFrame(data, columns=["weights"])
iterable = it.combinations(df["weights"], 2)
演示
[mul(x, y) for x, y in iterable] # 3
# [0.02, 0.03, 0.05, 0.06, 0.1, 0.15]
详情
- 此函数包含您要应用的操作。
- 一个简单的可迭代数据(例如
list
)。注意,不需要嵌套。 - 使用任意迭代来减少组合对
选项 3 - pandas 操作
或者,一旦您在 pandas 中有数据,您可以坚持使用它:
combs = list(it.combinations(data, 2))
df = pd.DataFrame(combs, columns=["a", "b"])
df
# a b
# 0 0.1 0.2
# 1 0.1 0.3
# 2 0.1 0.5
# 3 0.2 0.3
# 4 0.2 0.5
# 5 0.3 0.5
df.prod(axis=1).round(2).tolist()
# [0.02, 0.03, 0.05, 0.06, 0.1, 0.15]
我建议选择纯 Python 或 pandas
(选项 1 或 3)。