list-comprehension

What to replace loops and nested if sentences with in order to speed up Python code?

瘦欲@ 提交于 2020-01-07 03:16:05
问题 How can I avoid for loops and nested if sentences and be more Pythonic? At first glance this may seem like a "please do my all of my work for me" question. I can assure you that it is not. I'm trying to learn some real Python, and would like to discover ways of speeding up code based on a reproducible example and a pre-defined function. I'm calculating returns from following certain signals in financial markets using loads of for loops and nested if sentences. I have made several attempts,

What to replace loops and nested if sentences with in order to speed up Python code?

岁酱吖の 提交于 2020-01-07 03:15:09
问题 How can I avoid for loops and nested if sentences and be more Pythonic? At first glance this may seem like a "please do my all of my work for me" question. I can assure you that it is not. I'm trying to learn some real Python, and would like to discover ways of speeding up code based on a reproducible example and a pre-defined function. I'm calculating returns from following certain signals in financial markets using loads of for loops and nested if sentences. I have made several attempts,

Convert For Loop to List Comprehension: Testing if elements in list 2 are partial match for elements in list 1

随声附和 提交于 2020-01-06 15:18:15
问题 somewhat of a python/programming newbie here, I have come up with a code of 2 nested for loops to test if the elements of the the 2nd loop are a partial (or complete) match of the elements in the first list, and if so those elements are removed from the 1st list. Here is the code: >>> lst = ["my name is Bob Jenkins", "Tommy Cooper I am", "I be Bazil", "I Tarzan"] >>> names = ["Bob", "Tarzan"] >>> for l in lst: for n in names: if n in l: lst.remove(l) >>> print lst ['Tommy Cooper I am', 'I be

Avoid computing the same expression twice in list comprehension [duplicate]

我的未来我决定 提交于 2020-01-06 03:40:06
问题 This question already has answers here : Python list comprehension - want to avoid repeated evaluation (12 answers) Closed 6 months ago . I am using a function in a list comprehension and an if function: new_list = [f(x) for x in old_list if f(x) !=0] It annoys me that the expression f(x) is computed twice in each loop. Is there a way to do it in a cleaner way? Something along the lines of storing the value or including the if statement at the beginning of the list comprehension. 回答1: Compute

Explanation of List Comprehensions

北城以北 提交于 2020-01-05 08:10:26
问题 I'm attempting to learn about dynamic programming and recursion for Python 3.x. The code below is a simple example from my textbook: def recMC(coinValueList, change): minCoins = change if change in coinValueList: return 1 else: for i in [c for c in coinValueList if c <= change]: numCoins = 1 + recMC(coinValueList, change-i) if numCoins < minCoins: minCoins = numCoins return minCoins print(recMC([1,5,10,25], 63)) I'm really struggling to understand line 6 here, the line comprehension. I'm

Python : Finding an item in a list where a function return a minimum value?

一曲冷凌霜 提交于 2020-01-04 06:25:11
问题 I've a list of point with coordinates and another point. A sample from the list : (45.1531912,5.7184742),(45.1531912,5.7184742),(45.1531113,5.7184544),(45.1525337,5.718298),(45.1525337,5.718298), A point : (45.1533837,5.7185242) A function : def dist(point1,point2) .... return aDistance Is there a python one liner (list-comprehension ?) to find a point in the list where a given function returns the minimal value for the list ? 回答1: The min() function takes a key argument already, no need for

Haskell equation solving in the real numbers

岁酱吖の 提交于 2020-01-04 05:29:31
问题 I've just started playing with GHCi. I see that list generators basically solve an equation within a given set: Prelude> [x | x <- [1..20], x^2 == 4] [2] (finds only one root, as expected) Now, why can't I solve equations with results in ℝ, given that the solution is included in the specified range? [x | x <- [0.1,0.2..2.0], x*4 == 2] How can I solve such equations within real numbers set? Edit: Sorry, I meant 0.1 , of course. 回答1: As others have mentioned, this is not an efficient way to

Delete Tuples in more dimensional list if same

て烟熏妆下的殇ゞ 提交于 2020-01-04 05:24:11
问题 I have a list of tuples namely: [[[('p', 'u'), ('r', 'w')], [('t', 'q')]], [[('p', 'u'), ('r', 'w')], [('v', 'q')]], [[('p', 'u'), ('r', 'w')], [('t', 's')]], [[('p', 'u'), ('r', 'w')], [('v', 's')]], [[('p', 'w'), ('r', 'u')], [('t', 'q')]], [[('p', 'w'), ('r', 'u')], [('v', 'q')]], [[('p', 'w'), ('r', 'u')], [('t', 's')]], [[('p', 'w'), ('r', 'u')], [('v', 's')]], [[('r', 'u'), ('p', 'w')], [('t', 'q')]], [[('r', 'u'), ('p', 'w')], [('v', 'q')]], [[('r', 'u'), ('p', 'w')], [('t', 's')]], [[

comprehension list in python2 works fine but i get an error in python3

别等时光非礼了梦想. 提交于 2020-01-03 16:42:51
问题 I have the following code using the comprehensive list: x = int ( input()) y = int ( input()) z = int ( input()) n = int ( input()) ret_list = [ (x,y,z) for x in range(x+1) for y in range(y+1) for z in range(z+1) if x+y+z!=n ] print(ret_list) in python2 works as expected. However in python3 i get the following error: print([ (x,y,z) for x in range(x+1) for y in range(y+1) for z in range(z+1) if x+y+z!=n ]) File "tester.py", line 16, in <listcomp> print([ (x,y,z) for x in range(x+1) for y in

Python: Generate a geometric progression using list comprehension

你离开我真会死。 提交于 2020-01-03 15:35:42
问题 In Python, Can I generate a geometric progression using list comprehensions alone? I don't get how to refer to elements that were added to the list. That would be like Writing python code to calculate a Geometric progression or Generate list - geometric progression. 回答1: List comprehensions don't let you refer to previous values. You could get around this by using a more appropriate tool: from itertools import accumulate from operator import mul length = 10 ratio = 2 progression = list