tuples

Delete duplicate tuples independent of order with same elements in generator Python 3.5

喜你入骨 提交于 2019-12-02 13:30:51
I have a generator of tuples and I need to delete tuples containing same elements. I need this output for iterating. Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3)) Output= ((1, 1), (1, 2), (1, 3)) Order of output doesn't matter. I have checked this question but it is about lists: Delete duplicate tuples with same elements in nested list Python I use generators to achieve fastest results as the data is very large. You can normalize the data by sorting it, then add it to a set to remove duplicates >>> Input = ((1, 1), (1, 2), (1, 3), (3, 1), (3, 2), (3, 3)) >>> Output = set(tuple

Get all 1-k tuples in a n-tuple

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-02 13:28:48
With n=5 and k=3 the following loop will do it List<String> l=new ArrayList<String>(); l.add("A");l.add("B");l.add("C");l.add("D");l.add("E"); int broadcastSize = (int) Math.pow(2, l.size()); for (int i = 1; i < broadcastSize; i++) { StringBuffer buffer = new StringBuffer(50); int mask = i; int j = 0; int size=0; System.out.println(); while (mask > 0) { if ((mask & 1) == 1) { System.out.println(".. "+mask); buffer.append(l.get(j)); if (++size>3){ buffer = new StringBuffer(50); break; } } System.out.println(" "+mask); mask >>= 1; j++; } if (buffer.length()>0) System.out.println(buffer.toString(

python sort tuple by different criteria

泄露秘密 提交于 2019-12-02 13:16:24
I have a list a = [(1,'a'), (1,'b'), (2,'c')] , and I want to get this list: [(2,'c'), (1,'a'), (1,'b')] If I do this: sorted(a, reverse=True) I can only get: [(2,'c'), (1,'b'), (1,'a')] How can I get the list I want? If you want to preserve the sort order in the original list, but sort only by the first element, you can do >>> from operator import itemgetter >>> a = [(1,'a'), (1, 'x'), (1,'b'), (2,'c')] >>> sorted(a, key=itemgetter(0), reverse=True) [(2, 'c'), (1, 'a'), (1, 'x'), (1, 'b')] In Python the sort and sorted functions use the TimSort algorithm, which is a stable sort. Being stable

How can i sort a list of tuples by one of it's values and then the other? [duplicate]

醉酒当歌 提交于 2019-12-02 13:06:18
This question already has an answer here: Sorting a Python list by two fields 7 answers I'll get to the point,i have this: ocurrencias = [('quiero', 1), ('aprender', 1), ('a', 1), ('programar', 1), ('en', 1), ('invierno', 2), ('hace', 1), ('frio', 1), ('este', 1)] I want to sort it by the second value of the tuples and then by their string value and then print every element to get this: output:invierno 2 a 1 aprender 1 en 1 este 1 frio 1 hace 1 programar 1 quiero 1 Don't know if i'm making it clear enough,but i'm not really proficient at english so forgive me. Thanks in advance Use sorted with

How to get the count of Custom tuples against two lists

荒凉一梦 提交于 2019-12-02 13:04:36
Please help me to get the counter for the list SS2 in list SS1 in PYTHON using from collections import Counter or any other fastest way SS1 = [(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] SS2=[(1, 2, 3), (1, 2, 4), (1, 2, 5), (1, 2, 6), (1, 3, 4), (1, 3, 5), (1, 3, 6), (1, 4, 5), (1, 4, 6), (1, 5, 6), (2, 3, 4), (2, 3, 5), (2, 3, 6), (2, 4, 5), (2, 4, 6), (2, 5, 6), (3, 4, 5), (3, 4, 6), (3, 5, 6), (4, 5, 6)] Here is what i have tried and i don't know how to get the count for (1,2,4) th elements of the each tuple SS1=[(1, 2, 3, 4, 5), (1

converting string to tuple in python

妖精的绣舞 提交于 2019-12-02 12:22:55
问题 I have a string returnd from a software like "('mono')" from that I needed to convert string to tuple . that I was thinking using ast.literal_eval("('mono')") but it is saying malformed string. 回答1: Since you want tuples, you must expect lists of more than element in some cases. Unfortunately you don't give examples beyond the trivial (mono) , so we have to guess. Here's my guess: "(mono)" "(two,elements)" "(even,more,elements)" If all your data looks like this, turn it into a list by

How do I add x tuples into a list x number of times?

寵の児 提交于 2019-12-02 11:47:39
I have a question about tuples and lists in Haskell. I know how to add input into a tuple a specific number of times. Now I want to add tuples into a list an unknown number of times; it's up to the user to decide how many tuples they want to add. How do I add tuples into a list x number of times when I don't know X beforehand? hammar When doing functional programming, it is often better to think about composition of operations instead of individual steps. So instead of thinking about it like adding tuples one at a time to a list, we can approach it by first dividing the input into a list of

How to fix 'Non-exhaustive patterns in function' error in haskell?

时光怂恿深爱的人放手 提交于 2019-12-02 11:33:28
I am trying to create a function which takes a list of tuples as argument and sort the by the second element. It doesn't print anytning else, just the error '*** Exception: main.hs:20:1-76: Non-exhaustive patterns in function sortWords' Here is the code: sortWords :: [(String, Int)] -> [(String, Int)] sortWords [(str,num)] = sortBy (\x y -> compare (snd x) (snd y)) [(str,num)]` And here is how I call the function main = do putStrLn $ show $ sortWords [("friend",1),("she",2)] I have to say that I run my program on http://Repl.it website Thanks! sortWords [(str,num)] = Your function definition

Pattern matching equality on tuples in Haskell

ⅰ亾dé卋堺 提交于 2019-12-02 10:40:17
问题 For this function on symmetric equality over tuples, symEq :: Eq a => (a,a) -> (a,a) -> Bool symEq (x,y) (u,v) = (x,y) == (u,v) || (x,y) == (v,u) would like to rewrite it using pattern matching as follows, symEq' :: Eq a => (a,a) -> (a,a) -> Bool symEq' (x,y) (x,y) = True symEq' (x,y) (y,x) = True symEq' _ _ = False The latter fails with error on conflicting definitions for x and y . How to rewrite symEq and take benefit of pattern matching ? 回答1: Unlike in some languages (I hear Erlang works

Modifying all the tuples in a Python list

余生长醉 提交于 2019-12-02 10:33:04
问题 I have a list containing tuples with a standard format: bar_list = [(bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4)...] Though I want to iterate through each tuple in the list and for each make specific modifications such as: foo0 = bar1 foo1 = get_foo(foo0) #get_foo(var) being a function foo2 = bar2 foo3 = bar3/2 And then repackage the revalued tuples in another list: foo_list = [(foo1, foo2, foo3), (foo1, foo2, foo3), (foo1, foo2, foo3)...] How could I