tuples

Convert a tuple of lists of the same length to a list of tuples for arbitrary — Haskell

与世无争的帅哥 提交于 2019-12-11 04:37:52
问题 I'm trying to convert a tuple of lists of the same length: ([Int], [Int], ..) to a list of tuples [(Int, Int, ...)] . This can be accomplished for predetermined sizes with the following code: buildList :: ([a], [a], [a], [a], [a], [a], [a], [a]) -> [(a, a, a, a, a, a, a, a)] buildList ([], [], [], [], [], [], [], []) = [] buildList (a:as, b:bs, c:cs, d:ds, e:es, f:fs, g:gs, h:hs) = (a, b, c, d, e, f, g, h) : buildList (as, bs, cs, ds, es, fs, gs, hs) As you can probably see, this isn't going

Most pythonic way to remove tuples from a list if first element is a duplicate

旧时模样 提交于 2019-12-11 04:28:18
问题 The code I have so far is pretty ugly: orig = [(1,2),(1,3),(2,3),(3,3)] previous_elem = [] unique_tuples = [] for tuple in orig: if tuple[0] not in previous_elem: unique_tuples += [tuple] previous_elem += [tuple[0]] assert unique_tuples == [(1,2),(2,3),(3,3)] There must be a more pythonic solution. 回答1: If you don't care which tuple round you return for duplicates, you could always convert your list to a dictionary and back: >>> orig = [(1,2),(1,3),(2,3),(3,3)] >>> list(dict(orig).items()) [

“Undirected” tuple comparison

天大地大妈咪最大 提交于 2019-12-11 03:46:59
问题 I am currently working on an undirected graph in python, where the edges are represented by tuples (edge between A and B is represented by either (A,B) or (B,A)). I was wondering whether there is a tuple operation that performs an undirected comparison of tuples like this: exp1 = undirected_comp((A,B), (B,A)) #exp1 should evaluate to True exp2 = undirected_comp((A,B), (A,C)) #exp2 should evaluate to False 回答1: not exactly, but in general, you can do this kind of comparison with set (A,B) ==

Type sensitive tuple visitor

让人想犯罪 __ 提交于 2019-12-11 03:41:48
问题 Suppose I have a std::tuple made up of types like struct A { static void tip(); }; struct B { static void tip(); }; struct Z { }; std::tuple<const A&,const B&,const Z&> tpl; Yes, I need separate A , B . (The implementation of ::tip() differs for each type.) What I try to implement is a type-sensitive "visitor" that iterates through the tuple starting from the beginning to the end. Upon visiting a particular element of type T a function should be called depending on whether T has the ::tip()

Getting a tuple in a Dafaframe into multiple rows

亡梦爱人 提交于 2019-12-11 03:37:00
问题 I have a Dataframe, which has two columns (Customer, Transactions). The Transactions column is a tuple of all the transaction id's of that customer. Customer Transactions 1 (a,b,c) 2 (d,e) I want to convert this into a dataframe, which has customer and transaction id's, like this. Customer Transactions 1 a 1 b 1 c 2 d 2 e We can do it using loops, but is there a straight 1 or 2 lines way for doing that. 回答1: You can use DataFrame constructor: df = pd.DataFrame({'Customer':[1,2], 'Transactions

Search in Item2 in List<Tuple>

我只是一个虾纸丫 提交于 2019-12-11 03:28:52
问题 I've been struggling. How do I write this: /* initialization */ List<Tuple<string, string, string>> mytuple = new List<Tuple<string, string, string>>(); //pseudocode if(mytuple.Contains("hello") in Item2) { Console.Write("Success"); } 回答1: /* initialization */ List<Tuple<string, string, string>> mytuple = new List<Tuple<string, string, string>>(); bool containsHello = mytuple.Any(c=>c.Item2.Contains("hello")); if(containsHello ) { Console.Write("Success"); } 回答2: You can use linq to check it:

How do tuples work in VS2012 ?

旧街凉风 提交于 2019-12-11 03:16:09
问题 Visual studio 2012 features, tuples but not variadic templates. How is this done, how one implements tuples without the use of varadic templates ? 回答1: In a few words, Microsoft did exactly the same thing they did to implement tuple-like datatypes in .NET previously: Creating many versions, each with a fixed number of parameters. Check the .NET documentation. "How is this done, how one implements tuples without the use of varadic templates" Note that what Microsoft did is the easy way: C++

runtime error in Haskell

跟風遠走 提交于 2019-12-11 03:12:28
问题 My program is searching in list of tuples I wrote it as the following import List data BookInfo = Book Int String [String] deriving(Show) enter :: Int->String->[String]->BookInfo enter id name subject=Book id name subject bookId (Book id _ _ ) = id index :: BookInfo -> Int index (Book id name subject) = bookId (Book id name subject) arrayentering ::BookInfo->[BookInfo]->[BookInfo] arrayentering (Book id name subject) [] =[(Book id name subject)] arrayentering (Book _ " " [" "]) [] =[]

Tuple and CSV Reader in Python

泄露秘密 提交于 2019-12-11 03:08:55
问题 Attempting something relatively simple. First, I have dictionary with tuples as keys as follows: (0,1,1,0): "Index 1" I'm reading in a CSV file which has a corresponding set of fields with various combinations of those zeroes and ones. So for example, the row in the CSV may read 0,1,1,0 without any quoting. I'm trying to match the combination of zeroes and ones in the file to the keys of the dictionary. Using the standard CSV module for this However the issue is that the zeroes and ones are

Write a sequence of tuples to a csv file f#

本秂侑毒 提交于 2019-12-11 02:57:27
问题 Iam trying to write a sequence of tuples to a csv, but the normal File.WriteAllLines is overloaded by a sequence of tuples. I have tried therefore to flatten my tuples into a sequence of strings. Here is my code:- open System;; open Microsoft.FSharp.Reflection;; let tupleToString (t: string * float) = if FSharpType.IsTuple(t.GetType()) then String.Format("{0},{1}", fst t, snd t) else "";; let testTuple = ("monkey", 15.168);; tupleToString(testTuple);; let testSeqTuple = [("monkey", 15.168); (