tuples

How can I pass a tuple to str.format()?

為{幸葍}努か 提交于 2019-12-12 02:25:08
问题 I'm trying to use the str.format() function to print a matrix in columns. This is the line that goes wrong: >>>> "{!s:4}{!s:5}".format('j',4,3) 'j 4 ' >>>> "{!s:4}{!s:5}".format(b) Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: tuple index out of range >>> b ('dat', 'is') What am I doing wrong? Edit: I think I know what the problem is: I'm passing a tuple with two elements, which is than passed on to the function as a tuple with one element, my original

List.remove skipping an item in a list [duplicate]

情到浓时终转凉″ 提交于 2019-12-12 02:24:19
问题 This question already has answers here : problem Deleting list items in a for loop (python) [duplicate] (4 answers) Python: Removing list element while iterating over list [duplicate] (9 answers) How to remove items from a list while iterating? (30 answers) How do I pass a variable by reference? (26 answers) Closed 2 years ago . I've been scratching my head for an hour on this issue. I'm trying to create a function that removes an item from a list of tuples is the tuple contains a 0 in

How to join two arrays in a tuple into one array in numpy

夙愿已清 提交于 2019-12-12 02:17:14
问题 I have a tuple with two arrays and I want to make it one array: The tuple: (array([['No', 'Yes', 'No', 'No'], ['No', 'Yes', 'No', 'Yes'], ['No', 'No', 'No', 'Yes']], dtype='<U7'), array([['Yes', 'No', 'No', 'Yes']], dtype='<U7')) I need to make it one array, so that it looks like : (array([['No', 'Yes', 'No', 'No'], ['No', 'Yes', 'No', 'Yes'], ['No', 'No', 'No', 'Yes'], ['Yes', 'No', 'No', 'Yes']], dtype='<U7')) How can I do this? 回答1: Just np.vstack them np.vstack(tuple_of_array) example

Unpacking iterables in Python3?

有些话、适合烂在心里 提交于 2019-12-12 02:09:38
问题 Why is this returning in sort_tup_from_list for key, val in tup: ValueError: not enough values to unpack (expected 2, got 1) # list with tuples lst = [("1", "2"), ("3", "4")] # sorting list by tuple val key def sort_tup_from_list(input_list): tmp = [] print(tup) for tup in input_list: for key, val in tup: tmp.append((val, key)) tmp.sort(reverse=True) return tmp print(sort_tup_from_list(lst)) When I comment out second for loop, it prints tuple: lst = [("1", "2"), ("3", "4")] def sort_tup_from

Why not have Jave methods return a tuple instead of an object reference (or null)?

孤人 提交于 2019-12-12 02:04:21
问题 Typically Java methods look like: public <U,V> U doSomething(V aReference) { // Do something } This typically means that the method doSomething() returns a null if it fails (for whatever reason) or a valid object reference. In some cases the "valid object reference" may itself be null . For example, the method aMap.get(k) may return null if there is no key k or if there is a key k but its corresponding value is null . Confusion! Not to mention NullPointerException s if 50% of your LOC isn't

Python: How to remove the last comma from tuples

感情迁移 提交于 2019-12-12 01:58:08
问题 How can I remove the comma from each tuple in the list. I want to make a list of tuples from 1 list, like this: l = [1,2,3,5,4] l1 = [ ] l2 = [ ] for i in l: l1.append (i) t = tuple(l1) l2.append(t) l1 = [] print l2 Expected result: [(1), (2), (3), (5), (4)] Real result: [(1,), (2,), (3,), (5,), (4,)] 回答1: If you only want the first element of each tuple in the list displayed (without a comma), you can always manually format the output by using something like this: l = [1, 2, 3, 5, 4] l1 = []

Tuple structure equivalent in Matlab

雨燕双飞 提交于 2019-12-12 01:46:58
问题 In Haskell, there is a structure called 'tuples' which allows two elements to be paired together (Ie: (1,2), ('A', 'B') etc) I was wondering if there was something similar in Matlab so that I could match elements and then query matlab in a way similar to "If element X is matched to Y then.. else.." Thanks! 回答1: The closet thing I know of in MATLAB is to use a map object. They are pretty easy to use. You can create one as follows someMap = containers.Map(); Adding a new key is pretty easy as

Formatted string literals in Python 3.6 with tuples

旧城冷巷雨未停 提交于 2019-12-12 01:46:47
问题 With str.format() I can use tuples for accesing arguments: >>> '{0}, {1}, {2}'.format('a', 'b', 'c') 'a, b, c' or >>> t = ('a', 'b', 'c') >>> '{0}, {1}, {2}'.format(*t) 'a, b, c' But with the new formatted string literals prefixed with 'f' how can use tuples? 回答1: Your first str.format() call is a regular method call with 3 arguments, there is no tuple involved there . Your second call uses the * splat call syntax; the str.format() call receives 3 separate individual arguments, it doesn't

Java Tuple Without Creating Multiple Type Parameters

陌路散爱 提交于 2019-12-12 01:46:39
问题 Is there any way to create a Tuple in Java, without having to create multiple classes? For example, it is possible to make a different class for every different type of Tuple, each with a different number of Type Parameters: public class SingleTuple<T>{} public class DoubleTuple<T1, T2>{} public class TripleTuple<T1, T2, T3>{} public class QuadraTuple<T1, T2, T3, T4>{} public class PentaTuple<T1, T2, T3, T4, T5>{} And it's also possible to create a Tuple object without any Type Parameters by

error: cannot pass objects of non-trivially-copyable type through `…`

陌路散爱 提交于 2019-12-12 01:36:37
问题 I have a class unit , which has the properties std::is_trivial<unit>::value; // true std::is_trivially_copyable<unit>::value; // true (on compilers which have this trait) I'd like to pass are vectors of unit as a tuple, e.g. using geodeticTuple = std::tuple<unit, unit, unit>; I need these vectors to be passable into conversion functions who use different types of arguments, e.g. someOtherType convert(const geodeticTuple& point, const geodeticTuple& origin) or someOtherType convert(const