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)
Related
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 1 year ago.
Improve this question
Given two lists, I need to count the frequency of the items in one list as they are found in the other list; and place the relative frequencies of each item inside frequencyList (where the
frequency of searchFor[0] is stored in frequencyList[0])
I am unable to import anything
textList=['a','b','a','c',...]
searchFor=['a','b']
frequencyList=[2,1]
Try:
[textList.count(i) for i in searchFor]
Or?
list(map(textList.count, searchFor))
The other answer is quite compact and very pythonic but this is an alternate solution that is slightly more efficient as it only requires one pass over the input list.
textList=['a','b','a','c']
output_dict = {}
for i in textList:
try:
output_dict[i] = d[i] + 1
except:
output_dict[i] = 1
print(output_dict['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 2 years ago.
Improve this question
I need to understand the following code.
The code is from a class method.
Code snippet
index = [n for n, value in enumerate(self.Variable[i]) if value == 1]
The above code can be rewritten as:
indices = []
for n, value in enumerate(self.BUSES[i]):
if value==1:
indices.append(n)
enumerate returns a pair of (index, value at that index) for a given list. So you are testing if value at a given index is 1, and if that is true, you add the index to indices.
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]
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
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