可能重复:
在Javascript数组中测试值在JavaScript数组
中查找项目的最佳方法?
Javascript - array.contains(obj)
我通常在python中编程,但最近开始学习JavaScript.
在python中,这是一个完全有效的if语句:
list = [1,2,3,4]
x = 3
if x in list:
print "It's in the list!"
else:
print "It's not in the list!"
Run Code Online (Sandbox Code Playgroud)
但是我在Javascript中做了同样的事情.
如何在JavaScript中检查x是否在列表中?
我之前使用过pygame和python 2.7,但最近我'升级'到python 3.2.我下载并安装了Pygame的最新版本,据说可以使用这个版本的python.但是,我有一个相当令人沮丧的错误,应该是一个简单的代码块.代码是:
import pygame, random
title = "Hello!"
width = 640
height = 400
pygame.init()
screen = pygame.display.set_mode((width, height))
running = True
clock = pygame.time.Clock()
pygame.display.set_caption(title)
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.quit():
running = False
else:
print(event.type)
clock.tick(240)
pygame.quit()
Run Code Online (Sandbox Code Playgroud)
每次我运行它我得到:
17
1
4
Traceback (most recent call last):
File "C:/Users/David/Desktop/hjdfhksdf.py", line 15, in <module>
for event in pygame.event.get():
pygame.error: video system not initialized
Run Code Online (Sandbox Code Playgroud)
为什么我收到此错误?
几天前,当我们为变量$ x分配一个字符串时,我和朋友正在玩Chrome中的Javascript控制台(使用稍微旧的版本,但这可以在OSX和Windows上的最新稳定版本中重复) .
$x = "hello"
Run Code Online (Sandbox Code Playgroud)
但是当我们回显$ x的值时,我们在控制台中得到以下代码:
bound: function (xpath, context)
{
var doc = (context && context.ownerDocument) || inspectedWindow.document;
var result = doc.evaluate(xpath, context || doc, null, XPathResult.ANY_TYPE, null);
switch (result.resultType) {
case XPathResult.NUMBER_TYPE:
return result.numberValue;
case XPathResult.STRING_TYPE:
return result.stringValue;
case XPathResult.BOOLEAN_TYPE:
return result.booleanValue;
default:
var nodes = [];
var node;
while (node = result.iterateNext())
nodes.push(node);
return nodes;
}
}
Run Code Online (Sandbox Code Playgroud)
我们在Safari和Firefox的稳定版本中获得了类似的输出.据我们所知,$ x变量未附加到全局窗口对象.
什么是$ x,它用于什么?
我正在尝试用Python编写数据库抽象层,它允许您使用链式函数调用来构造SQL语句,例如:
results = db.search("book")
.author("J. K. Rowling")
.price("<40.00")
.title("Harry")
.execute()
Run Code Online (Sandbox Code Playgroud)
但是当我尝试将所需方法动态添加到db类时,我遇到了问题.
以下是我的代码的重要部分:
import inspect
def myName():
return inspect.stack()[1][3]
class Search():
def __init__(self, family):
self.family = family
self.options = ['price', 'name', 'author', 'genre']
#self.options is generated based on family, but this is an example
for opt in self.options:
self.__dict__[opt] = self.__Set__
self.conditions = {}
def __Set__(self, value):
self.conditions[myName()] = value
return self
def execute(self):
return self.conditions
Run Code Online (Sandbox Code Playgroud)
但是,当我运行例如:
print(db.search("book").price(">4.00").execute())
Run Code Online (Sandbox Code Playgroud)
输出:
{'__Set__': 'harry'}
Run Code Online (Sandbox Code Playgroud)
我是以错误的方式来做这件事的吗?有没有更好的方法来获取被调用函数的名称或以某种方式制作函数的"硬拷贝"?
我通常使用python编写脚本/程序,但最近开始使用JavaScript进行编程,并且在使用数组时遇到了一些问题.
在python中,当我创建一个数组并在y中使用x时,我得到了这个:
myarray = [5,4,3,2,1]
for x in myarray:
print x
Run Code Online (Sandbox Code Playgroud)
我得到了预期的输出:
5
4
3
..n
Run Code Online (Sandbox Code Playgroud)
但我的问题是,当使用Javascript时,我得到一个不同的,完全出乎意料的(对我而言)结果:
var world = [5,4,3,2,1]
for (var num in world) {
alert(num);
}
Run Code Online (Sandbox Code Playgroud)
我得到了结果:
0
1
2
..n
Run Code Online (Sandbox Code Playgroud)
如何让JavaScript输出num作为数组中的值,如python,为什么会发生这种情况?
我在python中工作,目前有以下代码:
list = []
for a in range(100):
for b in range(100):
for c in range(100):
list.append(run(a,b,c))
Run Code Online (Sandbox Code Playgroud)
run(a,b,c)返回一个整数(例如,它可以将这三个数相乘).是否有更快的方法来循环这些数字或使用地图功能?
谢谢 :)
我正在尝试创建一个函数,它将加载大量图像并将它们映射到PyGame中的相应名称.我对python并不是那么好,这真让我陷入困境.我目前的代码是这样的:
tile1 = pygame.image.load("/one.bmp")
tile2 = pygame.image.load("/two.bmp")
tile3 = pygame.image.load("/three.bmp")
Run Code Online (Sandbox Code Playgroud)
并且它继续进行大约20个瓷砖.事情是我发现我需要更多,并想知道如何使用for x in y循环来做到这一点.我的基本想法是:
tile = ['/one.bmp', '/two.bmp', '/three.bmp']
tilelist = [1,2,3]
for tile in tile:
tilelist[x] = pygame.image.load(tile)
Run Code Online (Sandbox Code Playgroud)
或类似的东西,但我不是那里.我也想知道是否可以使用词典来完成.
任何帮助将不胜感激,谢谢:)