tuples

Convert a string to a tuple [duplicate]

被刻印的时光 ゝ 提交于 2019-12-17 21:22:06
问题 This question already has answers here : Parse a tuple from a string? (4 answers) Closed 2 years ago . I read some tuple data from a file. The tuples are in string form, for example Color["RED"] = '(255,0,0)' . How can I convert these strings into actual tuples? I want to use this data in PyGame like this: gameDisplay.fill(Color["RED"]) # but it doesn't have the right data right now: gameDisplay.fill('(255,0,0)') 回答1: You could use the literal_eval of the ast module: ast. literal_eval ( node

Convert a columns of string to list in pandas

笑着哭i 提交于 2019-12-17 20:53:21
问题 I have a problem with the type of one of my column in a pandas dataframe. Basically the column is saved in a csv file as a string, and I wanna use it as a tuple to be able to convert it in a list of numbers. Following there is a very simple csv: ID,LABELS 1,"(1.0,2.0,2.0,3.0,3.0,1.0,4.0)" 2,"(1.0,2.0,2.0,3.0,3.0,1.0,4.0)" If a load it with the function "read_csv" I get a list of strings. I have tried to convert to a list, but I get the list version of a string: df.LABELS.apply(lambda x: list

Python: Generating a “set of tuples” from a “list of tuples” that doesn't take order into consideration

冷暖自知 提交于 2019-12-17 20:48:10
问题 If I have the list of tuples as the following: [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')] I would like to remove duplicate tuples (duplicate in terms of both content and order of items inside) so that the output would be: [('a', 'b'), ('c', 'd')] Or [('b', 'a'), ('c', 'd')] I tried converting it to set then to list but the output would maintain both ('b', 'a') and ('a', 'b') in the resulting set! 回答1: Try this : a = [('a', 'b'), ('c', 'd'), ('a', 'b'), ('b', 'a')] b = list(set([ tuple

Deduction guide and variadic templates

主宰稳场 提交于 2019-12-17 20:45:55
问题 Consider the following code: #include <tuple> #include <iostream> template <class T> struct custom_wrapper { template <class Arg> custom_wrapper(Arg arg): data(arg) {} T data; }; template <class Arg> custom_wrapper(Arg arg) -> custom_wrapper<Arg>; template <class... T> struct custom_tuple { template <class... Args> custom_tuple(Args... args): data(args...) {} std::tuple<T...> data; }; template <class... Args> custom_tuple(Args... args) -> custom_tuple<Args...>; int main(int argc, char* argv[]

Sum numbers by letter in list of tuples

家住魔仙堡 提交于 2019-12-17 20:27:29
问题 I have a list of tuples: [ ('A',100), ('B',50), ('A',50), ('B',20), ('C',10) ] I am trying to sum up all numbers that have the same letter. I.e. I want to output [('A', 150), ('B', 70), ('C',10)] I have tried using set to get the unique values but then when I try and compare the first elements to the set I get TypeError: unsupported operand type(s) for +: 'int' and 'str' Any quick solutions to match the numbers by letter? 回答1: Here is a one(and a half?)-liner: group by letter (for which you

How can I sort tuples by reverse, yet breaking ties non-reverse? (Python)

爷,独闯天下 提交于 2019-12-17 18:55:24
问题 If I have a list of tuples: results = [('10', 'Mary'), ('9', 'John'), ('10', 'George'), ('9', 'Frank'), ('9', 'Adam')] How can I sort the list as you might see in a scoreboard - such that it will sort the score from biggest to smallest, but break ties alphabetically by name? So after the sort, the list should look like: results = [('10', 'George'), ('10', 'Mary'), ('9', 'Adam'), ('9', 'Frank'), ('9', 'John')] At the moment all I can do is results.sort(reverse=True) , but breaks ties reverse

“using” function

家住魔仙堡 提交于 2019-12-17 18:25:59
问题 I've defined 'using' function as following: def using[A, B <: {def close(): Unit}] (closeable: B) (f: B => A): A = try { f(closeable) } finally { closeable.close() } I can use it like that: using(new PrintWriter("sample.txt")){ out => out.println("hellow world!") } now I'm curious how to define 'using' function to take any number of parameters, and be able to access them separately: using(new BufferedReader(new FileReader("in.txt")), new PrintWriter("out.txt")){ (in, out) => out.println(in

Java N-Tuple implementation

五迷三道 提交于 2019-12-17 17:30:47
问题 I just made a Java n-tuple which is type-safe. I'm using some unconventional methods to achieve type-safety (I just made it for fun). Can someone can give some input on improving it or some possible flaws. public class Tuple { private Object[] arr; private int size; private static boolean TypeLock = false; private static Object[] lastTuple = {1,1,1}; //default tuple type private Tuple(Object ... c) { // TODO Auto-generated constructor stub size=c.length; arr=c; if(TypeLock) { if(c.length ==

Push type to the end of the tuple

血红的双手。 提交于 2019-12-17 16:49:11
问题 I can add element to the begining of the tuple, or remove it from there type ShiftTuple<T extends any[]> = ((...t: T) => void) extends ((x: infer X, ...r: infer R) => void) ? R : never; type UnshiftTuple<X, T extends any[]> = ((x: X, ...t: T) => void) extends ((...r: infer R) => void) ? R : never; But I have difficulties with doing the same things with the last element instead of the first. Even if I write the function function f<X, Z extends any[]>(x: X, ...args: Z) { return [...args, x] }

How do I sum the first value in each tuple in a list of tuples in Python?

风流意气都作罢 提交于 2019-12-17 15:54:20
问题 I have a list of tuples (always pairs) like this: [(0, 1), (2, 3), (5, 7), (2, 1)] I'd like to find the sum of the first items in each pair, i.e.: 0 + 2 + 5 + 2 How can I do this in Python? At the moment I'm iterating through the list: sum = 0 for pair in list_of_pairs: sum += pair[0] I have a feeling there must be a more Pythonic way. 回答1: A version compatible with Python 2.3 is sum([pair[0] for pair in list_of_pairs]) or in recent versions of Python, see this answer or this one. 回答2: sum(i