tuples

Tuple Comparison

本秂侑毒 提交于 2019-12-02 06:42:38
问题 I`ve a dictionary defined like this : d = {"date": tuple(date),"open":tuple(open),"close":tuple(close),"min":tuple(min),"max":tuple(max),"MA":tuple(ma)} Each one of those tuples contains a list of values ( same number of values for each tuple ), how can I iterate trough each value of paticular keys to compare if "close" is superior to "MA" ? 回答1: what am I missing? d['close'] > d['MA'] ? Edit: Re, your comments [...] what I want to return is how many times one element of "close" is > to the

Modifying all the tuples in a Python list

寵の児 提交于 2019-12-02 06:12:52
I have a list containing tuples with a standard format: bar_list = [(bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4), (bar1, bar2, bar3, bar4)...] Though I want to iterate through each tuple in the list and for each make specific modifications such as: foo0 = bar1 foo1 = get_foo(foo0) #get_foo(var) being a function foo2 = bar2 foo3 = bar3/2 And then repackage the revalued tuples in another list: foo_list = [(foo1, foo2, foo3), (foo1, foo2, foo3), (foo1, foo2, foo3)...] How could I accomplish this? You could use a list comprehension : foo_list = [(get_foo(bar1), bar2, bar3/2) for bar1, bar2,

Tuple to List - Python / PostgreSQL return type of SETOF Record

*爱你&永不变心* 提交于 2019-12-02 05:40:56
so from this code: from dosql import * import cgi import simplejson as json def index(req, userID): userID = cgi.escape(userID) get = doSql() rec = get.execqry("select get_progressrecord('" + userID + "');", False) return json.dumps(rec) Notice that the variable rec, receives a query from the database, from this defined function I created in PostgreSQL: create or replace function get_progressrecord(in int, out decimal(5,2), out decimal(5,2), out decimal(4,2), out text, out int, out decimal(4,2)) returns setof record as $$ select height, weight, bmi, healthStatus, age, changePercentage from

To sum up values of same items in a list of tuples while they are string

隐身守侯 提交于 2019-12-02 05:26:18
问题 If I have list of tuples like this: my_list = [('books', '$5'), ('books', '$10'), ('ink', '$20'), ('paper', '$15'), ('paper', '$20'), ('paper', '$15')] how can I turn the list to this: [('books', '$15'), ('ink', '$20'), ('paper', '$50')] i.e. to add the expense of same item while both the items are string in the tuples. I have problem with the price items being string. Any hint would be greatly appreciated. Thanks a lot! I am getting the first list in this way: my_list=[] for line in data:

Find index of nested item in python

为君一笑 提交于 2019-12-02 05:04:46
问题 I've been working with some relatively complex arrays such as: array = [ "1", 2, ["4", "5", ("a", "b")], ("c", "d")] and I was looking for a way to find an item and retrieve is "index" (Is it OK to refer to the location of item such as "a" - that is inside a Tuple as index on the same level as array?) Now my first thought was to use something like a simple helper function such as: def myindex(nestedlist, item): for i in nestedlist: if item in i: index = [] index.append(i) index.append(i.index

python sort list of tuple

血红的双手。 提交于 2019-12-02 05:04:21
问题 I am trying to sorting a list of tuple. for example, If >>>recommendations = [('Gloria Pritchett', 2), ('Manny Delgado', 1), ('Cameron Tucker', 1), ('Luke Dunphy', 3)] I want to get Luke Dunphy Gloria Pritchett Cameron Tucker Manny Delgado This is what I did: This code only gives me >>> [('Luke Dunphy', 3), ('Gloria Pritchett', 2), ('Cameron Tucker', 1), ('Manny Delgado', 1)] I have no idea how to append only names(strings) in sorted_list. Please help! 回答1: First of all, it sounds like you

Sorting dict items by key, beyond alphanumeric sorting

寵の児 提交于 2019-12-02 04:48:39
I have written this code: n=5 dizN={} for q in range(0,n+1): h=n-q dizN['a'+str(q)+'p'+str(h)]=0 that creates such a dictionary: dizN Out[120]: {'a0p5': 0, 'a1p4': 0, 'a2p3': 0, 'a3p2': 0, 'a4p1': 0, 'a5p0': 0} Note that "n" is the basic parameter for my code. As you can see, the sum of integers present in dict keys string is always =n (=5 in this case, where n=5). It is important for me (for more difficult purposes in my program) that, for every n anyone can choose, the dict is ordered in this way: {'a0p(n)': 0, 'a1p(n-1)': 0, ....., 'a(n-1)p1': 0, 'a(n)p0': 0} My code is ok, but only for n

Remove a tuple containing nan in list of tuples — Python

∥☆過路亽.° 提交于 2019-12-02 04:29:35
I have a long list of tuples and want to remove any tuple that has a nan in it using Python. What I currently have: x = [('Recording start', 0), (nan, 4), (nan, 7), ..., ('Event marker 1', 150)] Result I'm looking for: x = [('Recording start', 0), ('Event marker 1', 150)] I've tried use np.isnan and variants of that, but have had no success and keep getting an error: ufunc 'isnan' is not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule "safe" Any suggestions would be appreciated!! You could use list comprehension

Getting unique tuples out of a python set

ぐ巨炮叔叔 提交于 2019-12-02 04:22:14
问题 I currently have a set like the following: {(a,b), (b,a), (c,b), (b,c)} What I Would like to have is: {(a,b), (c,b)} As you may notice the duplicate values have been removed completely so that two tuples never have the same elements inside regardless of order. How can I tell the set to disregard the order of the elements in the tuple and just check the values between the tuples? 回答1: Okay, so you've got a set {c1, c2, c3, ...} , where each cN is itself a collection of some sort. If you don't

Are tuples really immutable in Python? [duplicate]

荒凉一梦 提交于 2019-12-02 04:20:29
问题 This question already has answers here : Append to a list defined in a tuple - is it a bug? [duplicate] (4 answers) Closed 4 years ago . One question that I faced today, which actually tested the immutability of the tuples in Python: Interviewer : Are tuples immutable in Python? Me : Yes Interviewer : So what does print(t1) here print? t1 = (4, 5) t1 = t1 + (91, 10) print(t1) Me : (4, 5, 91, 10) Interviewer : How does immutability of tuple then define this behavior? Me : It's got nothing to