enumerate

List comprehension behavior in python

我怕爱的太早我们不能终老 提交于 2019-12-13 04:15:25
问题 I am working with Codeskulptor on a rock collision problem. I want to check collisions between rocks and my rocks are in a list. I came up with the solution to build a list of combinations and then check for collision. I do not have itertools available. My combination list was created like this: def combinations(items): n_items = [(n,item) for n,item in enumerate(items)] return [(item,item2) for n,item in n_items for m,item2 in n_items[n:] if n != m] letters = ['A','B','C','D'] print

Does enumerate create a copy of its argument?

守給你的承諾、 提交于 2019-12-12 11:15:10
问题 If I use enumerate while iterating through a very large list of graph clusters, I want to make sure I'm not unnecessarily creating any copies of this list in memory. I've been trying to confirm that it will not create any copies, but would like to know for sure. for i, cluster in enumerate(c): # code that does stuff with i and cluster 回答1: No, it doesn't. It lazily iterates the iterable you pass in while the loop executes. 来源: https://stackoverflow.com/questions/23015133/does-enumerate-create

Swift enumerate functions. How does it work behind?

荒凉一梦 提交于 2019-12-12 09:29:13
问题 The enumerate function returns a tuple for each item in the array composed of the index and the value for that item. The map function returns an array of elements built from the results of applying a provided transforming closure for each element in the array. Declaration : func map<U>(transform: (T) -> U) -> [U] var numbers = [1, 2, 3] numbers = map(numbers) { (index, element) in index + element } //[1, 3, 5] That is good. Works. var numbers = [1, 2, 3] var result = map(enumerate(numbers)) {

C++ builder - enumerate components on TPanel

两盒软妹~` 提交于 2019-12-11 18:42:39
问题 It is quite simple to enumerate Form components for (int i=0;i<ComponentCount;i++) { ShowMessage(Components[i]->Name); //..... } but the same thing does not work if I want to enumerate only the components which are located on Panel. for (int i=0;i<Panel1->ComponentCount;i++) { ShowMessage(Panel1->Components[i]->Name); //..... } because Panel1->ComponentCount; is just zero while having several components on Panel. So, how can I enumerate the child components of Panel? 回答1: The ComponentCount

Enumerate NSArray starting at givven index searching both ways (wrap around)

房东的猫 提交于 2019-12-11 10:32:47
问题 Example. I've got an array with 15 objects. I want to start enumerating from a given index. Say start at index 5 and then the index above, the index under, above, under etc... I do want it to wrap around. So the order of indexes in my example would be. 5, 6, 4, 7, 3, 8, 2, 9, 1, 10, 0, 11, 14, 12, 13 It would be great to have a method signature similar to following line, but I don't require that to approva an answer: - (void)enumerateFromIndex:(NSUInteger)index wrapAroundAndGoBothWays:(void (

Why does having more than one capture group in my regex crash my app?

爷,独闯天下 提交于 2019-12-11 02:17:42
问题 Regardless of what the regex is, >1 capture group will crash this code with the following error. Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString substringWithRange:]: Range {9223372036854775807, 0} out of bounds; string length 279' public extension String { //Multi use parsing function func regexParse(pattern:String, captureGroup:Int, caseSensitive:Bool) ->[String] { do { //Creates empty results array. var resultsArray = [""] //Sets Case sensitivity var

Iterate Across Columns in a List of Lists in Python

时光怂恿深爱的人放手 提交于 2019-12-11 01:28:53
问题 When I attempt iteration across columns in a row, the column does no change within a nested loop: i_rows = 4 i_cols = 3 matrix = [[0 for c in xrange(i_cols)] for r in xrange(i_rows)] for row, r in enumerate(matrix): for col, c in enumerate(r): r[c] = 1 print matrix Observed output [[1, 0, 0], [1, 0, 0], [1, 0, 0], [1, 0, 0]] Expected output [[1, 1, 1], [1, 1, 1], [1, 1, 1], [1, 1, 1]] I have tried different expressions such as xrange() and len() and I am considering switching to numpy. I am a

Vectorizing for loop with repeated indices in python

穿精又带淫゛_ 提交于 2019-12-11 01:27:57
问题 I am trying to optimize a snippet that gets called a lot (millions of times) so any type of speed improvement (hopefully removing the for-loop) would be great. I am computing a correlation function of some j'th particle with all others C_j(|r-r'|) = sqrt(E((s_j(r')-s_k(r))^2)) averaged over k. My idea is to have a variable corrfun which bins data into some bins (the r, defined elsewhere). I find what bin of r each s_k belongs to and this is stored in ind. So ind[0] is the index of r (and thus

enumerating a list in a list

南笙酒味 提交于 2019-12-10 17:33:49
问题 I have a date with events that happened on the date. I want to enumerate the list of events on the date when i show the calendar. Also I need to be able to delete an event from the list. def command_add(date, event, calendar): if date not in calendar: calendar[date] = list() calendar[date].append(event) calendar = {} command_add("2015-10-29", "Python class", calendar) command_add("2015-10-12", "Eye doctor", calendar) command_add("2015-10-12", "lunch with sid", calendar) command_add("2015-10

Matplotlib plt.plot with enumerate not working

a 夏天 提交于 2019-12-10 11:54:20
问题 import numpy as np import matplotlib.pyplot as plt array = np.array([[1,2,3,4,5,6],[10,20,30,40,50,60],[3,4,5,6,7,8],[100,200,300,400,500,600]]) def plot(list): fig = plt.figure() ax = fig.add_subplot(111) for a,i in enumerate(list.T): ax.scatter(i[0],i[1],c='red') # This is plotted ax.plot(i[2],i[3],'g--') # THIS IS NOT BEING PLOTTED !!!! fig.show() plot(array) Now, I need to call plot several times using different array lists. So my for loop cannot be removed. Is there any other way to plot