list-comprehension

Shallow or deep copy in a list comprehension

爷,独闯天下 提交于 2021-01-28 03:04:30
问题 If you have a list (Original) in python like: class CustomID (object): def __init__(self, *args): self.ID = '' self.manymore = float() self.visited = False self.isnoise = False IDlist = ['1','2','3','4','5','6','7','8','9','10'] Original = list() for IDs in IDlist: NewObject = CustomID() NewObject.ID = IDs Original.append(NewObject) and if you do a comprehension for a new list and a function to use over the comprehension sublist: def Func(InputList=list()): for objects in InputList: objects

In Python, whats the difference between a list comprehension with a list and a tuple?

大城市里の小女人 提交于 2021-01-27 20:15:50
问题 Playing around with iPython, I was surprised to discover that given a list f of objects each supporting some method x() (that, say, prints out "Hi!" ), the expression: (y.x() for y in f) is not semantically equivalent to [y.x() for y in f] The first one (with the tuple as output) results in a generator expression that is not evaluated unless I iterate over it, whereas the one with the list actually causes the generation to happen immediately: In [30]: (y.x() for y in f) Out[30]: <generator

Convert forloop to list comprehension

两盒软妹~` 提交于 2021-01-27 19:12:28
问题 I am trying to convert the following to list comprehension but struggling: lorem_ipsum = """Lorem ipsum dolor sit amet, consectetur adipiscing elit.""" def word_count2(str): counts = dict() words = str.split() for word in words: if word in counts: counts[word] += 1 else: counts[word] = 1 return counts print(word_count2(lorem_ipsum)) So far I have tried a few variations on this:- aString = lorem_ipsum counts = dict() words = aString.split [counts[word] += 1 if word in counts else counts[word]

When to drop list Comprehension and the Pythonic way?

烂漫一生 提交于 2021-01-27 15:33:09
问题 I created a line that appends an object to a list in the following manner >>> foo = list() >>> def sum(a, b): ... c = a+b; return c ... >>> bar_list = [9,8,7,6,5,4,3,2,1,0] >>> [foo.append(sum(i,x)) for i, x in enumerate(bar_list)] [None, None, None, None, None, None, None, None, None, None] >>> foo [9, 9, 9, 9, 9, 9, 9, 9, 9, 9] >>> The line [foo.append(sum(i,x)) for i, x in enumerate(bar_list)] would give a pylint W1060 Expression is assigned to nothing, but since I am already using the foo

Average of elements in a list of list grouped by first item in the list

偶尔善良 提交于 2021-01-27 14:22:17
问题 My list looks like my_list = [['A', 6, 7], ['A', 4, 8], ['B', 9, 3], ['C', 1, 1]], ['B', 10, 7]] I want to find the averages of the other two columns in each of the inner lists grouped by the first column in each of the inner list. [['A', 5, 7.5], ['B', 9.5, 5], ['C', 1, 1]] ['A', 5, 7.5] comes from ['A', (6+4)/2 ,(7+8)/2] I don't mind if I end up getting a dictionary or something, but I would prefer it remain a list. I've tried the following: my_list1 = [i[0] for i in my_list] my_list2 = [i

Python: Generating a symmetric array with list comprehension

╄→гoц情女王★ 提交于 2021-01-27 12:24:32
问题 I am trying to generate a matrix A == [f(i,j) for i,j in range(0,n)] . The matrix is symmetric (f(i,j) == f(j,i)) and the diagonal elements are zero (f(i,i) == 0) . Question: Is it possible to produce a list comprehension that generates this matrix but that only calls the function f(i,j) for i < j ? Not my question: Generate the symmetric matrix in some other way. Possible solution: Make the call to f through an auxiliary function g that saves the value of f in an additional storage or

List comprehensions: different behaviour with respect to scope in debug mode and in normal runtime

落爺英雄遲暮 提交于 2021-01-27 05:40:15
问题 Consider the following: def f(): a = 2 b = [a + i for i in range(3)] f() This runs without problems. As I understand it (please correct me if I'm wrong, though), the list comprehension expression introduces a new scope, but since it is created within a function (as opposed to, say, a class), it has access to the surrounding scope, including the variable a . In contrast, if I were to enter debug mode, stop at line 3 above, and then just manually write the following in the interpreter >>> b =

List comprehensions: different behaviour with respect to scope in debug mode and in normal runtime

不羁的心 提交于 2021-01-27 05:39:25
问题 Consider the following: def f(): a = 2 b = [a + i for i in range(3)] f() This runs without problems. As I understand it (please correct me if I'm wrong, though), the list comprehension expression introduces a new scope, but since it is created within a function (as opposed to, say, a class), it has access to the surrounding scope, including the variable a . In contrast, if I were to enter debug mode, stop at line 3 above, and then just manually write the following in the interpreter >>> b =

multiple variables in list comprehension?

北城余情 提交于 2021-01-20 15:33:57
问题 I want to create a list of lists from a list of multi-field strings and wonder if it is possible to do so in a comprehension. Input: inputs = ["1, foo, bar", "2, tom, jerry"] Desired output: [[1, "foo", "bar"], [2, "tom", "jerry"]] Splitting the string in a comprehension is easy: >>> [s.split(",") for s in inputs] [['1', ' foo', ' bar'], ['2', ' tom', ' jerry']] But I'm having trouble figuring out how to access the columns after the string has been split inside the comprehension, because it

multiple variables in list comprehension?

我与影子孤独终老i 提交于 2021-01-20 15:30:09
问题 I want to create a list of lists from a list of multi-field strings and wonder if it is possible to do so in a comprehension. Input: inputs = ["1, foo, bar", "2, tom, jerry"] Desired output: [[1, "foo", "bar"], [2, "tom", "jerry"]] Splitting the string in a comprehension is easy: >>> [s.split(",") for s in inputs] [['1', ' foo', ' bar'], ['2', ' tom', ' jerry']] But I'm having trouble figuring out how to access the columns after the string has been split inside the comprehension, because it