tuples

Is it possible to unpack a tuple into function arguments?

无人久伴 提交于 2020-02-29 10:03:59
问题 If I want to unpack a tuple and pass it as arguments is there a way to do this: //Does not compile fn main() { let tuple = (10, Vec::new()); foo(tuple); } fn foo(a: i32, b: Vec<i32>) { //Does stuff. } Instead of having to do this: fn main() { let tuple = (10, Vec::new()); foo(tuple.0, tuple.1); } fn foo(a: i32, b: Vec<i32>) { //Does stuff. } 回答1: On a nightly compiler: #![feature(fn_traits)] fn main() { let tuple = (10, Vec::new()); std::ops::Fn::call(&foo, tuple); } fn foo(a: i32, b: Vec<i32

Import csv file as a list of tuples

牧云@^-^@ 提交于 2020-02-26 04:14:32
问题 I have a csv file containing tuples Test.csv "(1,2,3)","(1,2,4)","(1,2,5)" I want to import Test.csv as a list of tuples . Expected Result new_list = [(1,2,3),(1,2,4),(1,2,5)] Code Attempt import csv with open ('Test.csv', newline='') as file: reader = csv.reader(file) list_a = list(reader) new_list = [tuple(map(str,i)) for i in list_a] print(new_list) Current Output comes as list of strings ['(1,2,3)','(1,2,4)','(1,2,5)'] What is wrong with my approach? How can I solve this by not using ast

Import csv file as a list of tuples

北城以北 提交于 2020-02-26 04:13:05
问题 I have a csv file containing tuples Test.csv "(1,2,3)","(1,2,4)","(1,2,5)" I want to import Test.csv as a list of tuples . Expected Result new_list = [(1,2,3),(1,2,4),(1,2,5)] Code Attempt import csv with open ('Test.csv', newline='') as file: reader = csv.reader(file) list_a = list(reader) new_list = [tuple(map(str,i)) for i in list_a] print(new_list) Current Output comes as list of strings ['(1,2,3)','(1,2,4)','(1,2,5)'] What is wrong with my approach? How can I solve this by not using ast

How to consume BlockingCollection<'a>.TryTake in F#

Deadly 提交于 2020-02-24 04:22:35
问题 How do I go about using the TryTake method on a BlockingCollection<'a> passing in a timeout period in milliseconds? Heres the signature: BlockingCollection.TryTake(item: byref, millisecondsTimeout: int) : bool is it possible to use the Tuple method of avoiding passing a ref type like on the Dictionary.TryGet methods? i.e. let success, item = myDictionary.TryGetValue(client) Im struggling with this particular signature, any suggestions would be great. Cheers! 回答1: I believe that you can only

Concatenation of tuples

蓝咒 提交于 2020-02-23 10:15:08
问题 Normal text: I'm having some problems with coding on python 3.2.1. Actually I'm taking online lectures that are on python 2.5. Here is the code: x = 100 divisors = () for i in range(1,x): if x%i == 0: divisors = divisors + (i) on running the program, following error appears: divisors = divisors + (i) TypeError: can only concatenate tuple (not "int") to tuple 回答1: (1) is not a tuple, its just a parenthesized expression. To make it a tuple, add a trailing comma, (1,) 回答2: Try to use this

Concatenation of tuples

淺唱寂寞╮ 提交于 2020-02-23 10:14:32
问题 Normal text: I'm having some problems with coding on python 3.2.1. Actually I'm taking online lectures that are on python 2.5. Here is the code: x = 100 divisors = () for i in range(1,x): if x%i == 0: divisors = divisors + (i) on running the program, following error appears: divisors = divisors + (i) TypeError: can only concatenate tuple (not "int") to tuple 回答1: (1) is not a tuple, its just a parenthesized expression. To make it a tuple, add a trailing comma, (1,) 回答2: Try to use this

Pandas: create a dictionary with a tuple as key

前提是你 提交于 2020-02-23 03:52:39
问题 Given this DataFrame : import pandas as pd first=[0,1,2,3,4] second=[10.2,5.7,7.4,17.1,86.11] third=['a','b','c','d','e'] fourth=['z','zz','zzz','zzzz','zzzzz'] df=pd.DataFrame({'first':first,'second':second,'third':third,'fourth':fourth}) df=df[['first','second','third','fourth']] first second third fourth 0 0 10.20 a z 1 1 5.70 b zz 2 2 7.40 c zzz 3 3 17.10 d zzzz 4 4 86.11 e zzzzz I can create a dictionary with a list of columns as values, like this: d = {df.loc[idx, 'first']: [df.loc[idx,

Given a list and a bitmask, how do I return the values at the indices that are True?

最后都变了- 提交于 2020-02-21 10:57:21
问题 I start with the following list s and bitmask b : s = ['baa', 'baa', 'black', 'sheep', 'have', 'you', 'any', 'wool'] b = [1, 0, 0, 0, 1, 1, 1, 0] # or any iterable with boolean values How do I write some function apply_bitmask(s, b) so that it returns ['baa', 'have', 'you', 'any'] 回答1: Python 3.1 itertools.compress (or Python 2.7's if you haven't upgraded yet) does exactly that (the list comprehension is a real close second): import itertools filtered = itertools.compress(s, b) Note that this

How to use ast.literal_eval in a pandas dataframe and handle exceptions

放肆的年华 提交于 2020-02-15 09:35:30
问题 I have a dataframe with a column containing a tuple data as a string. Eg. '(5,6)' . I need to convert this to a tuple structure. One way of doing it is using the ast.literal_eval(). I am using it in this way. df['Column'] = df['Column'].apply(ast.literal_eval) Unfortunately, my data in this column contains empty strings also. The ast.literal_eval() is not able to handle this. I get this error. SyntaxError: unexpected EOF while parsing I am unsure if this is because it is unable to handle such

Python Trueskill - Using Dictionaries

房东的猫 提交于 2020-02-07 06:58:09
问题 I'm trying out the Trueskill package from Python and have the basics worked out, For a two player match up I can do alice = Rating() bob = Rating() alice, bob = rate_1vs1(alice, bob) And for multiple player matchups I can do alice = Rating() bob = Rating() eve = Rating() alice, bob, eve = rate([(alice,),(bob,),(eve,)], ranks=[0, 1, 2]) I currently have a database with is structured as follows which I'd like to perform ratings on... game participant rank ....(various game stats) 1 alice 2 1