for-loop

Python: Finding Longest/Shortest Words In a List and Calling Them in a Function

南笙酒味 提交于 2020-01-12 08:39:19
问题 I have a list of words: words=["alpha","omega","up","down","over","under","purple","red","blue","green"] I have two functions that are supposed to find the shortest and longest words in this list: def bigWords(list=[], *args): largestWord="" largestLen=0 for word in list: if largestWord<len(word): largestWord=len(word) largestWord=word print "The longest word(s) in the list is %s." % largestWord def smallWords(list=[], *args): smallestWord="" smallestLen=0 for word in list: if smallestLen>len

Python: Finding Longest/Shortest Words In a List and Calling Them in a Function

末鹿安然 提交于 2020-01-12 08:39:01
问题 I have a list of words: words=["alpha","omega","up","down","over","under","purple","red","blue","green"] I have two functions that are supposed to find the shortest and longest words in this list: def bigWords(list=[], *args): largestWord="" largestLen=0 for word in list: if largestWord<len(word): largestWord=len(word) largestWord=word print "The longest word(s) in the list is %s." % largestWord def smallWords(list=[], *args): smallestWord="" smallestLen=0 for word in list: if smallestLen>len

Catching exceptions out of 'stream()' or 'parallelStream()' loses correct values

会有一股神秘感。 提交于 2020-01-12 07:41:18
问题 In the following code, when catching NumberFormatException out of for iteration, the strings in appropriate form appearing in strList before the first bad one (i.e., "illegal_3" ) have been parsed successfully (i.e., "1" and "2" have been parsed as integers 1 and 2 ). public void testCaughtRuntimeExceptionOutOfIteration() { List<String> strList = Stream.of("1", "2", "illegal_3", "4", "illegal_5", "6").collect(Collectors.toList()); List<Integer> intList = new ArrayList<>(); try{ for (String

addEventListener, for(), index. how to use closure? [duplicate]

谁说胖子不能爱 提交于 2020-01-12 07:10:34
问题 This question already has answers here : JavaScript closure inside loops – simple practical example (44 answers) Javascript infamous Loop issue? [duplicate] (5 answers) Closed 6 years ago . I have this code: var items = this.llistat.getElementsByTagName('a'); for( var i = 0; i < items.length; i++ ){ items[i].addEventListener('click', function(event) { alert( i ); }, items[i]); } where the event is listened, but there are 3 items and the alert allways print 3 on any of the elements (it doesn't

Selenium webdriver window handles c# switchTo failed

纵然是瞬间 提交于 2020-01-12 05:50:08
问题 Here comes 2 windows pop out during the testing. my code: string BaseWindow = driver.CurrentWindowHandle; ReadOnlyCollection<string> handles = driver.WindowHandles; foreach(string handle in handles) { Boolean a = driver.SwitchTo().Window(handle).Url.Contains("Main"); if (a == true) { InitialSetting.driver.SwitchTo().Window(handle); break; } } I want to switch to the window which url contains "Main". But when the test is running, it switches between two windows continuously and it doesn't stop

How do I rename a data-frame in a for-loop

杀马特。学长 韩版系。学妹 提交于 2020-01-11 15:42:07
问题 I am very new to programming with R, but I am trying to read in multiple files for a directory and give them each a unique name. I am reading the files in using the Dendrochronology Program Library in R (package dpIR) and the read.tucson function. Although I'm using a specific package, I think my question is fairly general: Within the loop, I want to create files by concatenating a "t" with each individual files name. So, if I have a file named "2503" in my directory, I want to create a

PHP foreach loop to create JSON from mysql

ε祈祈猫儿з 提交于 2020-01-11 14:47:33
问题 I have a many to one problem, I have data organized in mysql like: >order1 : mycustomer : item1 >order1 : mycustomer : item2 >order2 : mycustomer : item3 >order2 : mycustomer : item1 >order3 : mycustomer : item2 I want to create the JSON something like (for explanation purposes) >order1 mycustomer >> item1 >> item2, >order2 mycustomer >> item3 >> item1, >order3 mycustomer >> item2 But my looping is not correct, I am not getting the order with item array then repeat for next order. What am I

Get the two closest points from a list of points

跟風遠走 提交于 2020-01-11 14:30:10
问题 I am given a list of integers/floats and I need to find the two numbers closest together. How would I do that using only nested for loops? 回答1: For each element, you have to compare the distance of it to each of the other elements with your previous "closest" value - any time this comparison yields smaller values, you remember that pair as the "two closest" ones. So, it is straightforward: def find_two_closest(numbers): # most distant points: delta = max(numbers), min(numbers) for i, element

difference between for..in and for loops, and counter declaration

百般思念 提交于 2020-01-11 13:52:06
问题 the difference (speed, performace, side effects, ...) between implementations of the for loop: between var i; for(i = 0; i < length; i++){ //Do something} // more code and for(var i = 0; i < length; i++){ //Do something} // more code and for(i = 0; i < length; i++){ //Do something} // more code and between var e; for( e in array){ //Do something} // more code and for(var e in array){ //Do something} // more code and for(e in array){ //Do something} // more code 回答1: There is no difference.

How to loop through a text file and find the matching keywords in Python3

99封情书 提交于 2020-01-11 13:30:29
问题 I am working on a project to define a search function in Python3. Goal is to output the keywords from a list and the sentence(s) from adele.txt that contain(s) the keywords. This is a user defined list, userlist=['looking','for','wanna'], adele.txt is on the github page, https://github.com/liuyu82910/search Below is my function. The first loop is to get all the lines in lowercase from adele.txt, second loop to get the each word in lowercase in userlist. My code is not looping correctly. What