enumerate

Only index needed: enumerate or (x)range?

穿精又带淫゛_ 提交于 2019-11-27 02:07:42
If I want to use only the index within a loop, should I better use the range/xrange function in combination with len() a = [1,2,3] for i in xrange(len(a)): print i or enumerate ? Even if I won't use p at all? for i,p in enumerate(a): print i I would use enumerate as it's more generic - eg it will work on iterables and sequences, and the overhead for just returning a reference to an object isn't that big a deal - while xrange(len(something)) although (to me) more easily readable as your intent - will break on objects with no support for len ... That's a rare requirement – the only information

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

你说的曾经没有我的故事 提交于 2019-11-27 01:27:24
问题 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

How to make a continuous alphabetic list python (from a-z then from aa, ab, ac etc)

浪尽此生 提交于 2019-11-26 22:24:00
问题 I would like to make a alphabetical list for an application similar to an excel worksheet. A user would input number of cells and I would like to generate list. For example a user needs 54 cells. Then I would generate 'a','b','c',...,'z','aa','ab','ac',...,'az', 'ba','bb' I can generate the list from [ref] from string import ascii_lowercase L = list(ascii_lowercase) How do i stitch it together? A similar question for PHP has been asked here. Does some one have the python equivalent? 回答1: Use

How enumerate all classes with custom class attribute?

心已入冬 提交于 2019-11-26 15:01:00
Question based on MSDN example . Let's say we have some C# classes with HelpAttribute in standalone desktop application. Is it possible to enumerate all classes with such attribute? Does it make sense to recognize classes this way? Custom attribute would be used to list possible menu options, selecting item will bring to screen instance of such class. Number of classes/items will grow slowly, but this way we can avoid enumerating them all elsewhere, I think. Yes, absolutely. Using Reflection: static IEnumerable<Type> GetTypesWithHelpAttribute(Assembly assembly) { foreach(Type type in assembly

Only index needed: enumerate or (x)range?

依然范特西╮ 提交于 2019-11-26 09:55:53
问题 If I want to use only the index within a loop, should I better use the range/xrange function in combination with len() a = [1,2,3] for i in xrange(len(a)): print i or enumerate ? Even if I won\'t use p at all? for i,p in enumerate(a): print i 回答1: I would use enumerate as it's more generic - eg it will work on iterables and sequences, and the overhead for just returning a reference to an object isn't that big a deal - while xrange(len(something)) although (to me) more easily readable as your

How enumerate all classes with custom class attribute?

烈酒焚心 提交于 2019-11-26 05:57:21
问题 Question based on MSDN example. Let\'s say we have some C# classes with HelpAttribute in standalone desktop application. Is it possible to enumerate all classes with such attribute? Does it make sense to recognize classes this way? Custom attribute would be used to list possible menu options, selecting item will bring to screen instance of such class. Number of classes/items will grow slowly, but this way we can avoid enumerating them all elsewhere, I think. 回答1: Yes, absolutely. Using

Objective-C: Reading a file line by line

戏子无情 提交于 2019-11-26 03:17:58
问题 What is the appropriate way of dealing with large text files in Objective-C? Let\'s say I need to read each line separately and want to treat each line as an NSString. What is the most efficient way of doing this? One solution is using the NSString method: + (id)stringWithContentsOfFile:(NSString *)path encoding:(NSStringEncoding)enc error:(NSError **)error and then split the lines with a newline separator, and then iterate over the elements in the array. However, this seems fairly

What does enumerate() mean?

こ雲淡風輕ζ 提交于 2019-11-25 22:33:26
问题 What does for row_number, row in enumerate(cursor): do in Python? What does enumerate mean in this context? 回答1: The enumerate() function adds a counter to an iterable. So for each element in cursor , a tuple is produced with (counter, element) ; the for loop binds that to row_number and row , respectively. Demo: >>> elements = ('foo', 'bar', 'baz') >>> for elem in elements: ... print elem ... foo bar baz >>> for count, elem in enumerate(elements): ... print count, elem ... 0 foo 1 bar 2 baz