tuples

Finding the intersection in two lists of tuples regardless of tuple order

混江龙づ霸主 提交于 2021-02-05 07:33:08
问题 I have two lists of tuples listA = [('1','2'),('3','4'),('5','6')] listB = [('2','1'),('7','8')] I want to find the intersection of them even if the order of the tuple in the second list is different. So, for the example above: intersection = [('1','2')] the intersection should return the tuple above though it is not in the same order in listB How can I do that in python the most efficient way? because each of my list has around 2000 tuples. 回答1: >>> set(map(frozenset, listA)) & set(map

Finding the intersection in two lists of tuples regardless of tuple order

余生长醉 提交于 2021-02-05 07:32:45
问题 I have two lists of tuples listA = [('1','2'),('3','4'),('5','6')] listB = [('2','1'),('7','8')] I want to find the intersection of them even if the order of the tuple in the second list is different. So, for the example above: intersection = [('1','2')] the intersection should return the tuple above though it is not in the same order in listB How can I do that in python the most efficient way? because each of my list has around 2000 tuples. 回答1: >>> set(map(frozenset, listA)) & set(map

numpy where with array of tuples

主宰稳场 提交于 2021-02-05 06:48:25
问题 Why can't I find the location of a tuple in an array? Afterall, the bottom expression prints True foo = numpy.array([(5, 30), (5,), 5]) bar = numpy.where(foo==foo[0]) print(bar) Prints (array([], dtype=int64),) print((5,30)==foo[0]) Prints True 回答1: It's because: import numpy foo = numpy.array([(5, 30), (5,), 5]) bar = numpy.where(foo==foo[0]) print(foo==foo[0]) False That's why you're getting an empty array. A list comprehension alternative is [v for v in foo if v == foo[0]] will result in [

numpy where with array of tuples

眉间皱痕 提交于 2021-02-05 06:48:25
问题 Why can't I find the location of a tuple in an array? Afterall, the bottom expression prints True foo = numpy.array([(5, 30), (5,), 5]) bar = numpy.where(foo==foo[0]) print(bar) Prints (array([], dtype=int64),) print((5,30)==foo[0]) Prints True 回答1: It's because: import numpy foo = numpy.array([(5, 30), (5,), 5]) bar = numpy.where(foo==foo[0]) print(foo==foo[0]) False That's why you're getting an empty array. A list comprehension alternative is [v for v in foo if v == foo[0]] will result in [

Scala 3 - Extract Tuple of wrappers and InverseMap on First Order Type

帅比萌擦擦* 提交于 2021-02-05 06:10:23
问题 I am trying to create a function, which takes a tuple of higher-kinded types and applies a function to the types within the higher-kinded types. In the example below, there is a trait Get[A] which is our higher-kinded type. There is also a tuple of Get's: (Get[String],Get[Int]) as well as function from (String,Int) => Person . Scala-3 has a Match-Type called InverseMap which converts the type (Get[String], Get[Int]) into what is essentially the type (String,Int). So the ultimate goal is to

Scala 3 - Extract Tuple of wrappers and InverseMap on First Order Type

非 Y 不嫁゛ 提交于 2021-02-05 06:10:15
问题 I am trying to create a function, which takes a tuple of higher-kinded types and applies a function to the types within the higher-kinded types. In the example below, there is a trait Get[A] which is our higher-kinded type. There is also a tuple of Get's: (Get[String],Get[Int]) as well as function from (String,Int) => Person . Scala-3 has a Match-Type called InverseMap which converts the type (Get[String], Get[Int]) into what is essentially the type (String,Int). So the ultimate goal is to

Missing 3 required positional arguments Python

99封情书 提交于 2021-02-04 21:54:32
问题 Right so I'm working on a python code and I get this type error, "TypeError: printE() missing 3 required positional arguments: 'emp2', 'emp3', and 'emp4'" for emmp in employee: print(printE(emmp)) def printE(emp1, emp2, emp3, emp4): emp1 = "{}, {}, {}, {}".format(emp1[0], ' '.join(emp1[1:-2])) emp2 = "{}, {}, {}, {}".format(emp2[1], ' '.join(emp2[2:-3])) emp3 = "{}, {}, {}, {}".format(emp3[2], ' '.join(emp3[3])) emp4 = "{}, {}, {}, {}".format(emp4[3], ' '.join(emp4[0:-1])) print("{:10s} {:15s

How to print out elements of tuple one per line

拥有回忆 提交于 2021-02-04 19:45:26
问题 Below is my code for a DNA string neighboring question: chars = "ACGT" def neighbors(pattern, d): assert(d <= len(pattern)) if d == 0: return [pattern] r2 = neighbors(pattern[1:], d-1) r = [c + r3 for r3 in r2 for c in chars if c != pattern[0]] if (d < len(pattern)): r2 = neighbors(pattern[1:], d) r += [pattern[0] + r3 for r3 in r2] return r def neighbors2(pattern, d): return ([neighbors(pattern, d2) for d2 in range(d + 1)], []) print (neighbors2("ACG", 1)) The output is below: ([['ACG'], [

How to unpack optional items from a tuple? [duplicate]

只谈情不闲聊 提交于 2021-02-04 17:57:05
问题 This question already has answers here : Idiomatic way to unpack variable length list of maximum size n (5 answers) How to unpack tuple of length n to m<n variables [duplicate] (5 answers) Closed 6 years ago . I have a list of some input values, of which the first couple of mandatory and the last couple optional. Is there any easy way to use tuple unpacking to assign these to variables, getting None if the optional parameters are missing. eg. a = [1,2] foo, bar, baz = a # baz == None ideally

create a list of tuples from csv file

早过忘川 提交于 2021-02-04 16:16:19
问题 I am python beginner struggling to create and save a list containing tuples from csv file in python. The code I got for now is: def load_file(filename): fp = open(filename, 'Ur') data_list = [] for line in fp: data_list.append(line.strip().split(',')) fp.close() return data_list and then I would like to save the file def save_file(filename, data_list): fp = open(filename, 'w') for line in data_list: fp.write(','.join(line) + '\n') fp.close() Unfortunately, my code returns a list of lists, not