enumerate

Understanding pythons enumerate

倖福魔咒の 提交于 2019-12-02 04:21:33
I started to teach myself some c++ before moving to python and I am used to writing loops such as for( int i = 0; i < 20; i++ ) { cout << "value of i: " << i << endl; } moving to python I frequently find myself using something like this. i = 0 while i < len(myList): if myList[i] == something: do stuff i = i + 1 I have read that this isnt very "pythonic" at all , and I actually find myself using this type of code alot whenever I have to iterate over stuff , I found the enumerate function in Python that I think I am supposed to use but I am not sure how I can write similar code using enumerate

python dict function on enumerate object

这一生的挚爱 提交于 2019-12-01 18:53:07
问题 If I have an enumerate object x, why does doing the following: dict(x) clear all the items in the enumerate sequence? 回答1: enumerate creates an iterator. A iterator is a python object that only knows about the current item of a sequence and how to get the next, but there is no way to restart it. Therefore, once you have used a iterator in a loop, it cannot give you any more items and appears to be empty. If you want to create a real sequence from a iterator you can call list on it. stuff =

Quick method to enumerate two big arrays?

三世轮回 提交于 2019-12-01 12:35:25
I have two big arrays to work on. But let's take a look on the following simplified example to get the idea: I would like to find if an element in data1 is matched to an element in data2 and return the array index in both data1 and data2 if a match is found in form of a new array [index of data1, index of data2] . For example, with the below set of data1 and data2 , the program will return: data1 = [[1,1],[2,5],[623,781]] data2 = [[1,1], [161,74],[357,17],[1,1]] expected_output = [[0,0],[0,3]] My current code is as follow: result = [] for index, item in enumerate(data1): for index2,item2 in

Quick method to enumerate two big arrays?

本秂侑毒 提交于 2019-12-01 10:09:53
问题 I have two big arrays to work on. But let's take a look on the following simplified example to get the idea: I would like to find if an element in data1 is matched to an element in data2 and return the array index in both data1 and data2 if a match is found in form of a new array [index of data1, index of data2] . For example, with the below set of data1 and data2 , the program will return: data1 = [[1,1],[2,5],[623,781]] data2 = [[1,1], [161,74],[357,17],[1,1]] expected_output = [[0,0],[0,3]

Serial port enumeration in Delphi using SetupDiGetClassDevs

人盡茶涼 提交于 2019-11-30 16:08:19
I'm trying to enumerate "friendly names" for COM ports. The ports may dynamically change as USB-serial devices are connected and disconnected at runtime. Based on the possible methods described in this question , I am attempting to use the SetupDiGetClassDevs method. I found this example code , but it is written for an older version of the setupapi unit (the original link to homepages.borland.com doesn't work of course). I tried using the setupapi unit from the current JVCL( JVCL340CompleteJCL221-Build3845 ), but it doesn't seem to be compatible with Delphi 7. I get compiler errors: if

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

霸气de小男生 提交于 2019-11-30 04:54:54
This question already has an answer here: How to get line count cheaply in Python? 37 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 logs and we want to do a monthly analysis, so the code will have to process 30 logs of ~15GB - more than one and a half

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

空扰寡人 提交于 2019-11-29 16:53: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 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 answers are quite fast for thousands of data points, they start to break down after that. My potato segfaults

Serial port enumeration in Delphi using SetupDiGetClassDevs

余生长醉 提交于 2019-11-29 15:40:50
问题 I'm trying to enumerate "friendly names" for COM ports. The ports may dynamically change as USB-serial devices are connected and disconnected at runtime. Based on the possible methods described in this question, I am attempting to use the SetupDiGetClassDevs method. I found this example code, but it is written for an older version of the setupapi unit (the original link to homepages.borland.com doesn't work of course). I tried using the setupapi unit from the current JVCL

Enumerate plots in matplotlib figure

吃可爱长大的小学妹 提交于 2019-11-29 13:52:34
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. 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 passed to `annotate` Parameters ---------- fig : Figure Figure object to work on labels : iterable or None

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

限于喜欢 提交于 2019-11-29 11:35:00
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 present during the EnumerateFiles()? Even worse: what happens if files are removed during iterations: will