为什么以下python3代码会产生错误?
a='''
def x():
print(42)
'''
class Test:
def __init__(self):
exec(a)
x()
t = Test()
Run Code Online (Sandbox Code Playgroud)
结果在此消息中:
Traceback (most recent call last):
File "bug.py", line 11, in <module>
t = Test()
File "bug.py", line 9, in __init__
x()
NameError: global name 'x' is not defined
Run Code Online (Sandbox Code Playgroud) 每次启动Scala 2.9.2 REPL(Java HotSpot(TM)64位服务器VM,Java 1.8.0-ea)后,第一行代码执行都会给我带来错误:
scala> 1 + 2
error: error while loading CharSequence, class file '/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar(java/lang/CharSequence.class)' is broken
(bad constant pool tag 15 at byte 1484)
Run Code Online (Sandbox Code Playgroud)
稍后在当前REPL实例的进一步评估期间,不会发生类似的错误.
有没有人建议如何处理这种行为?
我们有两个列表,A和B:
A = ['a','b','c']
B = [1, 2]
Run Code Online (Sandbox Code Playgroud)
是否有一种pythonic方法来构建包含2 ^ n的A和B之间的所有映射集(此处为2 ^ 3 = 8)?那是:
[(a,1), (b,1), (c,1)]
[(a,1), (b,1), (c,2)]
[(a,1), (b,2), (c,1)]
[(a,1), (b,2), (c,2)]
[(a,2), (b,1), (c,1)]
[(a,2), (b,1), (c,2)]
[(a,2), (b,2), (c,1)]
[(a,2), (b,2), (c,2)]
Run Code Online (Sandbox Code Playgroud)
使用itertools.product,可以获得所有元组:
import itertools as it
P = it.product(A, B)
[p for p in P]
Run Code Online (Sandbox Code Playgroud)
这使:
Out[3]: [('a', 1), ('a', 2), ('b', 1), ('b', 2), ('c', 1), ('c', 2)]
Run Code Online (Sandbox Code Playgroud) 我有一份清单清单:
mat = [[1,2,3],[4,5,6],[1,2,3],[7,8,9],[4,5,6]]
Run Code Online (Sandbox Code Playgroud)
我想转换成一个集合,即删除重复列表并从中创建一个新列表,它只包含唯一列表.
在上述情况下,所需答案将是
[[1,2,3],[4,5,6],[7,8,9]]
Run Code Online (Sandbox Code Playgroud)
但是当我这样做时set(mat),它给了我错误
TypeError:不可用类型:'list'
你能解决我的问题吗?提前致谢!
我得到一个数据未知的数组.但我只有一个预定义的数据要显示/存储.如何获取初始数组的每个第n个元素并在JavaScript中减少它?
例如:我得到一个大小= 10000的数组,但只能显示n = 2k个元素.
我试着这样:delta = Math.round(10*n/size)/ 10 = 0.2 - >取初始数组的每个第5个元素.
for (i = 0; i < oldArr.length; i++) {
arr[i] = oldArr[i].filter(function (value, index, ar) {
if (index % delta != 0) return false;
return true;
});
}
Run Code Online (Sandbox Code Playgroud)
0.2为0.2总是0,但是有一些其他增量(0.3)它正在工作.对于delta = 0.4,我的工作方式相同,但每一个元素都采用该方法.我该怎么做才能让它发挥作用?
我有一个函数,用于映射数组underscore.这是功能:
var services = _.map(userCopy.get('services'), function (service) {
if (service.service_selected === true) {
return service;
}
});
Run Code Online (Sandbox Code Playgroud)
我的问题是,当conditional === false我,然后我被undefined投入services.当然我可以删除undefined,但这很难看,我只是想map正确使用.我该如何解决?
我已经看过很多像这样的东西了,我正在寻找基本的JavaScript继承的正确解决方案:
function Food(){} // Food constructor (class)
function Bread(){} // Bread constructor (class)
var basicFood = new Food(); // Food classes will inherit from this basicFood instance.
Bread.prototype = basicFood; // Bread now inherits from Food.
var bread = new Bread(); // We create some bread!
bread.constructor == Food; // Though, now we feel very uneasy about how
// the constructor is wrong,
Bread.prototype.constructor = Bread; // So we explicitly set the prototype's constructor
bread = new Bread(); // and …Run Code Online (Sandbox Code Playgroud) 请考虑以下代码段:
> a = [1, undefined, undefined, undefined, 3]
[1, undefined, undefined, undefined, 3]
> b = [1,,,,3]
[1, undefined × 3, 3]
> 1 in a
true
> 1 in b
false
Run Code Online (Sandbox Code Playgroud)
我错过了什么吗?似乎是,根据我undefined在数组中定义元素的方式,in运算符的行为会有所不同.
我需要帮助确定如何解决从嵌套列表中删除"列"以进行修改的问题.
说我有
L = [[1,2,3,4],
[5,6,7,8],
[9,1,2,3]]
Run Code Online (Sandbox Code Playgroud)
我想删除第二列(所以值2,6,1)得到:
L = [[1,3,4],
[5,7,8],
[9,2,3]]
Run Code Online (Sandbox Code Playgroud)
我只是拿出一个专栏来解决如何修改列表的问题.我以前做过类似的事吗?除了我们正在打印它,当然它在这种情况下不起作用,因为我相信中断与列表中我想要的其余值冲突.
def L_break(L):
i = 0
while i < len(L):
k = 0
while k < len(L[i]):
print( L[i][k] , end = " ")
if k == 1:
break
k = k + 1
print()
i = i + 1
Run Code Online (Sandbox Code Playgroud)
那么,你将如何修改这个嵌套列表?我的想法是否正确地将其与我发布的代码进行比较,或者这需要不同的东西吗?
我试图用下划线比较两个对象.
对象1(过滤器)
{
"tuxedoorsuit":"tuxedoorsuit-tuxedo",
"occasions":"occasions-wedding"
}
Run Code Online (Sandbox Code Playgroud)
对象2(属性)
{
"tuxedoorsuit":"tuxedoorsuit-tuxedo",
"occasions":"occasions-wedding",
"occasions":"occasions-prom",
"product_fit":"product_fit-slim",
"colorfamily":"colorfamily-black"
}
Run Code Online (Sandbox Code Playgroud)
我想在对象2中找到对象1的所有项目时返回true.对此最好的下划线方法是什么?
javascript ×5
python ×4
arrays ×3
list ×3
object ×1
oop ×1
prototype ×1
python-3.x ×1
scala ×1
set ×1