可以说我有以下内容
class Parent {
protected:
virtual void overrideMe() = 0;
}
class ChildA : public Parent {
protected:
void overrideMe() = 0{}
}
class ChildB : public Parent {
protected:
void overrideMe() = 0{}
}
class UtilClass {
public:
vector<Parent> planes;
void compute() {
Parent& p = planes[0];
}
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我会在UtilsClass中的compute()中输入一个错误,即"Parent"无法初始化.
我想做的是在UtilClass中填充数组"plane"(使用ChildA或childB,即非混合类型)并进行一些计算.
我是否必须在初始化期间使用指针,或者更好地使用模板?我几乎可以肯定模板的使用是不必要的,因为我想限制只使用父类的子项.
我想在python中分析一个拥有1000万用户和密码的文件.文件采用文本格式.以下是一些数据:
0000 00000000
0000 00001
0000 00001111
0000 000099
0000 00009999
0000 0000w
0000 5927499
0000 634252
0000 6911703
0000 701068
Run Code Online (Sandbox Code Playgroud)
在python中,我使用以下代码来读取文件:
f=open('10-million-combos.txt','r')
a=[]
for line in f.readlines():
a.append(line)
Run Code Online (Sandbox Code Playgroud)
上面的代码需要几秒钟才能运行.保存在列表中的数据如下所示:
>>>a[0:2]
['0000\t00000000\n', '0000\t00001\n']
Run Code Online (Sandbox Code Playgroud)
要提取我使用的用户和密码:
b=[]
for i in a:
b.append(i.split('\t'))
Run Code Online (Sandbox Code Playgroud)
问题是,上面的代码在很长一段时间后遇到内存错误,我无法分离用户和密码.
您对解决方案有什么建议吗?
我写了一个非常具有缩进气质的脚本,所以我决定制作函数.我是python的新手,现在我已经创建了这些函数,没有任何作用!
def main ():
wiki_scrape()
all_csv()
wiki_set = scraped_set('locations.csv')
country_set = all_set('all.csv')
print wiki_set
Run Code Online (Sandbox Code Playgroud)
我只是想知道这是否是从main()函数调用函数的正确方法?我一直在辩论如果在被调用函数中发生缩进问题,python似乎非常依赖于正确的缩进,即使它没有出现错误!
完整代码 - http://pastebin.com/gJGdHLgr
我有两个这样的列表:
newList = (
(1546, 'John'),
(8794, 'Michael'),
(892416, 'Dave'),
(456789, 'Lucy'),
)
oldList = (
(1546, 'John'),
(8794, 'Michael'),
(892416, 'Dave'),
(246456, 'Alexander')
)
Run Code Online (Sandbox Code Playgroud)
我想要一个比较两个列表的功能.它会是这样的:
def compare(new, old):
print('Alexander is not anymore in the new list !')
print('Lucy is new !')
return newList
Run Code Online (Sandbox Code Playgroud)
我想与每个人的身份比较独特.
编辑:结果将是我的功能比较.它打印出差异.我不知道如何开始
我有一个这样的类层次结构:
class Base {
public:
virtual bool foo() const = 0;
static Base& getHeadInstance();
};
class Concrete: public Base {
public:
explicit Concrete(Base& f): fwd(f) {}
// ... other member functions elided
bool foo() const override { return /* some calculation */ && fwd.foo(); }
private:
Base& fwd;
};
Run Code Online (Sandbox Code Playgroud)
这样我就可以构造一系列这样的实例:
Concrete c1(Base::getHeadInstance());
Concrete c2(c1);
Concrete c3(c2);
Run Code Online (Sandbox Code Playgroud)
这样就c3可以做出决定,并可能要遵循c2,而后者又可以c1像Chain of Responsibility模式一样遵循。
问题在于c2且c3构造不正确,其fwd成员始终引用Base::getHeadInstance()。
这里出了什么问题?解决方法是什么?
更新:
静态成员返回什么都无所谓。假设它返回了一个实例:
class …Run Code Online (Sandbox Code Playgroud) 我的程序应该使用用户输入绘制棋盘。绘图必须在调用 draw() 时发生。我收到一个错误:
line 18, in main
chessboard = Chessboard(tr, startX, startY, eval(width), val(height))
TypeError: __init__() takes from 3 to 5 positional arguments but 6 were given
Run Code Online (Sandbox Code Playgroud)
问题是我的主模块不能改变。我必须保持原样。这是一个要求。那么,当我无法从中改变一件事时,我该如何解决这个问题?它只在第 18 行给我一个错误。
主要模块:
import turtle
from chessboard import Chessboard
def main():
startX, startY = eval(input("Enter a starting point: "))
width = input("Input a width: ")
height = input("Input a height: ")
tr = turtle.Turtle()
if width == "" and height == "":
chessboard = Chessboard(tr, startX, startY)
elif height == …Run Code Online (Sandbox Code Playgroud) 我有一个应用程序,我正在尝试使用 Python 使其独立于平台。
我在所有 3 个操作系统(Mac、Win10、Ubuntu)中都安装了 Python 3.x
我有一个 python 脚本 batch.py ,它从自身内部调用其他 python 脚本,如下所示:
import os
import argparse
import shutil
if __name__ == '__main__':
parser.add_argument("-i", "--infolder", default="./pdfs",
help="Input folder with PDFs. Default: ./pdfs")
args = parser.parse_args()
infolder =args.infolder
# Watermarking process
watermark_outfolder = tmp+'/pdfs_watermarked'
if not os.path.exists(watermark_outfolder):
os.makedirs(watermark_outfolder)
else:
for root, dirs, files in os.walk(watermark_outfolder):
for f in files:
os.unlink(os.path.join(root, f))
for d in dirs:
shutil.rmtree(os.path.join(root, d))
watermark_command = 'python watermark.py --in '+infolder
os.system(watermark_command)
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是在 Linux 中,当我使用os.system('python ... …
test = [1,2]
one, two = test[:]
print(one)
print(two)
one, two = test[:] if len(test)==2 else test[0], test[0]
print(one)
print(two)
Run Code Online (Sandbox Code Playgroud)
输出:
1, 2, [1, 2], 1
Run Code Online (Sandbox Code Playgroud)
为什么在if用作三元运算符时解压列表会失败?
if (dice1 == dice2) or (dice1 == dice3) or (dice2 == dice1) or (dice2 == dice3)
or (dice3 == dice2) or (dice3 == dice1):
score = # no clue what to put here
Run Code Online (Sandbox Code Playgroud)
我需要找到办法;如果这些选项中的任何一个是正确的,那么将选择正确的选项并将骰子变量加在一起,例如两个或多个骰子是否相等?是的,那么score = sum of two equal dice。
真的很困惑,谁能提供帮助?
我有这个代码:
def positive(A):
if len(A)==0:
return 0
else:
if A[0]>0:
return A [0]+positive(A[1:])
else:
return positive(A[1:])
Run Code Online (Sandbox Code Playgroud)
理论上,当我输入一个数字列表时,它会返回正数列表。例如,当我通过时[2,5,-3,-5,2,-6]它会返回[2,5,2]. 但是在我的代码中,发生的情况是评估了正值,因此返回9. 我认为我的问题是A [0]+positive(A[1:])一致的,但我不知道如何改变它。任何帮助表示赞赏。