iterable

Can I return a list of ALL min tuples, using Python's Min function?

陌路散爱 提交于 2020-12-16 05:48:29
问题 Say I have a list of tuples, like the following: listo = [('a','1'),('b','0'),('c','2'),('d','0')] If I want the lowest tuple, based on the second index of each tuple, I can customize the min function with a lambda function, like this: min(listo, key=lambda x: x[1]) As it stands, this code would return: In [31]: min(listo, key=lambda x: x[1]) Out[31]: ('b', '0') But this only gives me one tuple, and only the first one it encounters at that. What if I wanted ALL the min tuples? So it returns

How can I filter an Iterable based upon a Predicate?

久未见 提交于 2020-06-27 19:30:32
问题 I want to do a string list filter function using an Iterable<String> and a predicate to select the strings to keep, the other ones must be removed from the list, but I'm not understating how I do the remove. static <T> Iterable<T> select(Iterable<T> it, Predicate<T> pred) { for (T s: it) { if (pred.test(s)==false) { // what to do here? } } return ...; } For this input: {"a","","b",""} I expect {"a","b"} 回答1: An Iterable represents the capability to provide an Iterator on request. So, to

How can I filter an Iterable based upon a Predicate?

核能气质少年 提交于 2020-06-27 19:28:48
问题 I want to do a string list filter function using an Iterable<String> and a predicate to select the strings to keep, the other ones must be removed from the list, but I'm not understating how I do the remove. static <T> Iterable<T> select(Iterable<T> it, Predicate<T> pred) { for (T s: it) { if (pred.test(s)==false) { // what to do here? } } return ...; } For this input: {"a","","b",""} I expect {"a","b"} 回答1: An Iterable represents the capability to provide an Iterator on request. So, to

Finding the Max value in a two dimensional Array

三世轮回 提交于 2020-04-10 07:39:26
问题 I'm trying to find an elegant way to find the max value in a two-dimensional array. for example for this array: [0, 0, 1, 0, 0, 1] [0, 1, 0, 2, 0, 0][0, 0, 2, 0, 0, 1][0, 1, 0, 3, 0, 0][0, 0, 0, 0, 4, 0] I would like to extract the value '4'. I thought of doing a max within max but I'm struggling in executing it. 回答1: Max of max numbers ( map(max, numbers) yields 1, 2, 2, 3, 4): >>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0] >>

Finding the Max value in a two dimensional Array

只谈情不闲聊 提交于 2020-04-10 07:39:02
问题 I'm trying to find an elegant way to find the max value in a two-dimensional array. for example for this array: [0, 0, 1, 0, 0, 1] [0, 1, 0, 2, 0, 0][0, 0, 2, 0, 0, 1][0, 1, 0, 3, 0, 0][0, 0, 0, 0, 4, 0] I would like to extract the value '4'. I thought of doing a max within max but I'm struggling in executing it. 回答1: Max of max numbers ( map(max, numbers) yields 1, 2, 2, 3, 4): >>> numbers = [0, 0, 1, 0, 0, 1], [0, 1, 0, 2, 0, 0], [0, 0, 2, 0, 0, 1], [0, 1, 0, 3, 0, 0], [0, 0, 0, 0, 4, 0] >>

Build a set fails when using set(int) (Python)

时光毁灭记忆、已成空白 提交于 2020-03-15 05:35:08
问题 I can do >>> s = {1} >>> type(s) <class 'set'> but >>> s = set(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable what is the difference? 回答1: The difference is that the set() constructor takes an iterable. A single number is not an iterable. s = set((1,)) 来源: https://stackoverflow.com/questions/27180659/build-a-set-fails-when-using-setint-python

Build a set fails when using set(int) (Python)

廉价感情. 提交于 2020-03-15 05:34:55
问题 I can do >>> s = {1} >>> type(s) <class 'set'> but >>> s = set(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable what is the difference? 回答1: The difference is that the set() constructor takes an iterable. A single number is not an iterable. s = set((1,)) 来源: https://stackoverflow.com/questions/27180659/build-a-set-fails-when-using-setint-python

Build a set fails when using set(int) (Python)

谁说胖子不能爱 提交于 2020-03-15 05:34:32
问题 I can do >>> s = {1} >>> type(s) <class 'set'> but >>> s = set(1) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'int' object is not iterable what is the difference? 回答1: The difference is that the set() constructor takes an iterable. A single number is not an iterable. s = set((1,)) 来源: https://stackoverflow.com/questions/27180659/build-a-set-fails-when-using-setint-python

Are containers always iterable?

感情迁移 提交于 2020-02-13 22:24:50
问题 Is there a scenario where the container is not iterable, according to this graph? 回答1: Depends on what you mean by always . According to collections.abc a container is an object that implements __contains__ method an iterable is an object that implements __iter__ (or __getitem__ , as a fallback) So, theoretically, no, you can implement a container that is not an iterable. However, all standard python containers (and most containers implemented by libraries) are also iterable. 来源: https:/