list-comprehension

Another question about random numbers in Haskell

折月煮酒 提交于 2021-02-05 11:21:11
问题 I am trying to make a version of the Voltorb game from Pokemon Gold and Silver in Haskell. Now for generation of the board, I want to have a list of (l,r,v) triplets where l is the line, r is the row and v is the value of the field. Values l and r are implemented with list comprehension since they should be the same every time. As for v though I can't find an option to implement it so that it is 0,1,2 or 3 "randomly" (I know that Haskell is purely functional and there is no true randomness,

converting str to int in list comprehension [duplicate]

僤鯓⒐⒋嵵緔 提交于 2021-02-05 10:54:48
问题 This question already has answers here : Remove empty strings from a list of strings (13 answers) Filtering a list of strings based on contents (5 answers) Closed 2 years ago . I have a list with years as strings but there are few missing years which are represented as empty strings. I am trying to convert those strings to integers and skip the values which can't be converted using list comprehension and try and except clause? birth_years = ['1993','1994', '' ,'1996', '1997', '', '2000',

Find duplicates of dictionary in a list and combine them in Python

别说谁变了你拦得住时间么 提交于 2021-02-05 08:23:11
问题 I have this list of dictionaries: "ingredients": [ { "unit_of_measurement": {"name": "Pound (Lb)", "id": 13}, "quantity": "1/2", "ingredient": {"name": "Balsamic Vinegar", "id": 12}, }, { "unit_of_measurement": {"name": "Pound (Lb)", "id": 13}, "quantity": "1/2", "ingredient": {"name": "Balsamic Vinegar", "id": 12}, }, { "unit_of_measurement": {"name": "Tablespoon", "id": 15}, "ingredient": {"name": "Basil Leaves", "id": 14}, "quantity": "3", }, ] I want to be able to find the duplicates of

Why converting list to set is faster than converting generator to set?

本小妞迷上赌 提交于 2021-02-04 19:47:25
问题 Here is an example >>> from timeit import timeit >>> print(timeit('[y for y in range(100)]', number=100000)) 0.7025867114395824 >>> print(timeit('(y for y in range(100))', number=100000)) 0.09295392291478244 >>> print(timeit('set([y for y in range(100)])', number=100000)) 1.0864544935180334 >>> print(timeit('set((y for y in range(100)))', number=100000)) 1.1277489876506621 It is very confusing. Generator takes less time to create(and that is understandable) but why converting generator to set

Is the order of results coming from a list comprehension guaranteed?

大憨熊 提交于 2021-02-04 12:50:06
问题 When using a list comprehension, is the order of the new list guaranteed in any way? As a contrived example, is the following behavior guaranteed by the definition of a list comprehension: >> a = [x for x in [1,2,3]] >> a [1, 2, 3] Equally, is the following equality guaranteed: >> lroot = [1, 2, 3] >> la = [x for x in lroot] >> lb = [] >> for x in lroot: lb.append(x) >> lb == la True Specifically, it's the ordering I'm interested in here. 回答1: Yes, the list comprehension preserves the order

In Python how do I create variable length combinations or permutations?

和自甴很熟 提交于 2021-02-02 06:43:47
问题 Lets say I have an array called arr = [1,2,3,4] How can I generate all the possible combinations with minimum 2 arguments that end up looking like [1,2] [1,3] [1,4] [1,2,3] [1,2,4] [1,2,3, 4] [2,3] [2,4] and so on and so forth? Nothing Im trying works. I cant seem to use itertools.combinations or permutations because I need to know the argument size and I cant seem to use itertools.products because that will take minimum one argument from each of a list of lists that looks like this [[1],[2],

Appending to a list comprehension in Python returns None

蓝咒 提交于 2021-02-01 21:00:04
问题 This is a question out of curiosity rather than trying to use it for a practical purpose. Consider I have the following simple example where I generate a list through list comprehension: >>> a = [1, 2, 3] >>> b = [2 * i for i in a] >>> b [2, 4, 6] >>> b.append(a) >>> b [2, 4, 6, [1, 2, 3]] However if I try and do this all in one action >>> a = [1, 2, 3] >>> b = [2 * i for i in a].append(a) >>> b == None True The result returns None . Is there any reason why this is the case? I would have

Appending to a list comprehension in Python returns None

最后都变了- 提交于 2021-02-01 20:46:26
问题 This is a question out of curiosity rather than trying to use it for a practical purpose. Consider I have the following simple example where I generate a list through list comprehension: >>> a = [1, 2, 3] >>> b = [2 * i for i in a] >>> b [2, 4, 6] >>> b.append(a) >>> b [2, 4, 6, [1, 2, 3]] However if I try and do this all in one action >>> a = [1, 2, 3] >>> b = [2 * i for i in a].append(a) >>> b == None True The result returns None . Is there any reason why this is the case? I would have

Appending to a list comprehension in Python returns None

随声附和 提交于 2021-02-01 20:46:17
问题 This is a question out of curiosity rather than trying to use it for a practical purpose. Consider I have the following simple example where I generate a list through list comprehension: >>> a = [1, 2, 3] >>> b = [2 * i for i in a] >>> b [2, 4, 6] >>> b.append(a) >>> b [2, 4, 6, [1, 2, 3]] However if I try and do this all in one action >>> a = [1, 2, 3] >>> b = [2 * i for i in a].append(a) >>> b == None True The result returns None . Is there any reason why this is the case? I would have

List element (object) not subscriptable

依然范特西╮ 提交于 2021-01-29 08:36:59
问题 I want to create a unit test for a function which sorts a list of objects according to some object attribute(s). Here is a code sample: class Foo: """Custom data type Foo.""" def __init__(self, a: str, b: int, c: int, d: int, e: int, f: int): self.a = a self.b = b self.c = c self.d = d self.e = e self.f = f self.g = c * d * e self.h = c * d This is a part of the unit test function which produces the error: from random import randint ... class Test(TestCase): def test_item_sort_foo(self):