tuples

How do I pass tuples elements to a function as arguments in python?

ε祈祈猫儿з 提交于 2019-12-01 14:15:15
问题 I have a list consisting of tuples, I want to pass each tuple's elements to a function as arguments: mylist = [(a, b), (c, d), (e, f)] myfunc(a, b) myfunc(c, d) myfunc(e, f) How do I do it? Best Regards 回答1: This is actually very simple to do in Python, simply loop over the list and use the splat operator ( * ) to unpack the tuple as arguments for the function: mylist = [(a, b), (c, d), (e, f)] for args in mylist: myfunc(*args) E.g: >>> numbers = [(1, 2), (3, 4), (5, 6)] >>> for args in

Creating a Python list from a list of tuples

荒凉一梦 提交于 2019-12-01 14:11:13
If I have, for example, a list of tuples such as a = [(1,2)] * 4 how would I create a list of the first element of each tuple? That is, [1, 1, 1, 1] . Use a list comprehension : >>> a = [(1,2)] * 4 >>> [t[0] for t in a] [1, 1, 1, 1] You can also unpack the tuple: >>> [first for first,second in a] [1, 1, 1, 1] If you want to get fancy, combine map and operator.itemgetter . In python 3, you'll have to wrap the construct in list to get a list instead of an iterable: >>> import operator >>> map(operator.itemgetter(0), a) <map object at 0x7f3971029290> >>> list(map(operator.itemgetter(0), a)) [1, 1

from list of tuples, get tuple closest to a given value

落花浮王杯 提交于 2019-12-01 13:41:41
Given a list of tuples containing coordinates, I want to find which coordinate is the closest to a coordinate I give in input: cooList = [(11.6702634, 72.313323), (31.67342698, 78.465323)] coordinate = (11.6702698, 78.113323) takenearest(myList, myNumber) ... (11.6702634, 72.313323) Please let me know... For your data cooList = [(11.6702634, 72.313323), (31.67342698, 78.465323)] coordinate = (11.6702698, 78.113323) the shortest Pythonic answer is: nearest = min(cooList, key=lambda x: distance(x, coordinate)) with a function distance(a, b) returning the distance between the points a and b as a

How to Compare Multiple lists of tuples in python?

℡╲_俬逩灬. 提交于 2019-12-01 13:39:28
问题 How can I compare Multiple lists of tuples like this: [[(1,2), (3,6), (5,3)], [(1,5), (3,5)], [(2,1), (1,8), (3,9)]] The output should be: [(1,2), (1,5), (1,8)],[(3,6), (3,5), (3,9)] It means that i want just those values whose x-axis value matches others. (5,3) and (2,1) should be discarded! 回答1: One possible Option >>> def group(seq): for k, v in groupby(sorted(chain(*seq), key = itemgetter(0)), itemgetter(0)): v = list(v) if len(v) > 1: yield v >>> list(group(some_list)) [[(1, 2), (1, 5),

Creating a Python list from a list of tuples

核能气质少年 提交于 2019-12-01 12:53:26
问题 If I have, for example, a list of tuples such as a = [(1,2)] * 4 how would I create a list of the first element of each tuple? That is, [1, 1, 1, 1] . 回答1: Use a list comprehension: >>> a = [(1,2)] * 4 >>> [t[0] for t in a] [1, 1, 1, 1] You can also unpack the tuple: >>> [first for first,second in a] [1, 1, 1, 1] If you want to get fancy, combine map and operator.itemgetter. In python 3, you'll have to wrap the construct in list to get a list instead of an iterable: >>> import operator >>>

python sqlalchemy insert multiple lines in a tuple data structure

拥有回忆 提交于 2019-12-01 11:50:23
I have been researching how to insert a list of ~500 tuples (rows) that has 7 elements (columns) into a database. I have read through various posts on stackoverflow as well as other forums. I found the following and it suggests to use the 'executemany()' method but its not so clear to me how. Do I need to covert my object from tuple to a dictionary? The problem is I don't have a name:value type of data structure. How to use SQLAlchemy to dump an SQL file from query expressions to bulk-insert into a DBMS? Here is an example: engine = create_engine('sqlite:///:memory:', echo=True) metadata =

python sqlalchemy insert multiple lines in a tuple data structure

时光总嘲笑我的痴心妄想 提交于 2019-12-01 11:40:27
问题 I have been researching how to insert a list of ~500 tuples (rows) that has 7 elements (columns) into a database. I have read through various posts on stackoverflow as well as other forums. I found the following and it suggests to use the 'executemany()' method but its not so clear to me how. Do I need to covert my object from tuple to a dictionary? The problem is I don't have a name:value type of data structure. How to use SQLAlchemy to dump an SQL file from query expressions to bulk-insert

extra empty element when removing an element from a tuple

萝らか妹 提交于 2019-12-01 11:26:31
I have a question about the following python outcome. Suppose I have a tuple : a = ( (1,1), (2,2), (3,3) ) I want to remove (2,2) , and I'm doing this with the following code: tuple([x for x in a if x != (2,2)]) This works fine, the result is: ( (1,1), (3,3) ) , just as I expect. But suppose I start from a = ( (1,1), (2,2) ) and use the same tuple() command, the result is ( (1,1), ) while I would expect it to be ((1,1)) In short >>> a = ( (1,1), (2,2), (3,3) ) >>> tuple([x for x in a if x != (2,2)]) ((1, 1), (3, 3)) >>> a = ( (1,1), (2,2) ) >>> tuple([x for x in a if x != (2,2)]) ((1, 1),) Why

Converting a string to a tuple in python

ε祈祈猫儿з 提交于 2019-12-01 11:19:51
Okay, I have this string tc='(107, 189)' and I need it to be a tuple, so I can call each number one at a time. print(tc[0]) #needs to output 107 Thank you in advance! All you need is ast.literal_eval : >>> from ast import literal_eval >>> tc = '(107, 189)' >>> tc = literal_eval(tc) >>> tc (107, 189) >>> type(tc) <class 'tuple'> >>> tc[0] 107 >>> type(tc[0]) <class 'int'> >>> From the docs : ast.literal_eval(node_or_string) Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python expression. The string or node provided may only consist of the following

Convert list items to tuple

瘦欲@ 提交于 2019-12-01 10:58:13
I have a list like this. ['February 01,2011 - February 28, 2011', 'March 01,2011 - March 31, 2011'] I want to convert it to [('February 01,2011 - February 28, 2011'), ('March 01,2011 - March 31, 2011')] Any Ideas?? Please help!!! Thanks in advance!!! To make a tuple with 1 element, append a comma in the parentheses: >>> my_list = ['February 01,2011 - February 28, 2011', 'March 01,2011 - March 31, 2011'] >>> [(x,) for x in my_list] [('February 01,2011 - February 28, 2011',), ('March 01,2011 - March 31, 2011',)] 来源: https://stackoverflow.com/questions/8876125/convert-list-items-to-tuple