enumerate

Is there a way of recover from an Exception in Directory.EnumerateFiles?

百般思念 提交于 2019-11-29 10:52:56
In .NET 4, there's this Directory.EnumerateFiles() method with recursion that seems handy. However, if an Exception occurs within a recursion, how can I continue/recover from that and continuing enumerate the rest of the files? try { var files = from file in Directory.EnumerateFiles("c:\\", "*.*", SearchOption.AllDirectories) select new { File = file }; Console.WriteLine(files.Count().ToString()); } catch (UnauthorizedAccessException uEx) { Console.WriteLine(uEx.Message); } catch (PathTooLongException ptlEx) { Console.WriteLine(ptlEx.Message); } I did found a solution to this. By using a stack

Does Kotlin have an “enumerate” function like Python?

痴心易碎 提交于 2019-11-29 05:47:00
In Python I can write: for i, element in enumerate(my_list): print i # the index, starting from 0 print element # the list-element How can I write this in Kotlin? There is a forEachIndexed function in the standard library: myList.forEachIndexed { i, element -> println(i) println(element) } See @s1m0nw1's answer as well, withIndex is also a really nice way to iterate through an Iterable . Iterations in Kotlin: Some Alternatives Like already said, forEachIndexed is a good way to iterate. Alternative 1: the extension withIndex , defined for Iterable types, can be used in for -each: val ints =

Enumerating files in an embedded resource directory

随声附和 提交于 2019-11-28 20:54:42
Is there a way, in WPF, to enumerate through all files within a specific embedded resource directory? That is, a directory of items all having a "Build Action" set to "Resource". Charles The resources are compiled into a resource stream named YourAssemblyName.g.resources . So, we load up this stream which appears to be a dictionary where the key is the resource name and the value is the resource data. We are interested in the resource name as that is (usually) the original folder and file name for the resource. We then filter out those keys that begin with the folder we are interested in.

For every point in an array, find the closest point to it in a second array and output that index

不羁的心 提交于 2019-11-28 11:37:08
问题 If I have two arrays: X = np.random.rand(10000,2) Y = np.random.rand(10000,2) How can I, for each point in X, find out which point in Y is closest to it? So that in the end I have an array showing: x1_index y_index_of_closest 1 7 2 54 3 3 ... ... I want to do this for both columns in X and compare each to each column and value in Y 回答1: This question is pretty popular. Since similar questions keep getting closed and linked here, I think it's worth pointing out that even though the existing

Enumerate plots in matplotlib figure

依然范特西╮ 提交于 2019-11-28 07:43:20
问题 In a matplotlib figure I would like to enumerate all (sub)plots with a), b), c) and so on. Is there a way to do this automatically? So far I use the individual plots' titles, but that is far from ideal as I want the number to be left aligned, while an optional real title should be centered on the figure. 回答1: import string from itertools import cycle from six.moves import zip def label_axes(fig, labels=None, loc=None, **kwargs): """ Walks through axes and labels each. kwargs are collected and

How do I enumerate() over a list of tuples in Python?

老子叫甜甜 提交于 2019-11-28 06:43:35
I've got some code like this: letters = [('a', 'A'), ('b', 'B')] i = 0 for (lowercase, uppercase) in letters: print "Letter #%d is %s/%s" % (i, lowercase, uppercase) i += 1 I've been told that there's an enumerate() function that can take care of the "i" variable for me: for i, l in enumerate(['a', 'b', 'c']): print "%d: %s" % (i, l) However, I can't figure out how to combine the two: How do I use enumerate when the list in question is made of tuples? Do i have to do this? letters = [('a', 'A'), ('b', 'B')] for i, tuple in enumerate(letters): (lowercase, uppercase) = tuple print "Letter #%d is

What happens with Directory.EnumerateFiles if directory content changes during iteration?

偶尔善良 提交于 2019-11-28 04:40:44
问题 I've read discussions about difference between Directory.EnumerateFiles and Directory.GetFiles(). I understand that internally they both use System.IO.FileSystemEnumerableFactory.CreateFileNameIterator() The difference is that EnumerateFiles might use deferred execution (lazy), while GetFiles() does a ToArray, so the function is already executed. But what happens if files and folders are added to the dictionary during the iteration. Will the iteration only iterate over the items that were

Is there a way of recover from an Exception in Directory.EnumerateFiles?

荒凉一梦 提交于 2019-11-28 04:28:57
问题 In .NET 4, there's this Directory.EnumerateFiles() method with recursion that seems handy. However, if an Exception occurs within a recursion, how can I continue/recover from that and continuing enumerate the rest of the files? try { var files = from file in Directory.EnumerateFiles("c:\\", "*.*", SearchOption.AllDirectories) select new { File = file }; Console.WriteLine(files.Count().ToString()); } catch (UnauthorizedAccessException uEx) { Console.WriteLine(uEx.Message); } catch

(Python) Counting lines in a huge (>10GB) file as fast as possible [duplicate]

不想你离开。 提交于 2019-11-27 13:51:47
问题 This question already has an answer here: How to get line count of a large file cheaply in Python? 38 answers I have a really simple script right now that counts lines in a text file using enumerate() : i = 0 f = open("C:/Users/guest/Desktop/file.log", "r") for i, line in enumerate(f): pass print i + 1 f.close() This takes around 3 and a half minutes to go through a 15GB log file with ~30 million lines. It would be great if I could get this under two minutes or less, because these are daily

Enumerating files in an embedded resource directory

人盡茶涼 提交于 2019-11-27 13:18:02
问题 Is there a way, in WPF, to enumerate through all files within a specific embedded resource directory? That is, a directory of items all having a "Build Action" set to "Resource". 回答1: The resources are compiled into a resource stream named YourAssemblyName.g.resources . So, we load up this stream which appears to be a dictionary where the key is the resource name and the value is the resource data. We are interested in the resource name as that is (usually) the original folder and file name