我们怎样才能在python程序中找到所有变量?例如. 输入
def fact(i):
f=2
for j in range(2,i+1):
f = f * i
i = i - 1
print 'The factorial of ',j,' is ',f
Run Code Online (Sandbox Code Playgroud)
产量
变量 - f,j,i
我再次制作一个简单的Checkers.并且,为了跟踪检查员移动到的所有位置,我创建了一个列表来添加所有位置.
Locations = [(10, 10), (70, 10), (130, 10), (190, 20), (250, 10), (310, 10), \
(370, 10), (430, 10), (10, 70), (130, 70), (190, 70), (250, 70), \
(310, 70), (370, 70), (430, 70), (10, 130), (70, 130), (130, 130), \
(190, 130), (250, 130), (310, 130), (370, 130), (430, 130), \
(10, 190), (70, 190), (130, 190), (190, 190), (250, 190), (310, 190), \
(370, 190), (430, 190)]
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试执行程序时,我总是得到:Ld8 = Locations [31] - 列表索引超出范围
所以,我想也许一个列表只能包含一定数量的数字.因此,我创建了第二个位置列表并添加了行E-H,以便拆分列表.但是,我仍然收到索引超出范围的相同错误.(Ld8是存储行D,第8列的位置的变量)
使用这种智能指针铸造是否安全?
APtr a(new A());
BPtr & b = (Bptr&)a; // this is it
Run Code Online (Sandbox Code Playgroud)
那里,
class A
{
public:
virtual ~A(){}
virtual void methodA() = 0;
}
typedef std::tr1::shared_ptr<A> APtr;
class B : public A
{
public:
virtual ~B(){}
virtual void methodB() = 0;
}
typedef std::tr1::shared_ptr<B> BPtr;
/////////////////////////////////////////////////////////////////////////////////
BPtr & b = a; //this way doesn't work
Run Code Online (Sandbox Code Playgroud) 我写了一些我试图修复的有缺陷的Huff压缩代码.我做的第一件事就是将指针切换到auto_ptr
(有理由我没有使用另一个智能指针).我创建了一个向量,auto_ptr
但是当我尝试将auto_ptr传递给函数时,*(vector.begin())
它不起作用.
我的功能代码我试图将所有权传递给(它是一个成员函数为set_node):
struct Node {
int weight;
char litteral;
auto_ptr<Node> childL;
auto_ptr<Node> childR;
void set_node(int w, char l, auto_ptr<Node>& L(), auto_ptr<Node>& R()){
weight = w;
litteral = l;
childL = L;
childR = R;
}
};
Run Code Online (Sandbox Code Playgroud)
这就是我尝试调用它的方式(p是节点):
p.set_node(w, '*', *nodes->begin(), *(nodes->begin()+1));
Run Code Online (Sandbox Code Playgroud)
这是向量声明的方式:
vector<auto_ptr<Node> >* nodes = new vector<auto_ptr<Node> >;
Run Code Online (Sandbox Code Playgroud) 我试图在Python中做一个简单的子字符串匹配,虽然我记得昨晚它工作正常,但从早上开始这段代码就出现了以下错误.这似乎是一个已知的错误,任何人都可以建议为什么会发生这种错误?
if string.find(row[1],drug) != -1:
print "abstract id = ", row[0],"Drug found=", drug
Run Code Online (Sandbox Code Playgroud)
错误:
File "./substring.py", line 31, in <module>
if string.find(row[1],drug) != -1:
TypeError: slice indices must be integers or None or have an __index__ method
Run Code Online (Sandbox Code Playgroud)
这里row[1]
和drug
两者都是简单的字符串.
我有一个qt表单,有一个乘法问题和4个按钮.4个按钮(选项)是随机生成的,问题也是如此.
我只想将4个选项随机化,因此它们是随机的,但它们都不等同于其他选择.这就是我现在正在做的事情,但是效果并不好:
while (choice1 == choice2 || choice1 == choice3 || choice1 == choice4)
choice1 = (rand() % max) + 1;
while (choice2 == choice1 || choice2 == choice3 || choice2 == choice4)
choice2 = (rand() % max) + 1;
while (choice3 == choice1 || choice3 == choice2 || choice3 == choice4)
choice3 = (rand() % max) + 1;
while (choice4 == choice1 || choice4 == choice2 || choice4 == choice3)
choice4 = (rand() % max) + 1;
Run Code Online (Sandbox Code Playgroud)
还有其他人有更好的方法吗?
我正在使用python 3
我有一个不同元组的列表.每个元组的第一个值总是相同,但其余的变化.如何在包含第一个值的元组之后搜索列表,然后将整个元组替换为列表中的新元组?例如:这是我的元组:(122, 23, 24, 9)
这个元组在列表中,第一个值总是相同,但其余的变化我如何找到列表中的元组,其中第一个值等于我的元组"122"并替换它我的整个元组
(125, 34, 35, 21)
(122, 341, 5, 27)
(124, 31, 51, 7)
Run Code Online (Sandbox Code Playgroud) 为链表创建节点时,它可能如下所示:
template <class T>
class node {
T data;
node* next;
}
Run Code Online (Sandbox Code Playgroud)
对于双向链表,它可能看起来像这样:
template <class T>
class node {
T data;
node* next;
node* prev;
}
Run Code Online (Sandbox Code Playgroud)
对于BST,它可能看起来像这样:
template <class T>
class node {
T data;
node* left_child;
node* right_child;
}
Run Code Online (Sandbox Code Playgroud)
这可以按以下格式推广:
template <class T>
class node {
T data;
node* links[N]; // N = 1 for linked list, N = 2 for tree or doubly linked list, etc...
}
Run Code Online (Sandbox Code Playgroud)
在不使用STL向量的情况下在类ctor中指定N的最佳方法是什么?
window.VARIABLE_NAME
不管用。我收到以下错误,请指导我。
Uncaught SyntaxError: Unexpected token.
Run Code Online (Sandbox Code Playgroud)
我写了以下代码。
var window.testing
Run Code Online (Sandbox Code Playgroud) 我是shell脚本的新手,请任何人解释下面的代码
ls *.py > xx
while [ 1 ]
do
read myline || break
python $myline
python $myline --genxml
done<xx
service nac-ms restart
Run Code Online (Sandbox Code Playgroud)
Thnaks Mukthyar