在Python中,我有一个元组列表和一个长度相同的整数列表,例如,
a = [
[1, 2],
[3, 2],
[4, 66],
[2, 3]
]
b = [
1,
31,
31,
44
]
Run Code Online (Sandbox Code Playgroud)
第k个条目a可以被认为与第k个条目相关联b.
条目[3, 2]和[2, 3]真的对我来说是相同的,我想a考虑到这一点uniquified.另外,我想要一个属于新唯一列表的条目列表.对于上面的例子,
a2 = [
[1, 2],
[3, 2], # or [2, 3]
[4, 66]
]
b2 = [
[1],
[31, 44],
[31]
]
Run Code Online (Sandbox Code Playgroud)
b2[0]是[1]因为[1, 2]只与相关1.b2[1]是[31, 44]因为[2, 3](等于[3, 2]与相关联31并44 …
我有一份指数清单,例如,
a = [
[2],
[0, 1, 3, 2],
[1],
[0, 3]
]
Run Code Online (Sandbox Code Playgroud)
我现在想"反转"这个名单:数字0出现在索引1和3,所以:
b = [
[1, 3],
[1, 2],
[0, 1],
[1, 3]
]
Run Code Online (Sandbox Code Playgroud)
关于如何快速做到这一点的任何提示?(我正在处理的列表可能很大.)
额外奖励:我知道每个索引都会出现两次a(就像上面的例子一样).
我在一个类中有两个函数,plot()和show().show()作为便捷的方法,没有别的,而不是两行添加到的代码plot()一样
def plot(
self,
show_this=True,
show_that=True,
color='k',
boundary_color=None,
other_color=[0.8, 0.8, 0.8],
show_axes=True
):
# lots of code
return
def show(
self,
show_this=True,
show_that=True,
color='k',
boundary_color=None,
other_color=[0.8, 0.8, 0.8],
show_axes=True
):
from matplotlib import pyplot as plt
self.plot(
show_this=show_this,
show_that=show_that,
color=color,
boundary_color=boundary_color,
other_color=other_color,
show_axes=show_axes
)
plt.show()
return
Run Code Online (Sandbox Code Playgroud)
这一切都有效.
我的问题是,这似乎这样的代码太多show()的包装.我真正想要的是:让我们show()拥有相同的签名和默认参数plot(),并将所有参数转发给它.
任何提示?
我想要一个除法方法,它返回a / b, 0if bis 0,nan如果至少有一个aor bis nan,并且适用于标量和数组输入,即,
import numpy as np
def div(a, b):
out = a / b
# out[b == 0] = 0 TypeError: 'float' object does not support item assignment
# out = np.nan_to_num(out, nan=0.0)
return out
assert div(2, 1) == 2
assert np.all(div(np.array([2, 2]), np.array([1, 1])) == np.array([2, 2]))
assert np.isnan(div(np.nan, np.nan))
assert div(np.array(0), np.array(0)) == 0
Run Code Online (Sandbox Code Playgroud)
np.nan_to_num几乎可以解决问题,但我很想控制条件(b == 0vs isnan …
我有一个模板化的课程pair,我想在课堂外编写一个show函数来做一些想象.在明确指定模板类型时,它都按预期工作:coutshow
#include <iostream>
template <class A_Type>
class pair
{
public:
A_Type a0;
A_Type a1;
};
void show(const pair<double> & p) {
std::cout << p.a0 << std::endl;
std::cout << p.a1 << std::endl;
}
int main() {
pair<double> p;
p.a0 = 1.2;
p.a1 = 1.3;
show(p);
}
Run Code Online (Sandbox Code Playgroud)
我想show忘记模板类型.
任何提示?