小编Reg*_*ser的帖子

为什么"this"在javascript中引用forEach中的Window?

如果我运行此代码,则窗口对象将打印到控制台.

var arr= [1,2,34,5,6,7,7,8];
arr.forEach(function(e){
console.log(this);
});
Run Code Online (Sandbox Code Playgroud)

为什么它不引用数组对象中的arr对象或特定项?我想了解它背后的原因,就像发生了什么.this使用by new或调用此函数的对象来定义,对吗?

javascript arrays

3
推荐指数
1
解决办法
1575
查看次数

Java:删除数组中某个项的所有实例

假设我创建一个数组,设置值如下:

double[] exampleArray = {10.0, 3.0, 0.0, 0.0, 0.0};
Run Code Online (Sandbox Code Playgroud)

如何0.0从阵列中删除所有的,只留下10.0和3.0并将数组长度缩短为2?

本网站上的其他问题涉及HashSetsCollections.有没有办法没有导入其他东西?

java arrays

3
推荐指数
1
解决办法
641
查看次数

Python:返回列表不起作用

我正在尝试制作一个程序,可以在一个类中添加/删除/显示学生,这5个类是列表中的5个列表.

非常感谢帮助.

当我运行此代码时:

global classes
def intro():
    print("Welcome to Powerschool v2.0!")
    print("Actions:")
    print("1. Add Student")
    print("2. Delete Student")
    print("3. Show Students in a Class")
    print("4. Show All Students")
    x = int(input())
    while x<1 or x>4:
        print ("Please choose an action, 1-4.")
        x = int(input())
    if x == 1:
        action1()
    elif x == 2:
        action2()
    elif x == 3:
        action3()
    elif x == 4:
        action4()
    classes = [[],[],[],[],[]]
    return classes
def action1():
    print("Which Class? 1-5")
    a = int(input())
    print("Please enter the …
Run Code Online (Sandbox Code Playgroud)

python python-3.4

1
推荐指数
1
解决办法
179
查看次数

列出索引超出范围和随机数以选择列表中的项目

我需要使用字符串制作一个iPhone模型列表.split().

这不是问题,但我还必须使用0-9中的随机数来挑选一个单词,然后使用while/for循环显示3个随机单词.

在我的代码中,当我输入:

import random

iPhone = 'Original 3G 3GS 4 4S 5 5C 5S 6 6Plus'.split()

z = 0
while z < 4:
    for y in range (1,3):
        for x in iPhone:
            x = random.randint(0,10)
            print (iPhone[x])
Run Code Online (Sandbox Code Playgroud)

它说:

 Traceback (most recent call last):
      File "C:\Users\zteusa\Documents\AZ_wordList2.py", line 15, in <module>
        print (iPhone[x])
    IndexError: list index out of range
Run Code Online (Sandbox Code Playgroud)

我不确定是什么造成的.

python random split list

0
推荐指数
1
解决办法
1067
查看次数

标签 统计

arrays ×2

python ×2

java ×1

javascript ×1

list ×1

python-3.4 ×1

random ×1

split ×1