itertools

Python Itertools permutations only letters and numbers

南楼画角 提交于 2019-12-30 10:16:41
问题 I need to get only the permutations that have letters and numbers (The permutation can not be. "A, B, C, D" I need it like this: "A, B, C, 1") In short, the permutations can not contain only letters, not just numbers. Must be a combination of both. My code: import itertools print list(itertools.combinations([0,1,2,3,4,'a','b','c','d'], 4)) Then I get: [(0, 1, 2, 3), (0, 1, 2, 4), (0, 1, 2, 'a'), (0, 1, 2, 'b'), (0, 1, 2, 'c'), (0, 1, 2, 'd'), (0, 1, 3, 4), (0, 1, 3, 'a'), (0, 1, 3, 'b'), (0,

Performance of library itertools compared to python code

拥有回忆 提交于 2019-12-30 09:53:18
问题 As answer to my question Find the 1 based position to which two lists are the same I got the hint to use the C-library itertools to speed up things. To verify I coded the following test using cProfile: from itertools import takewhile, izip def match_iter(self, other): return sum(1 for x in takewhile(lambda x: x[0] == x[1], izip(self, other))) def match_loop(self, other): element = -1 for element in range(min(len(self), len(other))): if self[element] != other[element]: element -= 1 break

Python: Generating all ordered combinations of a list

感情迁移 提交于 2019-12-29 08:36:57
问题 I'm using Python 2.7. I'm having a list, and I want all possible ordered combinations. import itertools stuff = ["a","b","c", "d"] for L in range(1, len(stuff)+1): for subset in itertools.combinations(stuff, L): print( ' '.join(subset)) This will give the following output: a b c d a b a c <-- not in correct order a d <-- not in correct order b c b d <-- not in correct order c d a b c a b d <-- not in correct order a c d <-- not in correct order b c d a b c d But I want the output only to be

Longest substring in alphabetical order [closed]

六眼飞鱼酱① 提交于 2019-12-29 07:19:09
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 5 years ago . Write a program that prints the longest substring of s in which the letters occur in alphabetical order. For example, if s = 'azcbobobegghakl', then your program should print Longest substring in alphabetical order is: beggh In the case of ties, print the first substring. For example, if s = 'abcbcd', then your

Generating all unique pair permutations

牧云@^-^@ 提交于 2019-12-28 17:56:47
问题 I need to generate all possible pairings, but with the constraint that a particular pairing only occurs once in the results. So for example: import itertools for perm in itertools.permutations(range(9)): print zip(perm[::2], perm[1::2]) generates all possible two-paired permutations; here's a small subset of the output: ... [(8, 4), (7, 6), (5, 3), (0, 2)] [(8, 4), (7, 6), (5, 3), (1, 0)] [(8, 4), (7, 6), (5, 3), (1, 2)] [(8, 4), (7, 6), (5, 3), (2, 0)] [(8, 4), (7, 6), (5, 3), (2, 1)] [(8, 5

Equivalent Nested Loop Structure with Itertools

拟墨画扇 提交于 2019-12-28 13:51:50
问题 Python's succint syntax through its batteries allows verbose code line to be expressed in readable one liners. Consider the following examples ====================================================| for a in range(3): | for b in range(3): | for c in range(3): | print (a,b,c), | - - - - - - - - - - - - - - - - - -| for e in product(range(3), repeat=3): | print e, | ====================================================| for a in range(3): | for b in range(a , 3): | for c in range(b , 3): | print

Python list of lists specific path combinations or permutations

拈花ヽ惹草 提交于 2019-12-25 19:48:10
问题 I have a list of lists and am looking for something similar to combinations or permutations, but there are conditions that may result in good "Path" or "Dead_End". If "Dead_End", it should index to the next choice in the list. Example: AList = [[1, 2, 3], [2, 0, 3], [3, 1, 0], [1, 0, 2]] N = 0 Index = 0 #AList[N][Index]=1, AList[2][2]=0 I would like to start with the value AList[N][Index], which at the start is [0][0] and therefore equals 1 and assign that to N. After N is assigned 1, we can

Django merge 2 querysets in staggered/alternating fashion without duplicates?

一曲冷凌霜 提交于 2019-12-25 08:46:36
问题 Follow up question to this one: Django merge 2 querysets in staggered/alternating fashion? I'm wondering is there an efficient way to avoid duplicates in the result? i.e. the generators will only return items we have not seen before. 来源: https://stackoverflow.com/questions/45834009/django-merge-2-querysets-in-staggered-alternating-fashion-without-duplicates

How can I make an unique list cells?

那年仲夏 提交于 2019-12-25 08:18:12
问题 I have a txt file which looks like below including 4 rows as an example and each row strings are separated by a , . "India1,India2,myIndia " "Where,Here,Here " "Here,Where,India,uyete" "AFD,TTT" https://gist.github.com/anonymous/cee79db7029a7d4e46cc4a7e92c59c50 the file can be downloaded from here I want to extract all unique cells across all , the output2 India1 India2 myIndia Where Here India uyete AFD TTT I tried to read line by line and print it ìf i call my data as df` myfile = open("df

Sorting a python dictionary after running an itertools function

女生的网名这么多〃 提交于 2019-12-25 06:24:00
问题 This question is the culmination of two pieces of code guided by two answers here on SO. The first question I had was how to compare similarity between two strings and I got a good answer as seen here with the following code: code 1 def get_bigrams(string): ''' Takes a string and returns a list of bigrams ''' s = string.lower() return { s[i:i+2] for i in range(len(s) - 1) } def string_similarity(str1, str2): ''' Perform bigram comparison between two strings and return a percentage match in