tuples

Multiplying a list of tuples by a tuple in F#

风格不统一 提交于 2019-12-23 18:10:24
问题 I'm writing some code in F# and I need to multiply and add each element in list of tuples (c,d) by a tuple (a,b) . To clarify, I have some tuple (a,b) And some list of tuples [(c,d),(e,f),(g,h)...] and I'm trying to get [(a*c,b*d),(a*e,b*f),(a*g,b*h)...] and I'm trying to get (with a separate function) [(a+c,b+d),...] I have tried to use List.map to multiply each element in the list by a tuple but I get an error that * is an invalid operator for type tuple. This is how I ended up implementing

Extract different values from list of tuples

情到浓时终转凉″ 提交于 2019-12-23 16:21:31
问题 How to extract a list of different values from following list of tuples? tuple = ((("test", 123), ("test", 465), ("test", 8910), ("test2", 123))) I want to get a list like: different_values = ("test", "test2") Now I want to access all values by this "keys" and get them by a list: test_values = (123, 456, 8910) test2_values = (123) How to do that? 回答1: I'd transform your data to a dictionary of lists: d = {} for k, v in tuples: d.setdefault(k, []).append(v) Now you can access the keys as d

write numpy array with its size to binary file

…衆ロ難τιáo~ 提交于 2019-12-23 15:19:31
问题 I need to write a 2D numpy array to a file, including its dimensions so I can read it from a C++ program and create the corresponding array. I have written some simple code that saves the array and it can be read from C++, but if I try to write the array's size first it always gives me an error. Here's my simple python code: 1 file = open("V.bin","wb") 2 file.write(V.shape) 3 file.write(V) 4 file.close() The second line gives the error, I've also tried: n1, n2 = V.shape file.write(n1) file

Kotlin: How to modify a value in a pair? [duplicate]

穿精又带淫゛_ 提交于 2019-12-23 14:40:44
问题 This question already has an answer here : Why aren't the entries in a Kotlin Pair mutable? (1 answer) Closed 2 years ago . Why can't I change the values in the pair: var p: Pair<Int, String> = Pair(5, "Test") p.first = 3 Error under p.first : Val cannot be reassigned 回答1: Pair, like most data classes, is immutable. Its definition is effectively data class Pair<out A, out B>(val first: A, val second: B) If it were mutable, it could not be covariant in out A and out B , nor would it be safe to

Kotlin: How to modify a value in a pair? [duplicate]

不羁的心 提交于 2019-12-23 14:40:40
问题 This question already has an answer here : Why aren't the entries in a Kotlin Pair mutable? (1 answer) Closed 2 years ago . Why can't I change the values in the pair: var p: Pair<Int, String> = Pair(5, "Test") p.first = 3 Error under p.first : Val cannot be reassigned 回答1: Pair, like most data classes, is immutable. Its definition is effectively data class Pair<out A, out B>(val first: A, val second: B) If it were mutable, it could not be covariant in out A and out B , nor would it be safe to

Kotlin: How to modify a value in a pair? [duplicate]

无人久伴 提交于 2019-12-23 14:40:30
问题 This question already has an answer here : Why aren't the entries in a Kotlin Pair mutable? (1 answer) Closed 2 years ago . Why can't I change the values in the pair: var p: Pair<Int, String> = Pair(5, "Test") p.first = 3 Error under p.first : Val cannot be reassigned 回答1: Pair, like most data classes, is immutable. Its definition is effectively data class Pair<out A, out B>(val first: A, val second: B) If it were mutable, it could not be covariant in out A and out B , nor would it be safe to

Multi-Assignment based on Collection

假如想象 提交于 2019-12-23 12:52:08
问题 Edit originally the question was "Collection to Tuple" as I assumed I needed a tuple in order to do variable multi-assignment. It turns out that one can do variable multi-assignment directly on collections. Retitled the question accordingly. Original Have a simple Seq[String] derived from a regex that I would like to convert to a Tuple. What's the most direct way to do so? I currently have: val(clazz, date) = captures match { case x: Seq[String] => (x(0), x(1)) } Which is ok, but my routing

About an error accessing a field inside Tuple2

寵の児 提交于 2019-12-23 12:37:10
问题 i am trying to access to a field within a Tuple2 and compiler is returning me an error. The software tries to push a case class within a kafka topic, then i want to recover it using spark streaming so i can feed a machine learning algorithm and save results within a mongo instance. Solved! I finally solved my problem, i am going to post the final solution: This is the github project: https://github.com/alonsoir/awesome-recommendation-engine/tree/develop build.sbt name := "my-recommendation

How to perfectly convert one-element list to tuple in Python?

本小妞迷上赌 提交于 2019-12-23 12:16:54
问题 So I am trying to do this: tuple([1]) The output I expect is : (1) However, I got this: (1,) But if I do this: tuple([1,2]) It works perfectly! like this: (1,2) This is so weird that I don't know why the tuple function cause this result. Please help me to fix it. 回答1: This is such a common question that the Python Wiki has a page dedicated to it: One Element Tuples One-element tuples look like: 1, The essential element here is the trailing comma. As for any expression, parentheses are

How to “explicitly specify the categories order by passing in a categories argument” when using tuples as index keys in pandas?

那年仲夏 提交于 2019-12-23 12:16:40
问题 I've been trying to figure out how to make these tuples index keys in pandas but I'm getting an error. How can I use the suggestion from the error with pd.Categorical below to fix this error? I am aware that I can convert to a string but I am curious to see what is meant by the suggestion in the error message? This works perfectly fine when I run it with 0.22.0 . I've opened a GitHub issue for this if anyone wants to see the proper output from 0.22.0 . I want to update my pandas and handle