tuples

Get count of tuples in list regardless of elements orders

随声附和 提交于 2019-12-31 05:10:42
问题 I have a list of tuples that looks something like this: my_list = [(1,12),(12,1),(12,1),(20,15),(7,8),(15,20)] I want to get a count of the number combinations regardless of the order. For example, if it were to be simply printed, I'd like the output to be: 1,12 = 3 20,15 = 2 7,8 = 1 Basically, I have a list of connections but the direction doesn't matter, so 1 to 12 is the same as 12 to 1. I have been working on this for a while and can't come up with a clean solution. Everything I've come

AttributeError: 'tuple' object has no attribute 'append'

我们两清 提交于 2019-12-31 04:14:10
问题 Can anyone help me with this code? Jobs = () openFile = open('Jobs.txt') x = 1 while x != 0: Stuff = openFile.readline(x) if Stuff != '': Jobs.append(Stuff) else: x = 0 This code throws: AttributeError: 'tuple' object has no attribute 'append' I'm using Python 3.6. 回答1: In the line: Jobs = () you create a tuple . A tuple is immutable and has no methods to add, remove or alter elements. You probably wanted to create a list (lists have an .append-method). To create a list use the square

Why single element tuple is interpreted as that element in python?

血红的双手。 提交于 2019-12-31 02:42:05
问题 Could anyone explain why single element tuple is interpreted as that element in Python? And Why don't they just print the tuple (1,) as (1) ? See the examples below: >>> (1) 1 >>> ((((1)))) 1 >>> print(1,) 1 >>> print((1,)) (1,) 回答1: A single element tuple is never treated as the contained element. Parentheses are mostly useful for grouping, not for creating tuples; a comma does that. Why don't they just print (1,) as (1)? Probably because printing a builtin container type gives a

Maximum depth of a binary tree in python

*爱你&永不变心* 提交于 2019-12-31 01:52:30
问题 I created a tuple from a binary tree and it looks like this: tuple = (1,(2,(4,5,6),(7,None,8)),(3,9,(10,11,12))) The tree structure becomes more clear by applying indentation: (1, (2, (4, 5, 6 ), (7, None, 8 ) ), (3, 9, (10, 11, 12 ) ) ) I know how to find the maximum depth of the binary tree using recursive method, but I am trying to find the maximum depth using the tuple I created. Can anyone help me with how to do it? 回答1: Recursive method: a = (1,(2,(4,5,6),(7,None,8)),(3,9,(10,11,12)));

Using array as tuple member: Valid C++11 tuple declaration?

北战南征 提交于 2019-12-31 01:05:25
问题 The code below compiles fine with G++ 4.7.2: #include <tuple> std::tuple<float,int[2]> x; With clang++ 3.2, however, the following error is produced: error: array initializer must be an initializer list. If I remove the float type from the tuple declaration, the error disappears. Is the above tuple declaration valid? ( $CXX -std=c++11 -c file.cpp ) 回答1: I don't think there is anything in the Standard that forbids your declaration. However, you will run into problems as soon as you try to

How to filter a set of (int, str) tuples, to return only tuple with min value in first element?

流过昼夜 提交于 2019-12-30 10:50:35
问题 Suppose I have a set of tuples representing URLS with "scores": {(0.75, 'http://www.foo.com'), (0.33, 'http://www.bar.com'), (0.5, 'http://www.foo.com'), (0.66, 'http://www.bar.com')} . What is a concise way for me to filter out duplicate URLS, returning only the URL with the lowest score? That is, from the example set above, I want to get the following set, where each URL appears only once, with the lowest corresponding score from the original set: {(0.5, 'http://www.foo.com'),(0.33, 'http:/

How to find the index of a tuple element from an tuple array? iOS, Swift

旧巷老猫 提交于 2019-12-30 10:27:15
问题 This is inside tableview cellforrowatindexpath var valueArray:[(String,String)] = [] if !contains(valueArray, v: (title,status)) { let v = (title,status) valueArray.append(v) } This is inside didselectrowatIndexPath let cell = self.tableView.cellForRowAtIndexPath(selectedRow!) var newTuple = (cell!.textLabel!.text!, cell!.detailTextLabel!.text!) let index = valueArray.indexOf(newTuple) But i am not getting the index. It is throwing an error cannot convert value of type '(String,String)' to

How can I remove exception “Predefined type 'ValueTuple`2' must be a struct” when debugging?

时光怂恿深爱的人放手 提交于 2019-12-30 09:58:24
问题 I started using the new tuple feature in c# 7.0 but I noticed that neither in the function that returns a tuple nor in its caller is possible to check the variable values in debug mode. Instead an exception is shown: $exception error CS8182: Predefined type 'ValueTuple`2' must be a struct. Is there a way to get rid of that glitch and debug normally? 回答1: It seems a bug that Microsoft has fixed but it will be available in a future update (2017) https://github.com/dotnet/roslyn/pull/16930 回答2:

Value Tuples Vs Anonymous Types Performance

社会主义新天地 提交于 2019-12-30 09:00:08
问题 I'm surprised there isn't a question on this already. C# 7 added value tuples. I'm trying to figure out when I should adopt these features. Take this dictionary for example uses Anonymous Type: var changesTypeMapByEntityState = this.ChangeTracker.Entries() .Where(x => (int)x.State > (int)EntityState.Unchanged) .GroupBy(x => new { Type = x.Entity.GetType(), x.State }) .ToDictionary(x => x.Key, x => x.ToList()); Vs this Dictionary which uses Value Tuples var changesTypeMapByEntityState = this

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

送分小仙女□ 提交于 2019-12-30 08:13:53
问题 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. 回答1: dict[Tuple.Create(1, 1)] = "Hello"; or with C#7 ValueTuple: var dict = new Dictionary<