tuples

Why does the 2-tuple Functor instance only apply the function to the second element?

北战南征 提交于 2019-12-28 05:57:22
问题 import Control.Applicative main = print $ fmap (*2) (1,2) produces (1,4) . I would expect it it to produce (2,4) but instead the function is applied only to the second element of the tuple. Update I've basically figured this out almost straight away. I'll post my own answer in a minute.. 回答1: Let me answer this with a question: Which output do you expect for: main = print $ fmap (*2) ("funny",2) You can have something as you want (using data Pair a = Pair a a or so), but as (,) may have

Convert RGB color to English color name, like 'green' with Python

梦想与她 提交于 2019-12-28 02:26:09
问题 I want to convert a color tuple to a color name, like 'yellow' or 'blue' >>> im = Image.open("test.jpg") >>> n, color = max(im.getcolors(im.size[0]*im.size[1])) >>> print color (119, 172, 152) Is there a simple way in python to do this? 回答1: It looks like webcolors will allow you to do this: rgb_to_name(rgb_triplet, spec='css3') Convert a 3-tuple of integers, suitable for use in an rgb() color triplet, to its corresponding normalized color name, if any such name exists; valid values are html4

Using tuples in SQL “IN” clause

拟墨画扇 提交于 2019-12-28 01:47:30
问题 I have a table containing the fields group_id and group_type and I want to query the table for all the records having any tuple ( group id , group type ) from a list of tuples. For example, I want to be able to do something like: SELECT * FROM mytable WHERE (group_id, group_type) IN (("1234-567", 2), ("4321-765", 3), ("1111-222", 5)) A very similar question is already asked at: using tuples in sql in clause , but the solution proposed there presumes the tuple list is to be fetched from

What is the pythonic way to unpack tuples? [duplicate]

旧城冷巷雨未停 提交于 2019-12-27 11:39:30
问题 This question already has answers here : Unpack a list in Python? (3 answers) Closed last year . This is ugly. What's a more Pythonic way to do it? import datetime t= (2010, 10, 2, 11, 4, 0, 2, 41, 0) dt = datetime.datetime(t[0], t[1], t[2], t[3], t[4], t[5], t[6]) 回答1: Generally, you can use the func(*tuple) syntax. You can even pass a part of the tuple, which seems like what you're trying to do here: t = (2010, 10, 2, 11, 4, 0, 2, 41, 0) dt = datetime.datetime(*t[0:7]) This is called

How to convert comma-delimited string to list in Python?

陌路散爱 提交于 2019-12-27 11:01:43
问题 Given a string that is a sequence of several values separated by a commma: mStr = 'A,B,C,D,E' How do I convert the string to a list? mList = ['A', 'B', 'C', 'D', 'E'] 回答1: You can use the str.split method. >>> my_string = 'A,B,C,D,E' >>> my_list = my_string.split(",") >>> print my_list ['A', 'B', 'C', 'D', 'E'] If you want to convert it to a tuple, just >>> print tuple(my_list) ('A', 'B', 'C', 'D', 'E') If you are looking to append to a list, try this: >>> my_list.append('F') >>> print my

How to convert comma-delimited string to list in Python?

给你一囗甜甜゛ 提交于 2019-12-27 11:01:27
问题 Given a string that is a sequence of several values separated by a commma: mStr = 'A,B,C,D,E' How do I convert the string to a list? mList = ['A', 'B', 'C', 'D', 'E'] 回答1: You can use the str.split method. >>> my_string = 'A,B,C,D,E' >>> my_list = my_string.split(",") >>> print my_list ['A', 'B', 'C', 'D', 'E'] If you want to convert it to a tuple, just >>> print tuple(my_list) ('A', 'B', 'C', 'D', 'E') If you are looking to append to a list, try this: >>> my_list.append('F') >>> print my

List vs tuple, when to use each? [duplicate]

霸气de小男生 提交于 2019-12-27 10:19:27
问题 This question already has answers here : What's the difference between lists and tuples? (18 answers) Closed 5 years ago . In Python, when should you use lists and when tuples? Sometimes you don't have a choice, for example if you have "hello %s you are %s years old" % x then x must be a tuple. But if I am the one who designs the API and gets to choose the data types, then what are the guidelines? 回答1: There's a strong culture of tuples being for heterogeneous collections, similar to what you

List vs tuple, when to use each? [duplicate]

感情迁移 提交于 2019-12-27 10:19:10
问题 This question already has answers here : What's the difference between lists and tuples? (18 answers) Closed 5 years ago . In Python, when should you use lists and when tuples? Sometimes you don't have a choice, for example if you have "hello %s you are %s years old" % x then x must be a tuple. But if I am the one who designs the API and gets to choose the data types, then what are the guidelines? 回答1: There's a strong culture of tuples being for heterogeneous collections, similar to what you

C++ std::get equivalent with run-time arguments

给你一囗甜甜゛ 提交于 2019-12-25 16:38:41
问题 One cannot directly use std::get with run-time arguments: template<typename Tuple> void get(size_t i, Tuple const& t) { using std::get; std::cout<<get<i>(t)<<std::endl; //error: 'i' is not a constant expression } int main() { std::tuple<int,double> t; get(1,t); } However, one can manually map run-time to compile-time information: template<typename Tuple> void get_impl(size_t i, Tuple const& t, typename std::tuple_size<Tuple>::type) {} template<size_t N, typename Tuple, typename = std::enable

C++ std::get equivalent with run-time arguments

≯℡__Kan透↙ 提交于 2019-12-25 16:37:20
问题 One cannot directly use std::get with run-time arguments: template<typename Tuple> void get(size_t i, Tuple const& t) { using std::get; std::cout<<get<i>(t)<<std::endl; //error: 'i' is not a constant expression } int main() { std::tuple<int,double> t; get(1,t); } However, one can manually map run-time to compile-time information: template<typename Tuple> void get_impl(size_t i, Tuple const& t, typename std::tuple_size<Tuple>::type) {} template<size_t N, typename Tuple, typename = std::enable