iteration

Why does the generic Dictionary in .NET not provide a ForEach() method?

廉价感情. 提交于 2021-01-28 05:22:02
问题 After a couple hours of research (on MSDN websites and so on) I didn't manage to find out why the generic Dictionary<TKey, TValue> does not provide a ForEach() method like List<T> does. Could someone please give me an explanation? (I know that it's not hard to implement it as an extension method, a great example can be seen here, I just was wondering whether there might be a particular reason why it's not provided by the .NET libraries in the first place.) Thanks in advance. 回答1: Because it's

for loop in Javascript

烂漫一生 提交于 2021-01-28 05:11:12
问题 for (let x of a) does not work in IE11. Can I replace it by for (let x in a) ? for (let key in a) { s += key + ": " + a[key]; s += "<br />"; } for (let key of a) { s += key + ": " + a[key]; s += "<br />"; } 回答1: for...of is not supported in IE11 yet. You can use for..in to iterate over as it has the support from IE6. If you just want to add the key and value , you can use Object.keys and build the string that is needed. let f = ''; let s = ''; let a = { firstName: 'Hello', lastName: 'JS',

Why does python think my array is 0-d? (TypeError: iteration over a 0-d array) [duplicate]

时间秒杀一切 提交于 2021-01-28 04:52:45
问题 This question already has answers here : how to read array of numbers from text file in python (2 answers) Closed 4 years ago . I'm aware that this may have been answered before but please check that other answers are relevant to this instance! I'm writing an array to a file as a string so that it can be stored when my script isn't running and be accessed again easily when run. When I then read this string out of the file again it is automatically read as a string. I can fix this with a for

Efficient way to extract and collect a random subsample of a generator in Julia

此生再无相见时 提交于 2021-01-28 02:45:37
问题 Consider a generator in Julia that if collected will take a lot of memory g=(x^2 for x=1:9999999999999999) I want to take a random small subsample (Say 1%) of it, but I do not want to collect() the object because will take a lot of memory Until now the trick I was using was this temp=collect((( rand()>0.01 ? nothing : x ) for x in g)) random_sample= temp[temp.!=nothing] But this is not efficient for generators with a lot of elements, collecting something with so many nothing elements doesnt

How to take the last value of a for loop in Java?

风流意气都作罢 提交于 2021-01-28 02:36:43
问题 import java.util.Scanner; public class Problem1{ public static void main(String[] args){ //input Scanner kb = new Scanner(System.in); String word,letter; int counter=0, match,value; word=kb.next(); word=word.toLowerCase(); letter=kb.next(); letter=letter.toLowerCase(); //loop for (int i=0;i<word.length();i++) if (word.charAt(i)==letter.charAt(0)){ counter++; match=i; System.out.print(match); } if (counter==0) System.out.print(-1); } } I must execute this program in Codio. This program will

How efficient is Python's 'in' or 'not in' operators?

狂风中的少年 提交于 2021-01-28 00:21:54
问题 I have a list of over 100000 values and I am iterating over these values and checking if each one is contained in another list of random values (of the same size). I am doing this by using if item[x] in randomList . How efficient is this? Does python do some sort of hashing for each container or is it internally doing a straight up search of the other container to find the element I am looking for? Also, if it does this search linearly, then does it create a dictionary of the randomList and

Iterating over Arrays in Javascript

亡梦爱人 提交于 2021-01-27 06:53:35
问题 I am a JavaScript newbie. I'm trying to practice some sample JavaScript problems. I'm a little stuck when it comes to this question about iterating over arrays. Can anyone point me in the right direction? I am trying to take the values in oldArray , add 5 to each of them, and store in newArray . var oldArray = [12, 45, 6, 23, 19, 20, 20, 15, 30, 42]; var newArray = []; function plusFive(oldArray[i]) { for (var i = 0; i < oldArray.length; i++) { newArray.push(oldArray[i]) + 5) }; } } 回答1: Bug

Iterate over combinations of items of multiple lists in Python

。_饼干妹妹 提交于 2021-01-27 06:31:14
问题 I have several lists and I need to do something with each possible combination of these list items. In the case of two lists, I can do: for a in alist: for b in blist: # do something with a and b However, if there are more lists, say 6 or 7 lists, this method seems reluctant. Is there any way to elegantly implement this iteration? 回答1: You could use itertools.product to make all possible combinations from your lists. The result will be one long list of tuple with an element from each list in

Iterate over combinations of items of multiple lists in Python

北城余情 提交于 2021-01-27 06:30:19
问题 I have several lists and I need to do something with each possible combination of these list items. In the case of two lists, I can do: for a in alist: for b in blist: # do something with a and b However, if there are more lists, say 6 or 7 lists, this method seems reluctant. Is there any way to elegantly implement this iteration? 回答1: You could use itertools.product to make all possible combinations from your lists. The result will be one long list of tuple with an element from each list in

Understanding Java Iterator

故事扮演 提交于 2021-01-20 07:16:46
问题 If I run the following code, it will print out 3 times duplicate, but when I remove the if statement inside the while loop (just to see how many times it will iterate) it starts an infinite loop. How does actually this hasNext() method working? I thought that will iterate only 5 times as I have 5 items in the list. public class ExerciseOne { public static void main(String []args){ String []colors = {"MAGENTA","RED","WHITE","BLUE","CYAN"}; List<String> list = new ArrayList<String>(); for