tuples

Using NumPy to Find Median of Second Element of List of Tuples

让人想犯罪 __ 提交于 2019-12-10 14:57:37
问题 Let's say I have a list of tuples, as follows: list = [(a,1), (b,3), (c,5)] My goal is to obtain the first element of the median of the list of tuples, using the tuples' second element. In the above case, I would want an output of b, as the median is 3. I tried using NumPy with the following code, to no avail: import numpy as np list = [('a',1), ('b',3), ('c',5)] np.median(list, key=lambda x:x[1]) 回答1: You could calculate the median like this: np.median(dict(list).values()) # in Python 2.7;

elegant unpacking variable-length tuples

半世苍凉 提交于 2019-12-10 14:34:18
问题 A real, if silly problem: https://github.com/joshmarshall/tornadorpc/blob/master/tornadorpc/base.py def start_server(handlers, ...): ... for (route, handler) in handlers: ... Normally "handlers" is a list of 2-element tuples. But with this particular solution (Tornado) you can pass a third argument to a particular handler (kw args). So a tuple in "handlers" may have 2 elems sometimes or 3 elems other times. I need to unpack this in a loop. Sure, I can do smth like length checking or try.

Is it possible to index nested lists using tuples in python?

六眼飞鱼酱① 提交于 2019-12-10 14:24:16
问题 I just started with python and very soon wondered if indexing a nested list with a tuple was possible. Something like: elements[(1,1)] One example where I wanted to do that was something similar to the code below in which I save some positions of the matrix that I will later need to access in a tuple called index. index = ( (0,0), (0,2), (2,0), (2,2) ) elements = [ [ 'a', 'b', 'c'], [ 'c', 'd', 'e'], [ 'f', 'g', 'h'] ] for i in index: print (elements [ i[0] ] [ i[1] ]) # I would like to do

Count the duplicates in a list of tuples

∥☆過路亽.° 提交于 2019-12-10 14:23:10
问题 I have a list of tuples: a = [(1,2),(1,4),(1,2),(6,7),(2,9)] I want to check if one of the individual elements of each tuple matches the same position/element in another tuple, and how many times this occurs. For example: If only the 1st element in some tuples has a duplicate, return the tuple and how many times it's duplicated. I can do that with the following code: a = [(1,2), (1,4), (1,2), (6,7), (2,9)] coll_list = [] for t in a: coll_cnt = 0 for b in a: if b[0] == t[0]: coll_cnt = coll

Why use a 1-tuple, Tuple<T1> in C#? [duplicate]

烈酒焚心 提交于 2019-12-10 14:07:33
问题 This question already has answers here : What's the purpose of the Tuple(T1)/Singleton in .net? (4 answers) Closed 4 years ago . I don't see the use of 1-tuples in C#. Why do programmers use them? Tuple Class I have seen a declaration like the following: Tuple<string> state; But I wonder if there are further uses. 回答1: The Tuple classes only go up to 7. When you need an 8-tuple, you have to use Tuple<T1, T2, T3, T4, T5, T6, T7, Tuple<T8>> But if you need 8 items, you probably want to redesign

Is there a difference between a list and a tuple?

风流意气都作罢 提交于 2019-12-10 13:49:17
问题 I see existing questions that relate to specific programming languages. There are implementation differences in specific languages, but is there a theoretical conceptual difference? Mutable vs immutable : In Python, lists are fully mutable while tuples are immutable or persistently immutable so that modifications create new tuples and do not do in place modifications. But this is purely an implementation detail. In other languages tuples are mutable and lists are immutable. Heterogeneous vs

Destructuring tuple of tuple in closure

柔情痞子 提交于 2019-12-10 13:28:00
问题 I can destructure a tuple of tuple easily: let tt = (2, (3, 4)) let (a, (b, c)) = tt b // => 3 I'd like to do the same when declaring a closure, for example I thought I could write: [tt].map { (a, (b, c)) in // Use b } Xcode complains with "Unnamed parameters must be written with the empty name". Only way I got it to "work" was: [tt].map { (a, tuple: (b: Int, c: Int)) in // Use tuple.b } This has two drawbacks I'd like to avoid: I need to use tuple.b instead of b I need to specify the types

Is this not a tuple?

馋奶兔 提交于 2019-12-10 13:16:03
问题 I can't figure out what I'm doing wrong here. My error is: ImproperlyConfigured at /admin/ ' CategoryAdmin.fields ' must be a list or tuple. Isn't the CategoryAdmin.fields a tuple? Am I reading this wrong? admin.py .. class CategoryAdmin(admin.ModelAdmin): fields = ('title') list_display = ('id', 'title', 'creation_date') class PostAdmin(admin.ModelAdmin): fields = ('author', 'title', 'content') list_display = ('id', 'title', 'creation_date') admin.site.register( models.Category,

Python function parameter: tuple/list

只愿长相守 提交于 2019-12-10 13:08:00
问题 My function expects a list or a tuple as a parameter. It doesn't really care which it is, all it does is pass it to another function that accepts either a list or tuple: def func(arg): # arg is tuple or list another_func(x) # do other stuff here Now I need to modify the function slightly, to process an additional element: def func(arg): #arg is tuple or list another_func(x + ['a']) # etc Unfortunately this is not going to work: if arg is tuple, I must say x + ('a',) . Obviously, I can make it

Manipulating tuples

血红的双手。 提交于 2019-12-10 13:02:59
问题 Is there a way to manipulate multiple values of a tuple without using a temporary variable and starting a new statement? Say I have a method that returns a tuple and I want to do something with those values inline. e.g. if I want to split a string at a certain point and reverse the pieces def backToFront(s: String, n:Int) = s.splitAt(n)... I can do val (a, b) = s.splitAt(n) b + a (requires temporary variables and new statement) or List(s.splitAt(n)).map(i => i._2 + i._1).head (works, but