tuples

Build dict from list of tuples combining two multi index dfs and column index

情到浓时终转凉″ 提交于 2019-12-11 07:32:05
问题 I have two multi-index dataframes: mean and std arrays = [['A', 'A', 'B', 'B'], ['Z', 'Y', 'X', 'W']] mean=pd.DataFrame(data={0.0:[np.nan,2.0,3.0,4.0], 60.0: [5.0,np.nan,7.0,8.0], 120.0:[9.0,10.0,np.nan,12.0]}, index=pd.MultiIndex.from_arrays(arrays, names=('id', 'comp'))) mean.columns.name='Times' std=pd.DataFrame(data={0.0:[10.0,10.0,10.0,10.0], 60.0: [10.0,10.0,10.0,10.0], 120.0:[10.0,10.0,10.0,10.0]}, index=pd.MultiIndex.from_arrays(arrays, names=('id', 'comp'))) std.columns.name='Times'

sort tuples in lists in python

时光总嘲笑我的痴心妄想 提交于 2019-12-11 07:29:09
问题 i was wondering if there was any simple way around sorting tuples in lists in python, for instance if i have a list: list01 = ([a,b,c],[b,a,d],[d,e,c],[a,f,d]) and i sorted it, i would get: ([a,b,c],[a,b,d],[c,d,e],[a,d,f])? or even: ([a,b,c],[a,b,d],[a,d,f],[c,d,e]) if that's easier Thanx in advance:) 回答1: >>> list01 = (['a','b','c'],['b','a','d'],['d','e','c'],['a','f','d']) >>> map(sorted, list01) [['a', 'b', 'c'], ['a', 'b', 'd'], ['c', 'd', 'e'], ['a', 'd', 'f']] >>> sorted(map(sorted,

How are Tuples destructured into references?

别说谁变了你拦得住时间么 提交于 2019-12-11 06:06:26
问题 I'm looking at the Condvar example and am curious how the tuples pair and pair2 are destructured: let pair = Arc::new((Mutex::new(false), Condvar::new())); let pair2 = pair.clone(); // ... thread::spawn(move|| { let &(ref lock, ref cvar) = &*pair2; // ... } Removing the & from pair2 : let &(ref lock, ref cvar) = *pair2; gives a compiler error as I expected: 11 | let &(ref lock, ref cvar) = *pair2; | ^^^^^^^^^^^^^^^^^^^^^ expected tuple, found reference | = note: expected type `(std::sync:

List of Tuples to multi-dimensional array

ε祈祈猫儿з 提交于 2019-12-11 05:52:17
问题 Related, but not Duplicate (it's a completely different language, PHP not C#): How to create multi-dimensional array from a list? How would I go about converting a list of 'Tuple's to string[,]? This is part of a web crawler, but i'm messing with conversions of lists and arrays just out of curiosity. Here's the method. private string[,] getimages(string url) { List<Tuple<string, string>> images = new List<Tuple<string, string>>(); string raw = client.DownloadString(url); while (raw.Contains("

Why Scala REPL shows tuple type for Map expression?

一曲冷凌霜 提交于 2019-12-11 05:34:31
问题 Scala REPL gives the same type for both expressions - (tuple? -- strange!). Yet ("a" ->1) which is a Map I can add to map and ("a", 1) can not. Why Scala REPL shows tuple type type for Map expression? scala> :t ("a" -> 1) (String, Int) scala> :t ("a",1) (String, Int) scala> val m = Map.empty[String, Int] m: scala.collection.immutable.Map[String,Int] = Map() scala> m + ("a",1) <console>:9: error: type mismatch; found : String("a") required: (String, ?) m + ("a",1) ^ scala> m + ("a" ->1) res19:

Looking for a way to optimize this algorithm for parsing a very large string

岁酱吖の 提交于 2019-12-11 05:26:24
问题 The following class parses through a very large string (an entire novel of text) and breaks it into consecutive 4-character strings that are stored as a Tuple. Then each tuple can be assigned a probability based on a calculation. I am using this as part of a monte carlo/ genetic algorithm to train the program to recognize a language based on syntax only (just the character transitions). I am wondering if there is a faster way of doing this. It takes about 400ms to look up the probability of

How to identify “keys” of a tuple/list of 3-item tuples?

99封情书 提交于 2019-12-11 05:19:49
问题 Given a table of revenue values thus: A key point to note (and the core of my question) is the the brand name will almost always, but not always, contain the corresponding product name. In the case of last Banana entry, it doesn't. I will extract a dict of Brand<->Revenue pairs, fist accounting for those brands that have multiple entries, and summing in those cases, using the approach described here. So: revenuePerBrandDict = {} brandRevenueTuples = [] i=0 for brand in ourTab.columns[1][1:-1]

How to make a tuple including a numpy array hashable?

无人久伴 提交于 2019-12-11 05:14:52
问题 One way to make a numpy array hashable is setting it to read-only. This has worked for me in the past. But when I use such a numpy array in a tuple, the whole tuple is no longer hashable, which I do not understand. Here is the sample code I put together to illustrate the problem: import numpy as np npArray = np.ones((1,1)) npArray.flags.writeable = False print(npArray.flags.writeable) keySet = (0, npArray) print(keySet[1].flags.writeable) myDict = {keySet : 1} First I create a simple numpy

'int' object is not iterable when I'm not trying to iterate

末鹿安然 提交于 2019-12-11 05:06:41
问题 The following piece of code attempts to create a map that shows the minimum number of moves it would take to get from each square on that map to the specified location. The function as a whole is largely irrelevant to the problem, but I thought I should provide my problem in context. I've also imported deque from collections. The strange part comes in at line 7. I get TypeError: 'int' object not iterable. But the statement "distance_from_loc, f_loc = squares_to_check.popleft()" shouldn't be

Extract associated value of enum case into a tuple

99封情书 提交于 2019-12-11 05:05:07
问题 I know how to extract associated values in enum cases using switch statement: enum Barcode { case upc(Int, Int, Int, Int) case quCode(String) } var productBarcode = Barcode.upc(8, 10, 15, 2) switch productBarcode { case let .upc(one, two, three, four): print("upc: \(one, two, three, four)") case .quCode(let productCode): print("quCode \(productCode)") } But I was wondering if there is a way to extract associated value using tuples. I tried let (first, second, third, fourth) = productBarcode