tuples

Is there a version of the class Tuple whose Items properties are not readonly and can be set? [duplicate]

萝らか妹 提交于 2019-12-04 22:15:18
This question already has an answer here : Class inheritance: recreate base class items (or instance) from a property of the inherited class (1 answer) Closed 4 years ago . I want to know whether there is a built-in version of the class Tuple whose Items properties are not readonly and can be set. Or can someone provide me such a version? I am searching for a solution that implements the base functions of the Tuple class, ( Equals , GetHashCode ) Ritch Melton No, as mentioned a Tuple<> is intended to be immutable. I use a custom Pair class if I need a mutable type that does the same thing,

Are there tuples in PHP?

走远了吗. 提交于 2019-12-04 22:12:04
I know in Python and other languages we have access to tuples to better facilitate, semantically or otherwise, the structuring of data. My question is: Does PHP have tuples? If not, what is the nearest facility? Daniel Williams PHP's only real built in data structure that people use for everything is the array. Arrays in PHP are all hash tables and can have either numeric or string indexes and can contain anything (usually more arrays). There are a few array constructs that work like tuples. See http://us1.php.net/manual/en/language.types.array.php http://us1.php.net/list List is very

Swift - create a fixed length array enforced at compile time

空扰寡人 提交于 2019-12-04 19:47:28
I want to enforce (compile time) array with 5 elements of a particular type I couldn't find a solution so resorted to a workaround by creating a tuple (This is abusive I know) typealias FiveElementArray = (MyType,MyType,MyType,MyType,MyType) // mock array by using typed tuple It works for my needs - until I need to access an element by index at runtime. For instance: var DB = FiveElementArray // the tuple of 5 elements tableView(tableView : UITableView,cellForRowAtIndexPath:indexPath) -> UITableViewCell { // would like to populate with the value at index DB[indexpath.row] // no such syntax for

How to use priority queues in Scala?

杀马特。学长 韩版系。学妹 提交于 2019-12-04 18:11:09
问题 I am trying to implement A* search in Scala (version 2.10), but I've ran into a brick wall - I can't figure out how to use Scala's Priority Queue. It seems like a simple task, but searching on Google didn't turn up anything (except for a single code sample that stopped working back in version 2.8) I have a set of squares, represented by (Int, Int) s, and I need to insert them with priorities represented by Int s. In Python it's pretty simple, since you just have a list of key, value pairs and

Can I use an object (an instance of a class) as a dictionary key in Python?

雨燕双飞 提交于 2019-12-04 17:56:05
问题 I want to use a class instance as a dictionary key, like: classinstance = class() dictionary[classinstance] = 'hello world' Python seems to be not able to handle classes as dictionary key, or am I wrong? In addition, I could use a Tuple-list like [(classinstance, helloworld),...] instead of a dictionary, but that looks very unprofessional. Do you have any clue for fixing that issue? 回答1: Your instances need to be hashable. The python glossary tells us: An object is hashable if it has a hash

Multidimension histogram in python

岁酱吖の 提交于 2019-12-04 17:35:09
I have a multidimensional histogram H=histogramdd((x,y,z),bins=(nbins,nbins,nbins),range=((0,1),(0,1),(0,1))) I need to print in an array the values of H which are different from zero and I also need to know the coordinate/the bins where this happens. I am not familiar with tuples. Can you help me? use where to find the index of nozeros in H, and use the index to get the coordinate: import numpy as np x = np.random.random(1000) y = np.random.random(1000) z = np.random.random(1000) nbins = 10 H, [bx, by, bz]=np.histogramdd((x,y,z),bins=(nbins,nbins,nbins),range=((0,1),(0,1),(0,1))) ix, iy, iz =

Feeding tuple into function such as printfn

北战南征 提交于 2019-12-04 17:28:20
问题 I want to give a tuple to a printf function: let tuple = ("Hello", "world") do printfn "%s %s" tuple This, of course, does not work, compiler first says, that it needs string instead of string*string . I write it as follows: let tuple = ("Hello", "world") do printfn "%s %s" <| fst tuple Then compiler reasonably notes that now I have function value of type string -> unit . Makes sense. I can write let tuple = ("Hello", "world") do printfn "%s %s" <| fst tuple <| snd tuple And it works for me.

Python list_of_tuples: sum second val of each tuple, only if first val of tuple == something

淺唱寂寞╮ 提交于 2019-12-04 16:54:18
I have a list of "tagged" tuples...where each tuple is (tag_id, value)...like so: my_list = [(tag_A, 100), (tag_A, 200), (tag_A, 300), (tag_A, 400), (tag_B, 400), (tag_B, 600)] I want to sum the values of each tuple with the same tag...so that: sum_of_all_values_with_tag_A() = 1000 sum_of_all_values_with_tag_B() = 1000 I can't figure out a simple Pythonic way of doing that. sum(set(value for tag_id, value in my_list)) ...returns the sum of ALL the values. I suppose I can wrap that with a for or a while loop, so that only tuples with the tag I want to sum are touched by that expression...? I

python all possible pairs of 2 list elements, and getting the index of that pair

北慕城南 提交于 2019-12-04 15:39:16
问题 let's say I have two lists: a = list(1,2,3) b = list(4,5,6) So I can have 9 pairs of these list members: (1,4) (1,5) (1,6) (2,4) (2,5) (2,6) (3,4) (3,5) (3,6) Now, given two list members like above, can I find out the pair's index? Like (1,4) from above would be the 1st pair. 回答1: And to complete the answer and stay in the example: import itertools a = [1, 2, 3] b = [4, 5, 6] c = list(itertools.product(a, b)) idx = c.index((1,4)) But this will be the zero-based list index, so 0 instead of 1.

How can I access each element of a pair in a pair list?

ぃ、小莉子 提交于 2019-12-04 15:31:20
问题 I have a list called pairs. pairs = [("a", 1), ("b", 2), ("c", 3)] And I can access elements as: for x in pairs: print x which gives output like: ('a', 1) ('b', 2) ('c', 3) But I want to access each element in each pair, like in c++, if we use pair<string, int> we are able to access, first element and second element by x.first , and x.second .eg. x = make_pair("a",1) x.first= 'a' x.second= 1 How can I do the same in python? 回答1: Use tuple unpacking: >>> pairs = [("a", 1), ("b", 2), ("c", 3)]