tuples

pattern matching, tuples and multiplication in Python

限于喜欢 提交于 2019-12-06 06:06:50
What is be best way to reduce this series of tuples ('x', 0.29, 'a') ('x', 0.04, 'a') ('x', 0.03, 'b') ('x', 0.02, 'b') ('x', 0.01, 'b') ('x', 0.20, 'c') ('x', 0.20, 'c') ('x', 0.10, 'c') into: ('x', 0.29 * 0.04 , 'a') ('x', 0.03 * 0.02 * 0.01, 'b') ('x', 0.20 * 0.20 * 0.10, 'c') EDIT: X is a constant, it is known in advance and can be safely ignored And the data can be treated as pre-sorted on the third element as it appears above. I am trying to do it at the moment using operator.mul, and a lot of pattern matching, and the odd lambda function... but I'm sure there must be an easier way! Can

Initialize tuple of references with reference to tuple

梦想与她 提交于 2019-12-06 06:04:56
问题 If I have the code #include <tuple> using Vec3 = std::tuple<float, float, float>; using Vec3Ref = std::tuple<float&, float&, float&>; void stuff () { Vec3 foo (0,0,0); Vec3Ref bar (foo); } I get the error /usr/include/c++/4.6/tuple:100:4: error: binding of reference to type 'float' to a value of type 'const float' drops qualifiers : _M_head_impl(std::forward<_UHead>(__h)) { } ^ ~~~~~~~~~~~~~~~~~~~~~~~~~ //snip... /usr/include/c++/4.6/tuple:257:11: note: in instantiation of function template

Insert multiple rows into DB with Python list of Tuples

ⅰ亾dé卋堺 提交于 2019-12-06 05:54:41
问题 I have a list of tuples: list_ = [(1,7,3000),(1,8,3500), (1,9,3900)] I want to update a table with multiple rows/values for a given ID (in this case ID = 1) So: INSERT INTO table (ID, Speed, Power) VALUES (1,7,3000),(1,8,3500),(1,9,3900) I'm having trouble with the format - I've gotten the string down to something like this: INSERT INTO ... VALUES ((1,7,3000),(1,8,3500),(1,9,3900)) But of course this doesn't work due to the extra parenthesis wrapped around the tuples. Any ideas for

How to convert Tuple to AnyObject in Swift

折月煮酒 提交于 2019-12-06 04:54:28
问题 Following piece of code compiles with error: Error:(112, 20) type '(String, Int)' does not conform to protocol 'AnyObject' func myMethode() { aMethodeThatICanNotChange { let a = ("John",7) return a // Error:(112, 20) type '(String, Int)' does not conform to protocol 'AnyObject' } } func aMethodeThatICanNotChange(closure: () -> AnyObject) { // do something with closure } How can I cast/convert a Tuple to AnyObject? 回答1: How can I cast/convert a Tuple to AnyObject? Simply put, you can't. Only

Storing return values of functions in a tuple

微笑、不失礼 提交于 2019-12-06 04:11:59
问题 Consider #include <tuple> template <typename... F> auto execute (F... f) { return std::make_tuple(f(0)...); } int foo(int) { return 5; } int bar(int) { return 3; } int main() { auto tuple = execute(foo, bar); } What is a good workaround so that bar can return void? I tried this, but it won't compile: #include <tuple> struct Void { }; template <typename T> T check(T n) { return n; } Void check(void) { return Void{}; } template <typename... F> auto execute (F... f) { return std::make_tuple

How do I convert tuple of tuples to list in one line (pythonic)?

流过昼夜 提交于 2019-12-06 03:26:09
问题 query = 'select mydata from mytable' cursor.execute(query) myoutput = cursor.fetchall() print myoutput (('aa',), ('bb',), ('cc',)) Why is it (cursor.fetchall) returning a tuple of tuples instead of a tuple since my query is asking for only one column of data? What is the best way of converting it to ['aa', 'bb', 'cc'] ? I can do something like this : mylist = [] myoutput = list(myoutput) for each in myoutput: mylist.append(each[0]) I am sure this isn't the best way of doing it. Please

Python 3: Converting A Tuple To A String

佐手、 提交于 2019-12-06 02:39:38
I have the following code: var_one = var_two[var_three-1] var_one = "string_one" + var_1 And I need to do the following to it: var_four = 'string_two', var_one However, this returns the following error: TypeError: Can't convert 'tuple' object to str implicity I have tried things such as str(var_one) and using strip but these did not work. What can I do to achieve the result I require? EDIT - Here are what the variables contain: var_one: new variable var_two: tuple var_three: integer var_four: new EDIT2: The line in the program that makes the error is: os.system(var_four) Edwin Your code looks

sizeof variadic template (sum of sizeof of all elements)

时光毁灭记忆、已成空白 提交于 2019-12-06 02:09:41
问题 Considering the following function : template<typename... List> inline unsigned int myFunction(const List&... list) { return /* SOMETHING */; } What is the most simple thing to put instead of /* SOMETHING */ in order to return the sum of sizeof all arguments ? For example myFunction(int, char, double) = 4+1+8 = 13 回答1: unsigned myFunction() {return 0;} template <typename Head, typename... Tail> unsigned myFunction(const Head & head, const Tail &... tail) { return sizeof head + myFunction(tail

Why can't I subclass tuple in python3?

此生再无相见时 提交于 2019-12-06 02:09:41
问题 Let's preface this question by saying that you should use __new__ instead of __init__ for subclassing immutable objects. With that being said, let's see the following code: class MyTuple(tuple): def __init__(self, *args): super(MyTuple, self).__init__(*args) mytuple = MyTuple([1,2,3]) This works in python2, but in python3 I get: Traceback (most recent call last): File "tmp.py", line 5, in <module> mytuple = MyTuple([1,2,3]) File "tmp.py", line 3, in __init__ super(MyTuple, self).__init__(

Use statement For to write in a Tuple

↘锁芯ラ 提交于 2019-12-06 02:06:27
I use this code to send midi sysEx. It s perfect for sending "fix" data but now i need to send data with different size. var midiPacket:MIDIPacket = MIDIPacket() midiPacket.length = 6 midiPacket.data.0 = data[0] midiPacket.data.1 = data[1] midiPacket.data.2 = data[2] midiPacket.data.3 = data[3] midiPacket.data.4 = data[4] midiPacket.data.5 = data[5] //... //MIDISend... Now imagine i have a String name "TROLL" but the name can change. I need something like this : var name:String = "TOTO" var nameSplit = name.components(separatedBy: "") var size:Int = name.count midiPacket.length = UInt16(size)