我是C程序员.我是python的新手.在C中,当我们定义二叉树节点的结构时,我们为它的右边和左边的子节点分配NULL:
struct node
{
int val;
struct node *right ;
struct node *left ;
};
Run Code Online (Sandbox Code Playgroud)
在初始化节点时,我们写为:
val = some_value
right = NULL;
left = NULL;
Run Code Online (Sandbox Code Playgroud)
现在我的问题是:如何在Python中为节点的左右指针分配NULL值?
我们如何测试Python版本的NULL?在C中它将是:
if( ptr->right == NULL )
Run Code Online (Sandbox Code Playgroud)
谢谢!
在Android应用程序中,我正在发送从中获取的图片,Camera Intent因此我需要将a Bitmap转换为字节数组.要做到这一点,我使用ByteArrayOutputStream如下:
private byte[] getRawImageData(Bitmap source) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] rawImageData = null;
try {
source.compress(CompressFormat.JPEG, DEFAULT_COMRESSION, baos);
rawImageData = baos.toByteArray();
} finally {
try {
baos.close();
} catch (IOException e) {
// handle exception here
}
}
return rawImageData;
}
Run Code Online (Sandbox Code Playgroud)
一切都运行正常,真正的问题是ByteArrayOutputStreamjavadoc与Android 文档之间的差异.
该Javadoc中读取
关闭ByteArrayOutputStream无效.
在Android的文档读取:
关闭此流.这将释放用于此流的系统资源.
我正在关闭流不管什么,但我想知道哪些文档是正确的以及为什么它们是不同的.
我有这个型号:
class Movie(models.Model):
# here are some fields for this model
Run Code Online (Sandbox Code Playgroud)
我添加了以下字段(数据库已经填充了Movie模型):
user = models.ForeignKey(User, default=1)
Run Code Online (Sandbox Code Playgroud)
我运行命令'makemigrations'然后'迁移':
python manage.py makemigrations myapp
python manage.py migrate
Run Code Online (Sandbox Code Playgroud)
但它不起作用.我想要做的是将"用户"字段添加到Movie对象,并为我的数据库中的所有现有Movie对象提供默认值(在这种情况下,默认值为id = 1的User对象).
我尝试的另一件事是保留它没有默认值,然后,当我运行'makemigrations'命令时,给它默认值1(通过选择"现在提供一次性默认值"选项).在这两种情况下,当我运行'migrate'命令时,我得到一个IntegrityError:
django.db.utils.IntegrityError: movies_movie__new.user_id may not be NULL
Run Code Online (Sandbox Code Playgroud)
我还检查了数据库中已有用户的ID,以确保id = 1的用户存在且确实存在.那为什么不起作用呢?谢谢.
如何在 SQLAlchemy 中进行嵌套连接?我试图运行的语句是
SELECT a.col1, a.col2, c.col3
FROM a
LEFT OUTER JOIN (b INNER JOIN c ON c.col4 = b.col4) ON b.col5 = a.col5
Run Code Online (Sandbox Code Playgroud)
我需要显示 中的所有记录A,但仅将它们与 中的记录B连接JOIN起来C。
到目前为止我的代码是
session.query(a.col1, a.col2, c.col3).outerjoin(b, b.col5 == a.col5).all()
Run Code Online (Sandbox Code Playgroud)
这让我得到了我所需要的大部分内容,A记录在缺少记录的地方显示空值B;然而,太多的B人进来了,我需要限制他们。但是,如果我只是添加另一个连接,即
session.query(a.col1, a.col2, c.col3).outerjoin(b, b.col5 == a.col5).join(c, b.col4 == c.col4).all()
Run Code Online (Sandbox Code Playgroud)
它会删除 中所有A具有空值的记录B。
我应该指出,我无法直接加入,A因为C两者之间的唯一联系是通过B。
我正试图制作一个"目标",一个"网格"的副本.我不明白为什么这段代码不起作用.
grid = [[randint(0, 1), randint(0, 1), randint(0, 1)],
[randint(0, 1), randint(0, 1), randint(0, 1)],
[randint(0, 1), randint(0, 1), randint(0, 1)]]
target = [[0, 0, 0]] * 3
for x in range(3):
for y in range(3):
target[x][y] = grid[x][y]
print target
print grid
Run Code Online (Sandbox Code Playgroud)
这是一个结果:
[[0, 1, 0], [0, 1, 0], [0, 1, 0]]
[[1, 0, 0], [0, 1, 1], [0, 1, 0]]
Run Code Online (Sandbox Code Playgroud) 我的代码的重点是检查4个单词的句子以及它是否长4个单词.
import random
import time
import urllib
numwords = 4
#getWordList
wordlist = []
def getWordList() :
url = "some word list url"
flink = urllib.urlopen(url)
#print "Reading words from %s" % url
words = [ ] # word list
for eachline in flink :
text = eachline.strip()
text = text.replace('%','')
words += [text.lower()]
flink.close()
#print "%d words read" % len(words)
words.append('i','am','an','to')
return wordlist
warning = "\n"
prompt = "Enter a sentence with four words: "
while warning:
usrin = …Run Code Online (Sandbox Code Playgroud)