我正在尝试读一个字符串
char *string=malloc(sizeof(char));
char *start_string=string; //pointer to string start
while ((readch=read(file, buffer, 4000))!=0){ // read
filelen=filelen+readch; //string length
for (d=0;d<readch;d++)
*start_string++=buffer[d]; //append buffer to str
realloc(string, filelen); //realloc with new length
Run Code Online (Sandbox Code Playgroud)
有时这会崩溃并出现以下错误:
malloc: *** error for object 0x1001000e0: pointer being realloc'd was not allocated
Run Code Online (Sandbox Code Playgroud)
但有时不是,我不知道如何解决它.
我[a,b,c,d]在python中有一个列表,我通过使用for将它向左移动,for循环索引,我的结果是[b,c,d,a].
现在我已经计划将它移到右边,我假设结果将是[d,a,b,c],但我无法为它编写代码.
任何人都可以建议我使用代码和概念吗?
这是我向左移动的代码.
def shift_left(L):
first_item = L[0]
for i in range (1, len(L)):
L[i-1] = L[i]
L[-1] = first_item
Run Code Online (Sandbox Code Playgroud) 说你有一个清单['dogs, cats'].怎么能把它变成['dogs', 'cats']任意数量的['x, y, z']
我在python中使用redisbayes库来实现朴素的贝叶斯分类.但是当我写作 -
rb = redisbayes.RedisBayes(redis=redis.Redis())
rb.train('good', 'sunshine drugs love sex lobster sloth')
Run Code Online (Sandbox Code Playgroud)
它给出以下错误 -
ConnectionError: Error 10061 connecting localhost:6379.
No connection could be made because the target machine actively refused it.
Run Code Online (Sandbox Code Playgroud)
我试过这样做 -
pool = redis.ConnectionPool(host='localhost', port=6379, db=0)
rb = redisbayes.RedisBayes(redis=redis.Redis(connection_pool=pool))
Run Code Online (Sandbox Code Playgroud)
但它给出了同样的错误.我无法找到解决方案.如何使用python建立与redis的连接,或者使用MySQL的训练数据以任何其他方式在python中进行naive bayes分类?
def get_key(file):
'''(file open for reading) -> tuple of objects
Return a tuple containing an int of the group length and a dictionary of
mapping pairs.
'''
f = open(file, 'r')
dic = f.read().strip().split()
group_length = dic[0]
dic[0] = 'grouplen' + group_length
tup = {}
tup['grouplen'] = group_length
idx = 1
dic2 = dic
del dic2[0]
print(dic2)
for item in dic2:
tup[item[0]] = item[1]
print(tup)
return tup
Run Code Online (Sandbox Code Playgroud)
结果是:{'grouplen': '2', '"': 'w'}
dic 2是:
['"w', '#a', '$(', '%}', '&+', "'m", …Run Code Online (Sandbox Code Playgroud) 我试图在另一个python脚本中调用python脚本.目录是不同的.我试过了
import subprocess
subprocess.call("C:\temp\hello2.py", shell=True)
Run Code Online (Sandbox Code Playgroud)
但什么也没得到.这是行不通的.我回顾了很多论坛,但是当两个脚本都在同一个目录上时,它们都是关于调用它的.
我尝试在同一目录中同时使用这两个脚本.在这种情况下,我可以在Python.exe(通过cmd窗口)中运行模型,但不能在IDLE中运行.在IDLE中,我甚至没有收到错误消息.
我真的需要这样做,这样我就无法将其他脚本定义为不同的模块等.我需要在另一个脚本中调用脚本.
我有一个名为'new_data.txt'的'.txt'文档.现在它是空的.但是我在'for'循环中有一个'if'语句,如果'x'偶数与否则会出现.如果我想要(x +'是偶数!')添加到我的'new_data.txt'文档中.
for x in range(1,101):
if x % 2 == 0:
# and here i want to put something that will add: x + ' is even!' to my 'new_data.txt' document.
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?
python ×6
bayesian ×1
c ×1
c++ ×1
call ×1
linux ×1
python-2.7 ×1
redis ×1
subprocess ×1
unix ×1