list-comprehension

How to replace values using list comprehension in python3?

倖福魔咒の 提交于 2020-01-03 10:44:11
问题 I was wondering how would you can replace values of a list using list comprehension. e.g. theList = [[1,2,3],[4,5,6],[7,8,9]] newList = [[1,2,3],[4,5,6],[7,8,9]] for i in range(len(theList)): for j in range(len(theList)): if theList[i][j] % 2 == 0: newList[i][j] = 'hey' I want to know how I could convert this into the list comprehension format. 回答1: You can just do a nested list comprehension: theList = [[1,2,3],[4,5,6],[7,8,9]] [[x if x % 2 else 'hey' for x in sl] for sl in theList] returns

Writing a list comprehension to flatten a nested list [duplicate]

强颜欢笑 提交于 2020-01-03 02:51:23
问题 This question already has answers here : flatten list of list through list comprehension (5 answers) Closed 2 years ago . I have a nested list mixed with list and numbers. nested = [[1, 2, 3], [4, 5, 6], [7, 8], [9], 10, 11] The nested lists only contain numbers, they'll never contain more lists. Is it possible to write a list comprehension to make new list from the 'nested' list, producing the following output? [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] This was my attempt( code is not working)

Python nested dict comprehension with sets

好久不见. 提交于 2020-01-03 01:28:26
问题 Can someone explain how to do nested dict comprehensions? >> l = [set([1, 2, 3]), set([4, 5, 6])] >> j = dict((a, i) for a in s for i, s in enumerate(l)) >> NameError: name 's' is not defined I would have liked: >> j >> {1:0, 2:0, 3:0, 4: 1, 5: 1, 6: 1} I just asked a previous question about a simpler dict comprehension where the parentheses in the generator function were reduced. How come the s in the leftmost comprehension is not recognized? 回答1: Just reverse the order of the two loops: j =

I have a for loop to create a list, can I use a list comprehension instead?

試著忘記壹切 提交于 2020-01-02 06:46:30
问题 Let's have a list of values and an arbitrary integer number. values = ['5', '3', '.', '.', '7', '.', '.', '.', '.', '6', '.', '.', '1', '9', '5', '.', '.', '.', '.', '9', '8', '.', '.', '.', '.', '6', '.', '8', '.', '.', '.', '6', '.', '.', '.', '3', '4', '.', '.', '8', '.', '3', '.', '.', '1', '7', '.', '.', '.', '2', '.', '.', '.', '6', '.', '6', '.', '.', '.', '.', '2', '8', '.', '.', '.', '.', '4', '1', '9', '.', '.', '5', '.', '.', '.', '.', '8', '.', '.', '7', '9'] n = 9 I'd like to

Short-circuiting list comprehensions [duplicate]

不问归期 提交于 2020-01-02 03:16:27
问题 This question already has answers here : Using while in list comprehension or generator expressions (2 answers) Closed 4 years ago . On several occasions I've wanted python syntax for short-circuiting list comprehensions or generator expressions. Here is a simple list comprehension, and the equivalent for loop in python: my_list = [1, 2, 3, 'potato', 4, 5] [x for x in my_list if x != 'potato'] result = [] for element in my_list: if element != 'potato': result.append(element) There isn't

How to find students with the best grades in a list?

☆樱花仙子☆ 提交于 2020-01-02 03:15:15
问题 Suppose, I have a list of Students . Students have fields like name , birth date , grade , etc. How would you find Students with the best grade in Scala? For example: List(Student("Mike", "A"), Student("Pete", "B"), Student("Paul", A))" I want to get List(Student("Mike", "A"), Student("Paul", A)) Obviously, I can find the max grade ("A" in the list above) and then filter the list students.filter(_.grade == max_grade) This solution is O(N) but runs over the list twice. Can you suggest a better

Perl equivalent of (Python-) list comprehension

老子叫甜甜 提交于 2020-01-02 01:04:52
问题 I'm looking for ways to express this Python snippet in Perl: data = {"A": None, "B": "yes", "C": None} key_list = [k for k in data if data[k]] # in this case the same as filter(lambda k: data[k], data) but let's ignore that So looking at it one way, I just want the keys where the values are None or undef . Looking at it another way, what I want is the concise perl equivalent of a list comprehension with conditional. 回答1: I think you want grep: #!/usr/bin/env perl use strict; use warnings; my

Generate time sequence with step 7 seconds

不问归期 提交于 2020-01-01 16:13:12
问题 How would you generate the following sequence of strings in Python? 00:00:00 00:00:07 00:00:14 00:00:21 ... 00:00:49 00:00:56 00:01:03 The step is 7 seconds. The end is about 03:30:+/- I would come with solution that uses modular arithmetic (first 1200 to have hours, than 60 to have minutes and the remainder are seconds and the numbers should be converted to strings and "one-place" strings should be prefixed by "0"). Is there some smarter (pythonic) solution with using some helper generators

Generate time sequence with step 7 seconds

最后都变了- 提交于 2020-01-01 16:11:10
问题 How would you generate the following sequence of strings in Python? 00:00:00 00:00:07 00:00:14 00:00:21 ... 00:00:49 00:00:56 00:01:03 The step is 7 seconds. The end is about 03:30:+/- I would come with solution that uses modular arithmetic (first 1200 to have hours, than 60 to have minutes and the remainder are seconds and the numbers should be converted to strings and "one-place" strings should be prefixed by "0"). Is there some smarter (pythonic) solution with using some helper generators

modify list element with list comprehension in python

拟墨画扇 提交于 2020-01-01 08:47:12
问题 folks, I want to modify list element with list comprehension. For example, if the element is negative, add 4 to it. Thus the list a = [1, -2 , 2] will be converted to a = [1, 2, 2] The following code works, but i am wondering if there is a better way to do it? Thanks. for i in range(len(a)): if a[i]<0: a[i] += 4 回答1: a = [b + 4 if b < 0 else b for b in a] 回答2: If you want to change the list in-place , this is almost the best way. List comprehension will create a new list. You could also use