tuples

Python - short way to unpack list for string formatting operator?

巧了我就是萌 提交于 2019-12-01 04:44:18
Variations of the * or ** operators don't seem to work, unfortunately: lstData = [1,2,3,4] str = 'The %s are %d, %d, %d, and %d' % ('numbers', *lstData) Is there an easy way? Use format : str = 'The {} are {}, {}, {}, and {}'.format('numbers', *lstData) see the docs for more details about possible formatting (floats, decimal points, conversion, ..). s = 'The %s are %d, %d, %d, and %d' % tuple(['numbers'] + lstData) >>> data = range(5) >>> 'The {0} are {1}, {2}, {3}, {4} and {5}'.format('numbers', *data) 'The numbers are 0, 1, 2, 3 and 4' 来源: https://stackoverflow.com/questions/7978144/python

Save a tuple in NSUserDefaults

末鹿安然 提交于 2019-12-01 04:24:33
I'm using a tuple to store something like this. var accessLavels: (hasInventoryAccess: Bool, hasPayrolAccess: Bool) accessLavels = (hasInventoryAccess: true, hasPayrolAccess: false) Now I want to save it in NSUserDefaults . NSUserDefaults.standardUserDefaults().setValue(accessLavels, forKey: "AccessLevelKey") NSUserDefaults.standardUserDefaults().synchronize() But I get the following error. Type '(hasInventoryAccess: Bool, hasPayrolAccess: Bool)' does not conform to protocol 'AnyObject' How can I resolve this issue? If its impossible, then any other suggestions to save a tuple is welcome.

c# sorting a List<> using Tuple?

[亡魂溺海] 提交于 2019-12-01 04:22:46
I need to sort a List<> of MediaItem objects by publish date...the publish date is not a property of the item. So my initial intention was to temporarily tack on a publish date property, load em up, sort, and ditch the property. Someone at my work suggested I use Tuple and sort with LINQ...I've got about this far in my snippet test, filling up the List<>...but I'm not sure how to sort by that date property using LINQ. I'll keep looking on my end but if anyone's got any help I'd appreciate it. Thanks. List<Tuple<MediaItem, DateTime>> list = new List<Tuple<MediaItem, DateTime>>(); for (int i = 0

Can a C# named Tuple be used as an MVC page model type?

人走茶凉 提交于 2019-12-01 04:15:05
In C# 7 you can have named tuples: var foo = (Name: "Joe", Age: 42); If I pass this to a MVC model using: return View(foo); Then what syntax should be used in the cshtml file to declare the model? Although this doesn't work, something like... @model (string Name, int Age); As for current time you can't and need to use @model Tuple<string, int> //or @model ValueTuple<string, int> For the difference between the two options: What's the difference between System.ValueTuple and System.Tuple? You can follow on GitHub: Razor throws CompilationFailedException when iterating over named Tuple (I know it

Internals for python tuples

走远了吗. 提交于 2019-12-01 03:29:56
>>> a=1 >>> b=1 >>> id(a) 140472563599848 >>> id(b) 140472563599848 >>> x=() >>> y=() >>> id(x) 4298207312 >>> id(y) 4298207312 >>> x1=(1) >>> x2=(1) >>> id(x1) 140472563599848 >>> id(x2) 140472563599848 until this point I was thinking there will be only one copy of immutable object and that will be shared(pointed) by all the variables. But when I tried, the below steps I understood that I was wrong. >>> x1=(1,5) >>> y1=(1,5) >>> id(x1) 4299267248 >>> id(y1) 4299267320 can anyone please explain me the internals? thefourtheye >>> x1=(1) >>> x2=(1) is actually the same as >>> x1=1 >>> x2=1 In

Empty nested tuples error

扶醉桌前 提交于 2019-12-01 03:26:04
问题 #include <iostream> #include <tuple> int main(){ auto bt=std::make_tuple(std::tuple<>(),std::tuple<std::tuple<>>()); //Line 1 auto bt2=std::make_tuple(std::tuple<>(),std::tuple<>()); //Line 2 } Why does Line 1 gives a compile error while Line 2 compiles fine? (tested in both Gcc&Clang) Is there a possible workaround? error message for clang /usr/include/c++/4.6/tuple:150:50: error: ambiguous conversion from derived class 'std::_Tuple_impl<0, std::tuple<>, std::tuple<std::tuple<> > >' to base

How to use a Tuple as a Key in a Dictionary C#

一世执手 提交于 2019-12-01 03:22:38
I have a Dictionary fieldTracker which takes a Tuple<int, int> as Key and string as value. However, I can't seem to find the right way to access the value. Here is my current code: for (int i = 0; i < 2; i++) { for (int j = 0; j < 5; j++) dict.Add(new Tuple<int, int>(i, j), ""); } dict[(1,1)] = "Hello"; I've searched around a bit in the Microsoft documentation, but can't find the key to this problem. dict[Tuple.Create(1, 1)] = "Hello"; or with C#7 ValueTuple : var dict = new Dictionary<(int, int), string>(); for (int i = 0; i < 2; i++) { for (int j = 0; j < 5; j++) dict.Add((i, j), ""); } dict

ado.net mvc3 tuple using in model and single views

人走茶凉 提交于 2019-12-01 02:51:26
问题 I have the following ADO Model Student Id,Name and Course Id,Name,Student_ID I have made the following view for it @model Tuple<MvcApplication4.Models.Course, MvcApplication4.Models.Student > @{ ViewBag.Title = "Create"; } <h2>Create</h2> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Course</legend> <div class="editor-label"> @Html.LabelFor(model => model.Item1.Name) </div> <div class="editor-field"> @Html.EditorFor(model => model.Item1.Name) @Html

tuple vector and initializer_list

感情迁移 提交于 2019-12-01 02:42:24
I tried to compile the following snippets with gcc4.7 vector<pair<int,char> > vp = {{1,'a'},{2,'b'}}; //For pair vector, it works like a charm. vector<tuple<int,double,char> > vt = {{1,0.1,'a'},{2,4.2,'b'}}; However, for the vector of tuples, the compiler complains: error: converting to ‘std::tuple’ from initializer list would use explicit constructor ‘constexpr std::tuple< >::tuple(_UElements&& ...) [with _UElements = {int, double, char}; = void; _Elements = {int, double, char}]’ The error info spilled by the compiler is total gibberish for me, and I have no idea how were the constructors of

Python - short way to unpack list for string formatting operator?

喜欢而已 提交于 2019-12-01 02:28:39
问题 Variations of the * or ** operators don't seem to work, unfortunately: lstData = [1,2,3,4] str = 'The %s are %d, %d, %d, and %d' % ('numbers', *lstData) Is there an easy way? 回答1: Use format: str = 'The {} are {}, {}, {}, and {}'.format('numbers', *lstData) see the docs for more details about possible formatting (floats, decimal points, conversion, ..). 回答2: s = 'The %s are %d, %d, %d, and %d' % tuple(['numbers'] + lstData) 回答3: >>> data = range(5) >>> 'The {0} are {1}, {2}, {3}, {4} and {5}'