List Comprehension starting at a specific number [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 months ago.
Improve this question
I was trying to create this [80000, 104000, 135000...] list in Python. Its the value, starting at 80,000 multiplied by 1.3 each time I want
What i've tried:
a = [num*1.5 for num in ??? if num>=80000] #???--> i've tried range(10)
I should be able to do this but I can't find any solutions rn..
I must use list-comprehensions, if possible.
Some help would be nice, thank you!

There is a very basic mathematical operation that represents multiplying by the same value many time: power.
a = [80000 * (1.3**n) for n in range(100)]

You could write your own generator then use that in conjunction with a list comprehension.
def numgen(start, factor, limit):
for _ in range(limit):
yield int(start)
start *= factor
mylist = [value for value in numgen(80_000, 1.3, 10)]
print(mylist)
Output:
[80000, 104000, 135200, 175760, 228488, 297034, 386144, 501988, 652584, 848359]

import numpy as np
print(80000 * 1.3**np.arange(3))
# [ 80000. 104000. 135200.]

Related

More Pythonic way to loop through a range [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
A friend asked me to make her coding more "pythonic" but I'm pretty new at it myself. This is what I came up with, and I'm a little concerned that it won't hit all of the numbers (6, 7, 8, 9, 10 and 11). I also KNOW that there is a better way, but I just don't know what it is. Can you help?
prob = 0
for r in range(6,11):
prob += binom.pmf(k=r, n=11, p=0.2)
print(‘The probability is {}’,format(prob))
I think that your code is perfectly pythonic, for what that's worth
You could make it a little more compact and more performant with a list comprehension, the sum() function, and an f-string. Also, I took into account that you wanted 11 inclusive, so your range should be from 6 to 12.
probs = [binom.pmf(k=r, n=11, p=0.2) for r in range(6, 12)]
print(f'The probability is {sum(probs)}')
Use list comprenhension:
def binom(n,r,p):
# dummy function
return n+r+p
prob = sum([binom(r,12, 0.2) for r in range(6,11)])
print(f"The probability is {prob}")
Edit:
To make 11 inclusive, increase your range to 12
Edit 2:
As suggested by OneCricketeer, if only need to compute the sum, you can remove the square brackets, like so:
prob = sum(binom(r,12, 0.2) for r in range(6,11))

List groupings of a given length [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to find all possible groupings for a list so that the resulting list has a specified length, here's an example:
group([2,3,5,6,8], length=3)
would give
[[2,[3,5],[6,8]], [[2,3],5,[6,8]], [2,3,[5,6,8]], ...
what would be the best way to approach this?
Try this recursive solution, warning: if your list grows larger, this will grow exponentially! Also, this will have some duplicate in that the orders are different.
from itertools import permutations
master = []
def group(l, num, res=[]):
if num == 1:
res.append(l)
master.append(res)
return
for i in range(len(l)-num + 1):
firstgroup = list({e[:i+1] for e in permutations(l)})
for each in firstgroup:
myres = res[:]
myres.append(list(each))
lcp = l[:]
for j in each:
lcp.remove(j)
group(lcp, num-1, myres)

How do I return a list of numbers below a certain threshold without using complex list functions? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I can't figure out how to do this without using complex functions, please help. this is the docstring of the code:
'''
finds all numbers in the list below a certain threshold
:param numList: a list of numbers
:threshold: the cutoff (only numbers below this will be included)
:returns: a new list of all numbers from numList below the threshold
'''
One approach
def filterList(numList, threshold):
return list(filter(lambda x: x < threshold, numList))
Another approach:
filteredList = [x for x in numList if x < threshold]

Printing and array two times in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I have a list a=['abc','cdv','fasdf'] and also have a constant n which says the amount of time print each elements two times.
For example, n=2 should return a=['abc','abc','cdv','cdv']; or n=4 will return a=['abc','abc','cdv','cdv','fasdf','fasdf','abc','abc'].
Here is one way using itertools.chain and a generator comprehension:
from itertools import chain
a = ['abc','cdv','fasdf']
n = 4
res = list(chain.from_iterable([a[i % len(a)]]*2 for i in range(n)))
# ['abc', 'abc', 'cdv', 'cdv', 'fasdf', 'fasdf', 'abc', 'abc']
it looks like you'll need to recycle elements if n is larger than the length of the list. An easy way to deal with this is to duplicated the array as many times as needed.
import math
n_over = math.ceil(len(a)/n)
n_reps = 1 + n_over
a_long = a * n_reps
we can iterate over the new array to build the new one
a_rep = []
for e in a_long[0:n]:
a_new += [e]*n

shifting numbers in python code [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Please help I have no idea on how to write this function. I tried a ceaser cypher function and it didn't work. Any ideas?
Write a function cycle( S, n ) that takes in a string S of '0's and '1's and an integer n and returns the a string in which S has shifted its last character to the initial position n times. For example, cycle('1110110000', 2) would return '0011101100'.
The function you are looking for is:
def cycle(s, n):
return s[-n:] + s[:-n]
You could use Python's deque data type as follows:
import collections
def cycle(s, n):
d = collections.deque(s)
d.rotate(n)
return "".join(d)
print cycle('1110110000', 2)
This would display:
0011101100

Categories