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
Related
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.]
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 2 years ago.
Improve this question
I have the following list:
a=['c1','c2','c3']
And I would like to get to:
b=[sametext['c1'],sametext['c2'],sametext['c3']]
I've tried to make list in list but I'm not able to get any result? How can I get to b?
In [sametext['c1'],sametext['c2'],sametext['c3']], is sametext a dictionary which contains a mapping of some sort?
If that's the case then the way to do it will be with this list comprehension:
b = [sametext[x] for x in a]
Without list comprehensions:
b=[]
for x in a:
b.append(sametext[x])
If by sametext all you mean is some constant operation, like adding a prefix then similar to the first approach the way would be:
b = [f"yourprefix_{x}]" for x in a]
b= [ele+'some_text' for ele in a]
To make change in-place
a[:] = [ele+'some_text' for ele in a]
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)
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 create this kind of output in Python
["k", "kk", "kkk", "kkkk", ...]
["rep", "reprep", "repreprep", ...]
That is a list of n elements, made of the same character (or small group of characters) repeated X times, X being increased by one for each element.
I can't find a way to do this easily, without loops..
Thanks,
Here you have a generator using itertools.count, remember the property of "multiplying" strings in python by a number, where they will be replicated and concatenated nth times, where for example "a"*3 == "aaa" :
import itertools
def genSeq(item):
yield from (item*i for i in itertools.count())
Here you have a live example
repeating_value = "k" #Assign the value which do you want to be repeated
total_times=5 #how many times do you want
expected_list=[repeating_value*i for i in range(1,total_times+1)]
print(expected_list)
character = 'k'
_range = 5
output = [k*x for k in character for x in range(1, _range + 1)]
print(output)
I would multiple my character by a specified number in the range, and then I would simply iterate through the range in a list comprehension. We add 1 to the end of the range in order to get the full range.
Here is your output:
['k', 'kk', 'kkk', 'kkkk', 'kkkkk']
The following is by far the easiest which I have built upon the comment by the user3483203 which eliminates initial empty value.
var = 'rep'
list = [var * i for i in range(1,x,1)]
print(list)
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]