list-comprehension

Python list and dictionary comprehension

让人想犯罪 __ 提交于 2021-02-10 12:52:30
问题 I am trying to get use to list dictionaries comprehension. Here a small code I have not been able to transform. lst = ['C', 'A', 'B', 'A'] myd = {} for v, k in enumerate(lst): if k in myd: myd[k].append(v) else: myd[k] = [v] print(myd) >>> {'C': [0], 'A': [1, 3], 'B': [2]} I would be please to have some help. 回答1: Here is the answer, although I think the non comprehension approach is much easier to understand. And as mentioned this is not efficient in the least. I would stay with what you

Python list and dictionary comprehension

烂漫一生 提交于 2021-02-10 12:52:09
问题 I am trying to get use to list dictionaries comprehension. Here a small code I have not been able to transform. lst = ['C', 'A', 'B', 'A'] myd = {} for v, k in enumerate(lst): if k in myd: myd[k].append(v) else: myd[k] = [v] print(myd) >>> {'C': [0], 'A': [1, 3], 'B': [2]} I would be please to have some help. 回答1: Here is the answer, although I think the non comprehension approach is much easier to understand. And as mentioned this is not efficient in the least. I would stay with what you

python generator is too slow to use it. why should I use it? and when?

时光毁灭记忆、已成空白 提交于 2021-02-07 10:24:07
问题 Recently I got question about which one is the most fastest thing among iterator , list comprehension , iter(list comprehension) and generator . and then make simple code as below. n = 1000000 iter_a = iter(range(n)) list_comp_a = [i for i in range(n)] iter_list_comp_a = iter([i for i in range(n)]) gene_a = (i for i in range(n)) import time import numpy as np for xs in [iter_a, list_comp_a, iter_list_comp_a, gene_a]: start = time.time() np.sum(xs) end = time.time() print((end-start)*100) the

python generator is too slow to use it. why should I use it? and when?

旧时模样 提交于 2021-02-07 10:23:33
问题 Recently I got question about which one is the most fastest thing among iterator , list comprehension , iter(list comprehension) and generator . and then make simple code as below. n = 1000000 iter_a = iter(range(n)) list_comp_a = [i for i in range(n)] iter_list_comp_a = iter([i for i in range(n)]) gene_a = (i for i in range(n)) import time import numpy as np for xs in [iter_a, list_comp_a, iter_list_comp_a, gene_a]: start = time.time() np.sum(xs) end = time.time() print((end-start)*100) the

python generator is too slow to use it. why should I use it? and when?

≯℡__Kan透↙ 提交于 2021-02-07 10:23:22
问题 Recently I got question about which one is the most fastest thing among iterator , list comprehension , iter(list comprehension) and generator . and then make simple code as below. n = 1000000 iter_a = iter(range(n)) list_comp_a = [i for i in range(n)] iter_list_comp_a = iter([i for i in range(n)]) gene_a = (i for i in range(n)) import time import numpy as np for xs in [iter_a, list_comp_a, iter_list_comp_a, gene_a]: start = time.time() np.sum(xs) end = time.time() print((end-start)*100) the

Extracting first and last element from sublists in nested list

断了今生、忘了曾经 提交于 2021-02-07 08:44:34
问题 How do you extract the first and last elements from each sublist in a nested list? For example: x=[[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(x) #[[1, 2, 3], [4, 5, 6], [7, 8, 9]] The desired result- [[1, 3], [4, 6], [7, 9]] This is the closest I've seen- a = [x[i][i] for i in (0, -1)] print(a) #[1, 9] 回答1: You have the right idea. If your list inside list is only one layer deep, you can access the first and last elements with a list comprehension and -1 indexing like you were trying to do: a = [

Extracting first and last element from sublists in nested list

谁说我不能喝 提交于 2021-02-07 08:43:48
问题 How do you extract the first and last elements from each sublist in a nested list? For example: x=[[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(x) #[[1, 2, 3], [4, 5, 6], [7, 8, 9]] The desired result- [[1, 3], [4, 6], [7, 9]] This is the closest I've seen- a = [x[i][i] for i in (0, -1)] print(a) #[1, 9] 回答1: You have the right idea. If your list inside list is only one layer deep, you can access the first and last elements with a list comprehension and -1 indexing like you were trying to do: a = [

Extracting first and last element from sublists in nested list

故事扮演 提交于 2021-02-07 08:43:24
问题 How do you extract the first and last elements from each sublist in a nested list? For example: x=[[1, 2, 3], [4, 5, 6], [7, 8, 9]] print(x) #[[1, 2, 3], [4, 5, 6], [7, 8, 9]] The desired result- [[1, 3], [4, 6], [7, 9]] This is the closest I've seen- a = [x[i][i] for i in (0, -1)] print(a) #[1, 9] 回答1: You have the right idea. If your list inside list is only one layer deep, you can access the first and last elements with a list comprehension and -1 indexing like you were trying to do: a = [

List comprehension not working [duplicate]

我与影子孤独终老i 提交于 2021-02-05 12:24:54
问题 This question already has answers here : What does “list comprehension” mean? How does it work and how can I use it? (5 answers) List comprehension returning values plus [None, None, None], why? [duplicate] (4 answers) Appending item to lists within a list comprehension (7 answers) List comprehension output is None [duplicate] (3 answers) Closed 3 years ago . I want to put the unique items from one list to another list, i.e eliminating duplicate items. When I do it by the longer method I am

List comprehension not working [duplicate]

夙愿已清 提交于 2021-02-05 12:20:45
问题 This question already has answers here : What does “list comprehension” mean? How does it work and how can I use it? (5 answers) List comprehension returning values plus [None, None, None], why? [duplicate] (4 answers) Appending item to lists within a list comprehension (7 answers) List comprehension output is None [duplicate] (3 answers) Closed 3 years ago . I want to put the unique items from one list to another list, i.e eliminating duplicate items. When I do it by the longer method I am