itertools

Python Itertools permutations with strings

岁酱吖の 提交于 2021-01-28 07:10:15
问题 i want to use itertools permutations for strings instead of just letters. import itertools lst = list(permutations(("red","blue"),3)) #This returns [] I know i can do something like: a = list(permutations(range(3),3)) for i in range(len(a)): a[i] = list(map(lambda x: 'red' if x==0 else 'blue' if x==1 else 'green',a[i])) EDIT: I want to key in this as my input, and get this as my output input: ("red","red","blue") output: [(’red’, ’red’, ’red’), (’red’, ’red’, ’blue’),\ (’red’, ’blue’, ’red’),

Cartesian Product memory error converting itertools.product to list

怎甘沉沦 提交于 2021-01-28 06:50:34
问题 I'm trying to create the Cartesian product of a list of lists. When I try to convert the result to a list it will give me a memory error. If I run it without converting it to a list it runs fine. lists = [['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ],['a','b','c' ]] my_product = list(itertools.product(*lists)) I even tried to filter through some of the result using itertools.dropwhile to make it smaller before

Python 3 itertools.islice continue despite UnicodeDecodeError

拜拜、爱过 提交于 2021-01-28 04:01:02
问题 I have a python 3 program that monitors a log file. The log includes, among other things, chat messages written by users. The log is created by a third party application which I cannot change. Today a user wrote "텋��텋��" and it caused the program to crash with the following error: future: <Task finished coro=<updateConsoleLog() done, defined at /usr/local/src/bserver/logmonitor.py:48> exception=UnicodeDecodeError('utf-8',... say "\xed\xa0\xbd\xed\xb1\x8c"\r\n', 7623, 7624, 'invalid

Python 3 itertools.islice continue despite UnicodeDecodeError

扶醉桌前 提交于 2021-01-28 00:01:03
问题 I have a python 3 program that monitors a log file. The log includes, among other things, chat messages written by users. The log is created by a third party application which I cannot change. Today a user wrote "텋��텋��" and it caused the program to crash with the following error: future: <Task finished coro=<updateConsoleLog() done, defined at /usr/local/src/bserver/logmonitor.py:48> exception=UnicodeDecodeError('utf-8',... say "\xed\xa0\xbd\xed\xb1\x8c"\r\n', 7623, 7624, 'invalid

Finding all combinations of a list in Python with repetition

人走茶凉 提交于 2021-01-07 07:06:28
问题 I am looking to find and print all possible combinations of the set (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) of length 5. There should be 13 choose 5 combinations (6188) because order does NOT matter, and repetition is allowed. I found this code and was using it: from itertools import product for item in product([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], repeat=5): print(item) However, this is not printing all 6188 combinations. Trying to figure out how to tweak the code so it spits out

Finding all combinations of a list in Python with repetition

非 Y 不嫁゛ 提交于 2021-01-07 07:04:27
问题 I am looking to find and print all possible combinations of the set (0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12) of length 5. There should be 13 choose 5 combinations (6188) because order does NOT matter, and repetition is allowed. I found this code and was using it: from itertools import product for item in product([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], repeat=5): print(item) However, this is not printing all 6188 combinations. Trying to figure out how to tweak the code so it spits out

Python - Group Dates by Month

半世苍凉 提交于 2021-01-03 05:17:20
问题 Here's a quick problem that I, at first, dismissed as easy. An hour in, and I'm not so sure! So, I have a list of Python datetime objects, and I want to graph them. The x-values are the year and month, and the y-values would be the amount of date objects in this list that happened in this month. Perhaps an example will demonstrate this better (dd/mm/yyyy): [28/02/2018, 01/03/2018, 16/03/2018, 17/05/2018] -> ([02/2018, 03/2018, 04/2018, 05/2018], [1, 2, 0, 1]) My first attempt tried to simply

Python - Group Dates by Month

大城市里の小女人 提交于 2021-01-03 05:12:36
问题 Here's a quick problem that I, at first, dismissed as easy. An hour in, and I'm not so sure! So, I have a list of Python datetime objects, and I want to graph them. The x-values are the year and month, and the y-values would be the amount of date objects in this list that happened in this month. Perhaps an example will demonstrate this better (dd/mm/yyyy): [28/02/2018, 01/03/2018, 16/03/2018, 17/05/2018] -> ([02/2018, 03/2018, 04/2018, 05/2018], [1, 2, 0, 1]) My first attempt tried to simply

Python - Group Dates by Month

六月ゝ 毕业季﹏ 提交于 2021-01-03 05:12:25
问题 Here's a quick problem that I, at first, dismissed as easy. An hour in, and I'm not so sure! So, I have a list of Python datetime objects, and I want to graph them. The x-values are the year and month, and the y-values would be the amount of date objects in this list that happened in this month. Perhaps an example will demonstrate this better (dd/mm/yyyy): [28/02/2018, 01/03/2018, 16/03/2018, 17/05/2018] -> ([02/2018, 03/2018, 04/2018, 05/2018], [1, 2, 0, 1]) My first attempt tried to simply

get all the partitions of the set python with itertools

帅比萌擦擦* 提交于 2020-12-10 03:31:42
问题 How to get all partitions of a set? For example, I have array [1, 2, 3] . I need to get [[1], [2], [3]], [[1], [2, 3]], [[2], [1,3]], [[3], [1, 2]], [[1, 2, 3]] . Now, I wrote this code: def neclusters(S, K): for splits in itertools.combinations(range(len(S)), K): yield np.split(S, 1 + np.array(splits)) But that code don't return [[2],[1,3]] . I could take all permutations of the original set and run this code on them. But can this be made easier? 回答1: I wrote this one for fun: def partition