tuples

Working with list of tuples

浪尽此生 提交于 2019-12-04 15:04:15
I've been trying to solve this, but I just can't figure it out. So, I've a list with tuples, for example: [("Mary", 10), ("John", 45), ("Bradley", 30), ("Mary", 15), ("John", 10)] and what I want to get is a list with also tuples where, if the name is the same, the numbers of those tuples should be added and, if not, that tuple must be part of the final list too, exemplifying: [("Mary",25), ("John", 55), ("Bradley", 30)] I don't know if I explained myself really well, but I think you'll probably understand with the examples. I've tried this, but it doesn't work: test ((a,b):[]) = [(a,b)] test

Create List of Tuples from List using LINQ

本秂侑毒 提交于 2019-12-04 14:32:00
I'm trying to create a list of tuples from a list using LINQ, but can't work out how to do it. What I've got is various data in an external file, which I'm reading sections using a standard method into a List<Single> , I then need to turn this into lists of groups of sequential elements of this list (which may have a variable number of elements in the groups). To state it another way: List<Single> with n elements goes to List<Tuple<Single, Single>> with (n/2) elements or List<Single> with n elements goes to List<Tuple<Single, Single, Single>> with (n/3) elements I couldn't work out how to do

Why tuple is not mutable in Python? [duplicate]

怎甘沉沦 提交于 2019-12-04 13:18:28
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Why are python strings and tuples are made immutable? What lower-level design makes tuple not mutable in Python? Why this feature is useful? 回答1: A few reasons: Mutable objects like lists cannot be used as dictionary keys or set members in Python, since they are not hashable. If lists were given __hash__ methods based on their contents, the values returned could change as the contents change, which violates the

scala zip list to tuple

旧城冷巷雨未停 提交于 2019-12-04 13:17:37
Working with JodaTime, trying to convert a List[LocalDate] to Tuple2[JodaTime, JodaTime] so I can do multi-assigment like so: val(expire, now) = List(row.expireDate, new JodaDate) zip (_.toDateTimeAtStartOfDay.getMillis) which of course does not compile. Is there a similarly concise way to do the above? I know I can just do it manually: val(expire, now) = (row.expireDate.toDateTimeAtStartOfDay.getMillis, new JodaDate().toDateTimeAtStartOfDay.getMillis) but that's a bit ugly val Seq(expire, now) = Seq(row.expireDate, new JodaDate).map(_.toDateTimeAtStartOfDay.getMillis) What you want (assuming

Initialize tuple of references with reference to tuple

隐身守侯 提交于 2019-12-04 10:36:04
If I have the code #include <tuple> using Vec3 = std::tuple<float, float, float>; using Vec3Ref = std::tuple<float&, float&, float&>; void stuff () { Vec3 foo (0,0,0); Vec3Ref bar (foo); } I get the error /usr/include/c++/4.6/tuple:100:4: error: binding of reference to type 'float' to a value of type 'const float' drops qualifiers : _M_head_impl(std::forward<_UHead>(__h)) { } ^ ~~~~~~~~~~~~~~~~~~~~~~~~~ //snip... /usr/include/c++/4.6/tuple:257:11: note: in instantiation of function template specialization 'std::_Tuple_impl<0, float &, float &, float &>::_Tuple_impl <float, float, float>'

Storing return values of functions in a tuple

两盒软妹~` 提交于 2019-12-04 10:21:02
Consider #include <tuple> template <typename... F> auto execute (F... f) { return std::make_tuple(f(0)...); } int foo(int) { return 5; } int bar(int) { return 3; } int main() { auto tuple = execute(foo, bar); } What is a good workaround so that bar can return void? I tried this, but it won't compile: #include <tuple> struct Void { }; template <typename T> T check(T n) { return n; } Void check(void) { return Void{}; } template <typename... F> auto execute (F... f) { return std::make_tuple(check(f(0))...); } int foo(int) { return 5; } void bar(int) { } int main() { auto tuple = execute(foo, bar)

How to convert Tuple to AnyObject in Swift

本秂侑毒 提交于 2019-12-04 10:14:29
Following piece of code compiles with error: Error:(112, 20) type '(String, Int)' does not conform to protocol 'AnyObject' func myMethode() { aMethodeThatICanNotChange { let a = ("John",7) return a // Error:(112, 20) type '(String, Int)' does not conform to protocol 'AnyObject' } } func aMethodeThatICanNotChange(closure: () -> AnyObject) { // do something with closure } How can I cast/convert a Tuple to AnyObject? How can I cast/convert a Tuple to AnyObject? Simply put, you can't. Only class types (or types that are bridged to Foundation class types) can be cast to AnyObject. A tuple is not a

F# - On the parameters passed to C# methods - are they tuples or what?

和自甴很熟 提交于 2019-12-04 10:03:09
问题 I've read many times that Assemblies generated from F# or any other .NET language are (almost) indistinguishable. I was then experimenting with F# and C# interop on .NET 4 (beta 2). I created a new solution, and a C# project, with the following class: public class MyClass { public static int Add(int a, int b) { return a + b; } } Then, on a F# project, after referencing the C# project, I tried: MyClsas.Add(4, 5) |> printfn "%d" // prints 9 (no kidding!) So far so good. Then another sentence I

Tuple syntax in VS 2017

。_饼干妹妹 提交于 2019-12-04 09:58:51
问题 In VS2017 RC, when you tried to use new tuple syntax, you received the following error: CS8179 Predefined type 'System.ValueTuple`X' is not defined or imported In order to use tuple syntax, you had to manually import ValueTuple nuget package into the project. Not a big deal, as it was pre-release version and I thought it will be changed in RTM so it will be enabled by default. Unfortunately in the final release version it is still the case and you have to download nuget package for every

Python - print tuple elements with no brackets

試著忘記壹切 提交于 2019-12-04 08:23:08
问题 I'm looking for a way to print elements from a tuple with no brackets Heres my tuple: mytuple = [(1.0,),(25.34,),(2.4,),(7.4,)] I converted this to a list to make it easier to work with mylist == list(mytuple) then i did the following for item in mylist: print item.strip() but i get the following error 'tuple' object has no attribute 'strip' which is strange because I thought i converted to a list? what I expect to see as the final result is something like 1.0, 25.34, 2.4, 7.4 or 1.0, ,23.43,