Tag: 循环

迭代通过HashMap

可能重复: 如何迭代集合映射中的每个条目? 迭代HashMap的项目的最佳方法是什么?

在Python中为'循环访问索引

如何访问索引本身的列表如下? ints = [8, 23, 45, 12, 78] 当我循环使用for循环时,如何访问循环索引,在这种情况下从1到5?

PHP的foreach实际上是如何工作的?

让我先说这个,我知道什么是foreach ,是否和如何使用它。 这个问题涉及到它是如何在引擎盖下工作的,我不希望有任何答案,“这是如何循环使用foreach的数组”。 很长一段时间,我认为foreach与数组本身一起工作。 然后,我发现它提供了许多与该数组副本一起工作的事实,而我从此认为这是故事的结尾。 但是最近我就这个问题进行了讨论,经过一番小小的实验,发现事实上并不是100%的事实。 让我表明我的意思。 对于以下testing用例,我们将使用以下数组: $array = array(1, 2, 3, 4, 5); testing案例1 : foreach ($array as $item) { echo "$item\n"; $array[] = $item; } print_r($array); /* Output in loop: 1 2 3 4 5 $array after loop: 1 2 3 4 5 1 2 3 4 5 */ 这清楚地表明我们不直接使用源数组 – 否则循环会一直持续下去,因为我们在循环过程中不断地将项目推到数组上。 […]

++ i和i ++有什么区别?

在C中,使用++i和i++什么区别,哪些应该用在for循环的增量块中?

如何在JavaScript循环中添加延迟?

我想在while循环中添加一个延迟/睡眠: 我试过这样的: alert('hi'); for(var start = 1; start < 10; start++) { setTimeout(function () { alert('hello'); }, 3000); } 只有第一种情况是正确的:在显示alert('hi') ,它将等待3秒,然后alert('hello')将会显示,但是alert('hello')将会不断地重复。 我希望在alert('hi')后3秒钟显示alert('hello')之后,第二次alert('hello')需要等待3秒钟,等等。 任何人都可以请指教?

遍历对象属性

var obj = { name: "Simon", age: "20", clothing: { style: "simple", hipster: false } } for(var propt in obj){ alert(propt + ': ' + obj[propt]); } variablespropt如何表示对象的属性? 这不是一个内置的方法或属性。 那么为什么它会提出对象中的每个属性?

什么是在块中迭代列表的最“pythonic”方式?

我有一个Python脚本,需要input一个整数列表,我需要一次处理四个整数。 不幸的是,我没有控制input,或者我将它作为四元组元素列表传入。 目前,我正在这样迭代它: for i in xrange(0, len(ints), 4): # dummy op for example code foo += ints[i] * ints[i + 1] + ints[i + 2] * ints[i + 3] 它看起来很像“C-think”,这让我怀疑有一种更为pythonic的方式来处理这种情况。 该列表在迭代后被丢弃,所以不需要保存。 也许像这样会更好? while ints: foo += ints[0] * ints[1] + ints[2] * ints[3] ints[0:4] = [] 尽pipe如此,仍然不太“感觉”正确。 : – / 相关问题: 如何在Python中将列表分成均匀大小的块?

如何循环或枚举JavaScript对象?

我有一个JavaScript对象,如下所示: var p = { "p1": "value1", "p2": "value2", "p3": "value3" }; 现在我想遍历所有p元素( p1 , p2 , p3 …)并获得它们的键和值。 我怎样才能做到这一点? 如有必要,我可以修改JavaScript对象。 我的最终目标是循环通过一些关键值对,如果可能的话,我想避免使用eval 。

对于JavaScript中的每个数组?

我怎样才能循环使用JavaScript中的数组中的所有条目? 我以为是这样的: forEach(instance in theArray) arrays是我的arrays,但这似乎是不正确的。

要求用户input,直到他们提供有效的答复

我正在编写一个程序,必须接受来自用户的input。 #note: Python 2.7 users should use `raw_input`, the equivalent of 3.X's `input` age = int(input("Please enter your age: ")) if age >= 18: print("You are able to vote in the United States!") else: print("You are not able to vote in the United States.") 如果用户input合理的数据,这将按预期工作。 C:\Python\Projects> canyouvote.py Please enter your age: 23 You are able […]