我对下面的python代码中的"x"感到困惑.
>>> # Grocery list
... grocery_list = ['apples', 'bananas', 'oranges', 'milk']
>>> for x in grocery_list:
... print(x, len(x))
Run Code Online (Sandbox Code Playgroud)
我对上述for语句中x的作用感到困惑."x"是在for语句中定义的变量,还是其他东西?它只是似乎比如何,我用来定义一个变量不同,但我注意到"X"可以是任何东西,这让我觉得这是,事实上,一个用户定义的变量.
请帮忙.
我已经从我的真实代码编辑了这个,所以它更容易理解.
基类:
class MWTypes
{
public:
virtual long get() { return (0); }
};
Run Code Online (Sandbox Code Playgroud)
派生类:(还有其他类,如char,double等等......)
class TypeLong : public MWTypes
{
public:
TypeLong(long& ref) : m_long(ref) {}
~TypeLong();
long get() { return m_long; }
private:
long& m_long;
};
Run Code Online (Sandbox Code Playgroud)
和存储类:
class RowSet
{
public:
void addElememnt(MWTypes elem);
MWTypes getElement();
std::vector<MWTypes> getVector() { return m_row; }
private:
std::vector<MWTypes> m_row;
};
Run Code Online (Sandbox Code Playgroud)
怎么称呼:
for (i = 0; i < NumCols; i++) // NumCols is 3 on this instance
{
switch(CTypeArray[i]) // this is an …Run Code Online (Sandbox Code Playgroud) 我是C++的新手,我在OCaml和Python方面有更多的经验.我想通过制作一个播放"Morpion Solitaire"的程序来学习C++.我的开始有点困难.
在以下代码中:
typedef enum {NORTH, NORTHEAST, EAST, SOUTHEAST} direction;
char deltax[4] = { 0, 1, 1, 1};
char deltay[4] = { 1, 1, 0, -1};
class Coords {
private:
char x,y;
public:
Coords(char xx,char yy){
x = xx;
y = yy;
};
char get_x() const { return x;}
char get_y() const { return y;}
};
class Line {
private:
Coords orig;
direction dir;
Coords newcross;
public:
Line(char x1, char y1, direction d, char x2, char y2) {
orig = …Run Code Online (Sandbox Code Playgroud) 我想通过比较输出信号及其真实输出值来计算每个输入的神经网络的输出误差,因此我需要两个矩阵来计算此任务。
我有 (n*1) 形状的输出矩阵,但在标签中我只有应该被激活的神经元的索引,所以我需要一个形状相同的矩阵,所有元素都为零,除了它的索引是等于标签。我可以用一个函数来做到这一点,但我想知道numpypython 中是否有一个内置的方法可以为我做到这一点?
我正在学习python 3中的元编程.我的教授确实给了我们这个练习:
编写元类法术,用于在向导中转换学生的实例.
我这样做了:
class Spell(type):
def __new__(cls, classname, supers, cls_dict):
cls_dict['__init__'] = Spell.toWizard()
cls_dict['__qualname__'] = 'wizard'
return type.__new__(cls, 'wizard', supers, cls_dict)
@staticmethod
def toWizard():
def init(*args):
wizard(args, 'fire_spell')
return init
class student(metaclass=Spell):
def __init__(self, name):
self.name = name
class person():
def __init__(self, name):
self.name = name
class wizard(person):
def __init__(self, name, magic):
self.magic = magic
super().__init__(name)
if __name__ == '__main__':
s = student('name1')
print(s.__dict__)
s = student('name2')
print(s.__dict__)
Run Code Online (Sandbox Code Playgroud)
__init__正确调用向导类而不是学生类__init__,但创建的对象为空__dict__.我哪里错了?
我有一个循环冗余两个类之间在我的项目循环依赖,StatusEffect和BaseCharacter.
这两个类需要彼此了解,因为BaseCharacter需要存储一组StatusEffects并且StatusEffect需要能够对其进行操作BaseCharacter.我不认为可以消除这种行为并仍然可以正常工作.这就是我现在要做的事情:
基本字符命名空间内部存在Game::Character和StatusEffect存在的命名空间内Game::StatusEffects
在StatusEffects.h里面,我BaseCharacter像这样声明:
namespace Game {
namespace Character {
class BaseCharacter;
}
}
Run Code Online (Sandbox Code Playgroud)
然后我在下面:
namespace Game
{
namespace StatusEffects
{
class StatusEffect
{
public:
virtual void TickCharacter(Game::Character::BaseCharacter* character, int ticks = 1)
{
std::cout << "Name " << character->GetName() << std::endl;
}
protected:
private:
std::string name;
int StatusEffectUID;
};
}
}
Run Code Online (Sandbox Code Playgroud)
但是,这给了我一个编译器错误:
错误1错误C2027:使用未定义类型'Game :: Character :: BaseCharacter'
我认为,因为我正在使用指针,这个前向声明很好.我需要转发申报吗?我不需要转发声明整个类定义吗?
有人可以向我解释为什么这段代码总是返回数组的第一个元素?(本例中为7):
int a[] = {7,1,3};
printf("%d", *a);
Run Code Online (Sandbox Code Playgroud) 我生活在假设中,定义_ _len_ _和_ _getitem_ _方法对于一个类来说已经足够了,所以它的实例可以通过a迭代for element in instance:,直到它被违反了一个例子.我的原始代码完全不同,但这显示了很好地进入无限循环的问题:
class Limited(object):
def __init__(self, size=5):
self.size = size
def __len__(self):
return self.size
def __getitem__(self, item):
return item*10
if __name__ == "__main__":
test = Limited(4)
assert len(test) == 4
for q in test:
print q
Run Code Online (Sandbox Code Playgroud)
我找不到对终止迭代循环的要求的具体引用,但似乎如果不想遵守完整的Iterator协议,则需要像IndexError或StopIteration这样的异常才能终止.
这是正确的,在哪里找到它记录?
我很好奇,当我们导入一个模块又导入另一个模块时会发生什么。因此,我创建了两个模块:module1和module2。
模块1:
import random
print(random.randint(0,10))
print("module1 work")
Run Code Online (Sandbox Code Playgroud)
模块2:
import module1
print("module2 work")
Run Code Online (Sandbox Code Playgroud)
当我运行module2时,它给出以下输出:
1
module1 work
module2 work
Run Code Online (Sandbox Code Playgroud)
因此,我决定在导入random时确实导入了module1。但是,当我键入Shell时,print(random.randint(0,10))它会引发一个NameError: name 'random' is not defined。所以random不是从进口的module1。但是在这种情况下,为什么要进行module2print 1,而没有引发与Shell相同的错误?
我正在尝试使用 5 个变量列表进行 Python 冒泡排序。用户将输入 5 个值,并且bubble_sort将按升序排列它们,但是当我尝试运行它时,它会出现错误:
lista = list(n1, n2, n3, n4, n5)\n ^^^^^^^^^^^^^^^^^^^^^^^^\nTypeError: list expected at most 1 argument, got 5\nRun Code Online (Sandbox Code Playgroud)\n这是我写的代码:
\nprint(\'Ol\xc3\xa1!\')\nmsg = \'Ol\xc3\xa1!\'\nn1 = float(input(\'Digite um n\xc3\xbamero qualquer:\'))\nn2 = float(input(\'Digite outro n\xc3\xbamero qualquer:\'))\nn3 = float(input(\'Digite outro n\xc3\xbamero qualquer:\'))\nn4 = float(input(\'Digite outro n\xc3\xbamero qualquer:\'))\nn5 = float(input(\'Digite outro n\xc3\xbamero qualquer:\'))\nlista = list(n1, n2, n3, n4, n5)\ndef bubble_sort(arr):\n n = len(arr)\n for i in range(n):\n for j in range(0, n - i - 1):\n …Run Code Online (Sandbox Code Playgroud) python ×6
c++ ×3
arrays ×1
c ×1
inheritance ×1
matrix ×1
metaclass ×1
numpy ×1
pointers ×1
polymorphism ×1
python-2.7 ×1
vector ×1
virtual ×1