? 使 "num RANDOM 1 ? IF [:num = 1] [print "Number is 1.] [print "Number is zero.] [:num = 1] 既不是 FALSE 也不是 TRUE!
为什么这行不通?我正在使用Imagine LOGO。
我有一个名为 Info 的类,其中 Info 有一个字符串类型的实例变量,可以通过 Info.getName()
我还有一个实例信息列表,例如class_list = [Info('Aleck'), Info('John')].
给定 a name_list = ['Aleck', 'Bob'],我想在 name_list 中删除 class_list 中具有相同名称的元素,同时我还需要知道名称(例如 Bob)是否不在 class_list 中(例如打印出 bob 不在列表中)
对于上面的示例,结果应该是 class_list = [Info('John')] 并打印出 bob 不在列表中。
我知道这样做的丑陋方式,例如以下代码(我实际上并没有运行它,只是一个示例),是否有优雅或 Pythonic 的方式来做到这一点?
def removeElement(name_list, class_list):
list_to_be_removed = []
for name in name_list:
is_name_in_list = false
for obj in class_list
if name == obj.getName():
list_to_be_removed.add(obj)
is_name_in_list = true
break
if is_name_in_list == false:
print name + ' is not in the list'
is_name_in_list = false …Run Code Online (Sandbox Code Playgroud) 我有以下问题。有两个 n 维整数数组,我需要确定满足多个条件的项目的索引。
所以假设我们有:
array1 = np.array([1,-1,-2])
array2 = np.array([0,1,1])
Run Code Online (Sandbox Code Playgroud)
然后它应该返回索引 2(第三个数字)。我正在尝试按如下方式编程:
import numpy as np
n = 3
array1 = np.array([1,-1,-2])
array2 = np.array([0,1,1])
indices = [i for i in range(n) if array1[i]<0]
indices2 = [i for i in indices if array2[i] == min(array2[indices])]
index = [i for i in indices2 if array1[i] == min(array1[indices2])][0] #[0] breaks the tie.
Run Code Online (Sandbox Code Playgroud)
这似乎有效,但是,我觉得它不是很优雅。在我看来,您应该能够在一两行中完成此操作,并且定义较少的新变量。有人有改进的建议吗?提前致谢。
一个非常基本的问题。我有一个包含 14 个变量和 576 个观察值的数据框。
> head(Boston)
crim zn indus chas nox rm age dis rad tax ptratio black lstat medv
1 0.00632 18 2.31 0 0.538 6.575 65.2 4.0900 1 296 15.3 396.90 4.98 24.0
2 0.02731 0 7.07 0 0.469 6.421 78.9 4.9671 2 242 17.8 396.90 9.14 21.6
3 0.02729 0 7.07 0 0.469 7.185 61.1 4.9671 2 242 17.8 392.83 4.03 34.7
4 0.03237 0 2.18 0 0.458 6.998 45.8 6.0622 3 222 18.7 394.63 2.94 …Run Code Online (Sandbox Code Playgroud) 我有两个模型,一个用户模型和一个课程模型
用户架构如下所示:
create_table "users", force: true do |t|
t.string "username", default: "", null: false
t.string "first_name", default: "", null: false
t.string "last_name", default: "", null: false
t.string "password_digest", default: "", null: false
t.string "email", default: "", null: false
t.string "email_unconfirmed", default: "", null: false
t.string "email_verification_token", default: "", null: false
t.string "password_reset_token", default: "", null: false
t.datetime "password_reset_token_expires_at"
t.boolean "admin", default: false
t.boolean "teacher", default: false
t.datetime "created_at"
t.datetime "updated_at"
end
Run Code Online (Sandbox Code Playgroud)
课程架构如下所示:
create_table "courses", force: true do |t|
t.integer "teacher_id" …Run Code Online (Sandbox Code Playgroud) ruby if-statement ruby-on-rails associations conditional-statements
我正在使用AngularJS,我正在编写自己的指令.我想在我的自定义指令中使用条件逻辑.问题是由template部分引起的.这是我的一段代码:
angular.module('myDirectives').directive('widget', function() {
return {
replace: true,
restrict: 'E',
template:
'<div class="widget">' +
'<div class="panel panel-default">' +
'<div class="panel-heading">' +
'<a href="" class="btn btn-default" ng-click="isCollapsed = !isCollapsed">' +
'<i class="fa" ng-class=" { 'fa-angle-up': !isCollapsed, 'fa-angle-down': isCollapsed } "></i>' +
'</a>' +
'</div>' +
'<div class="panel-body" collapse="isCollapsed">' +
'<p>Panel Content</p>' +
'</div>' +
'</div>' +
'</div>',
transclude: true
}
});
Run Code Online (Sandbox Code Playgroud)
此行引发错误.
'<i class="fa" ng-class=" { 'fa-angle-up': !isCollapsed, 'fa-angle-down': isCollapsed } "></i>'
Run Code Online (Sandbox Code Playgroud)
在''各地fa-angle-up和fa-angle-down导致此.可能有一个非常简单的解决方法,但我还没有想到它.所以我的问题是你们的; …
conditional-statements angularjs angularjs-directive ng-class
1.Imagine条件if (obj.is_x() || obj.is_y() || obj.is_z())
将obj.is_y()和obj.is_z()被调用,如果评估obj.is_x()返回真的吗?
这个坏主意(一般来说)?这段代码看起来不好吗?
bool isbn13_prefix_valid (const string& prefix)
{
unsigned num = stoi(prefix);
if (num == 978 || num == 979) return 1; //super common ones
else if ( num >= 0 && num <= 5 || num == 7 || num >= 600 && num <= 649
|| num >= 80 && num <= 94 || num >= 950 && num <= 989
|| num >= 9900 …Run Code Online (Sandbox Code Playgroud) c++ optimization evaluation logical-operators conditional-statements
假设我的DataFrame构造如下:
import pandas
import numpy
column_names = ["name", "age", "score"]
names = numpy.random.choice(["Jorge", "Xavier", "Joaquin", "Juan", "Jose"], 50)
ages = numpy.random.randint(0, 100, 50)
scores = numpy.random.rand(50)
df = pandas.DataFrame.from_dict(dict(zip(column_names, [names, ages, scores])))
Run Code Online (Sandbox Code Playgroud)
上面的前10行DataFrame如下所示。
age name score
0 15 Jorge 0.031380
1 44 Juan 0.373199
2 84 Xavier 0.999065
3 55 Juan 0.159873
4 55 Joaquin 0.211931
5 33 Juan 0.484350
6 22 Xavier 0.510276
7 86 Joaquin 0.490013
8 2 Jose 0.185086
9 51 Juan 0.979015 …Run Code Online (Sandbox Code Playgroud) 我有一个格式的文件:
- foo bar - baz
one two three - or four
and another line
- next job
do this - and that
Run Code Online (Sandbox Code Playgroud)
我的语法是
grammar tasks {
regex TOP { \n* <oneTask>+ \n* }
regex oneTask { ^^ \- (<oneSection> <endSection>)+ }
regex oneSection { \N+ } # this is not quite working
regex endSection { \n+ }
Run Code Online (Sandbox Code Playgroud)
}
在正则表达式oneSection中,我如何编写"我想匹配' - '只有当它不在一行开头时"的事实?
我把文件放入一个字符串并解析这个字符串:
my $content = slurp("taskFile");
my $result = tasks.parse($content);
Run Code Online (Sandbox Code Playgroud)
这不太合适.
<[\N] - [\-]> does not make the match …Run Code Online (Sandbox Code Playgroud) 所以有一种情况:
数据帧:
dat <- data.frame(colA = rep(c(0,1,0), c(6,1,8)),
colB = rep(c(1,0,1,0), c(1,4,1,9)),
colC = rep(c(0,1,0), c(9,1,5)),
colD = rep(c(0,1,0), c(8,1,6)),
colE = rep(0, 15),
color = rep(c("blue","red","yellow"), each=5),
colorId = rep(c(22,40,35), each=5))
colA colB colC colD colE color colorId
0 1 0 0 0 Blue 22
0 0 0 0 0 Blue 22
0 0 0 0 0 Blue 22
0 0 0 0 0 Blue 22
0 0 0 0 0 Blue 22
0 1 0 0 0 Red …Run Code Online (Sandbox Code Playgroud) python ×3
dataframe ×2
r ×2
angularjs ×1
associations ×1
c++ ×1
evaluation ×1
if-statement ×1
indexing ×1
indices ×1
list ×1
logo-lang ×1
match ×1
ng-class ×1
optimization ×1
pandas ×1
perl6 ×1
regex ×1
ruby ×1
set ×1