foreach

In Python, is it better to use list comprehensions or for-each loops?

孤街浪徒 提交于 2020-01-26 11:47:54
问题 Which of the following is better to use and why? Method 1: for k, v in os.environ.items(): print "%s=%s" % (k, v) Method 2: print "\n".join(["%s=%s" % (k, v) for k,v in os.environ.items()]) I tend to lead towards the first as more understandable, but that might just be because I'm new to Python and list comprehensions are still somewhat foreign to me. Is the second way considered more Pythonic? I'm assuming there's no performance difference, but I may be wrong. What would be the advantages

Swift - Block loop from continuing until download operation finishes

余生颓废 提交于 2020-01-26 03:12:22
问题 I am currently trying to download multiple files upon user request for a game. My goal is to have it download only one file at a time and stop executing code until that file has finished downloading. My code is an array of JSON objects, in each contains a path for where to download them to(path) and the URL of the file. I am looping through the array and using AlamoFire to download them with a .downloadProgress closure. Alamofire.download( json["url"].stringValue, method: .get, to:

Error in PHP Generator

旧街凉风 提交于 2020-01-25 13:08:13
问题 What is the best way to inform who use my generator function if something errors occurs, instead of writing weird return or raising exception like this piece of code function csv_file_generator($csvFilename, $delimiter = ";", $enclousure = '"') { if(($csvHandler = fopen($csvFilename, 'rb')) === false) { return; } while (($row = fgetcsv($csvHandler, 0, $delimiter, $enclousure)) !== false) { yield $row; } if (feof($csvHandler) === false) { return; } if (fclose($csvHandler) === false) { return;

A foreach loop in a while loop

懵懂的女人 提交于 2020-01-25 13:05:20
问题 I'm trying to convert wordpress tags (and other input) to html classes. First I query the posts, set them in a while loop and in this while loop I convert the tags to usefull classes. I've got this right now: <?php while ($query->have_posts()) : $query->the_post(); $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { $thetags = $tag->name . ''; echo $the_tags; $thetags = strtolower($thetags); $thetags = str_replace(' ','-',$thetags); echo $thetags; } } ?> <!-- Loop posts

A foreach loop in a while loop

假如想象 提交于 2020-01-25 13:05:10
问题 I'm trying to convert wordpress tags (and other input) to html classes. First I query the posts, set them in a while loop and in this while loop I convert the tags to usefull classes. I've got this right now: <?php while ($query->have_posts()) : $query->the_post(); $posttags = get_the_tags(); if ($posttags) { foreach($posttags as $tag) { $thetags = $tag->name . ''; echo $the_tags; $thetags = strtolower($thetags); $thetags = str_replace(' ','-',$thetags); echo $thetags; } } ?> <!-- Loop posts

Getting a String value from NSOrderedSet using SwiftUI ForEach

独自空忆成欢 提交于 2020-01-25 08:34:11
问题 Using this question/answer I am able to use ForEach to use a NSOrderedSet created from a One to Many relationship in CoreData, however I can't seem to access a String Attribute stored in the Core Data entity. I have two CoreData entities: Client & SessionNote. Clients can have many SessionNotes, the NSOrderedSet of clientNotes, and SessionNote can only have one Client. Client+CoreDataClass.swift: public class Client: NSManagedObject { } Client+CoreDataProperties.swift: extension Client {

Invalid argument supplied for foreach! [duplicate]

戏子无情 提交于 2020-01-25 07:15:27
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Invalid argument supplied for foreach() I have the following code: <? foreach($format as $form) { echo $form; ?> <ul> <? $s = $database->onlineFormatUsers($form); while($row=mysql_fetch_assoc($s)) { $username=$row['username']; $id=$row['id'];?> <li><a href="../userprofile.php?id=<?echo $id?>"><?echo "$username";?></a></li> <? } ?> </ul> <? } ?> <? //the active formats $f = $database->activeFormats(); while($row

How to loop a data in php [duplicate]

人走茶凉 提交于 2020-01-25 06:51:33
问题 This question already has an answer here : Loop results based on category but only show category once (1 answer) Closed 2 months ago . I want to loop my data Like this Like this Not like this My Controller: function bills($patient_id){ $data['payments'] = $this->model_patient->getpayments($patient_id); $this->load->view('includes/header'); $this->load->view('billing', $data); $this->load->view('includes/footer'); } My Model: // joining my 3 tables, i want to display the procedures made by

Mysql Foreach from one table to another

元气小坏坏 提交于 2020-01-25 03:57:04
问题 I have 2 tables, First table is user and second table is user_attendance. In the user attendance I'm inserting IN and OUT per line, so 1 line for IN and 1 line for OUT. I want a MySQL select(so I don't have to do it on Php) which will loop for every ID in users and get the last OUT and first IN of that user in user table. I tried doing it on Php by selecting first all the ID in user and looping it Via php and Select the 1st in and Last out of that Id in attendance. Like so. $userCreds = DB:

adding items to columns/rows in listview using foreach

∥☆過路亽.° 提交于 2020-01-25 03:46:05
问题 I am into day 5 of learning c#, and am trying to figure out how to fill/re-fill a ListView control, containing 10 rows and 12 columns, using a foreach loop. I have coded the functionality I'm after in C. void listPopulate(int *listValues[], int numberOfColumns, int numberOfRows) { char table[100][50]; for (int columnNumber = 0; columnNumber < numberOfColumns; ++columnNumber) { for (int rowNumber = 0; rowNumber < numberOfRows; ++rowNumber) { sprintf(&table[columnNumber][rowNumber], "%d",