tuples

Python add item to the tuple

℡╲_俬逩灬. 提交于 2019-11-29 18:50:52
I have some object.ID-s which I try to store in the user session as tuple. When I add first one it works but tuple looks like (u'2',) but when I try to add new one using mytuple = mytuple + new.id got error can only concatenate tuple (not "unicode") to tuple . You need to make the second element a 1-tuple, eg: a = ('2',) b = 'z' new = a + (b,) Since Python 3.5 ( PEP 448 ) you can do unpacking within a tuple, list set, and dict: a = ('2',) b = 'z' new = (*a, b) kiriloff From tuple to list to tuple : a = ('2',) b = 'b' l = list(a) l.append(b) tuple(l) Or with a longer list of items to append a =

get the count of elements of tuples of your own…not just the range or sequence

微笑、不失礼 提交于 2019-11-29 18:21:39
The below code is running for first three elements of the tuple of this list SS1=[(1, 2, 3, 4, 5), (1, 2, 3, 4, 6), (1, 2, 3, 5, 6), (1, 2, 4, 5, 6), (1, 3, 4, 5, 6), (2, 3, 4, 5, 6)] from collections import Counter c = Counter(elem[0:3] for elem in SS1) for k, v in c.items(): if (v > 0): print(k,v) and the output is: (1, 2, 3) 3 (1, 2, 4) 1 (1, 3, 4) 1 (2, 3, 4) 1 But my expectation is not just for first three tuple...i want the counter for tuple (0,2,3) or tuple (1,2,4) likewise i can pass any three position of the tuple and get the count of it... How can I do this? If what i understood from

Why do we use tuples, if we can use a two dimensional list?

ぃ、小莉子 提交于 2019-11-29 17:20:52
Is there a benefit to specifically reserving varied data-type pairings for tuples like such: [(23, "Jordan"), (8, "Bryant")] As opposed to just using a two-dimensional list: [[23, "Jordan"], [8, "Bryant"]] I know the second piece of code will not work in Haskell Why do we use tuples, if we can use a two dimensional list? Because lists and tuples are conceptually different things, and the type system gives us an useful way to state and recognise the difference in code. For one of many possible examples, one might define... type ListyPair a = [a] ... and then... listyFst :: ListyPair a -> a

crudrepository findBy method signature for list of tuples

戏子无情 提交于 2019-11-29 16:36:14
I have an Entity Class like this: @Entity @Table(name = "CUSTOMER") class Customer{ @Id @Column(name = "Id") Long id; @Column(name = "EMAIL_ID") String emailId; @Column(name = "MOBILE") String mobile; } How to write findBy method for the below query using crudrepository spring data jpa? select * from customer where (email, mobile) IN (("a@b.c","8971"), ("e@f.g", "8888")) I'm expecting something like List<Customer> findByEmailMobileIn(List<Tuple> tuples); I want to get the list of customers from given pairs dimirsen I think this can be done with org.springframework.data.jpa.domain.Specification

tuples vs records

痞子三分冷 提交于 2019-11-29 16:28:10
问题 What is difference between tuples and records? 回答1: Both are product types which let you build types from multiple simpler types. Some languages treat tuples as a kind of record. Definitions A tuple is an ordered group of elements, like (10, 25). A record is typically a group of named elements like { "x": 10, "y": 25 } where the value has two fields labelled x and y and the value of field x is 10 . Etymology The word "tuple" comes from the common "-tuple" suffix on "quintuple", "sextuple",

Python: Why is comparison between lists and tuples not supported?

拥有回忆 提交于 2019-11-29 16:23:31
问题 When comparing a tuple with a list like ... >>> [1,2,3] == (1,2,3) False >>> [1,2,3].__eq__((1,2,3)) NotImplemented >>> (1,2,3).__eq__([1,2,3]) NotImplemented ... Python does not deep-compare them as done with (1,2,3) == (1,2,3) . So what is the reason for this? Is it because the mutable list can be changed at any time (thread-safety issues) or what? (I know where this is implemented in CPython, so please don't answer where , but why it is implemented.) 回答1: You can always "cast" it >>> tuple

What does the comma in this assignment statement do?

泄露秘密 提交于 2019-11-29 16:13:15
I was looking through an interesting example script I found (at this site , last example line 124), and I'm struggling to understand what the comma after particles achieves in this line: particles, = ax.plot([], [], 'bo', ms=6) The script will hit an error if the comma is omitted, but the syntax (which seems to resemble an unpacking statement) does not make much sense to me, and a statement like a, = [2,3] fails, which seems like an argument against the unpacking theory. Any insight would be greatly appreciated. It is needed to unpack the 1-tuple (or any other length-1 sequence). Example: >>>

I want to group tuples based on similar attributes

て烟熏妆下的殇ゞ 提交于 2019-11-29 16:09:59
I have a list of tuples. [ (1, 2), (2, 3), (4, 3), (5, 6), (6, 7), (8, 2) ] I want to group them into lists based on which tuples are connected (have related values). So the end result is two lists of related tuple values = [ [1, 2, 3, 4, 8], [5, 6, 7] ] How can I write a function to do this? This was a job interview question. I was trying to do it in Python, but I'm frustrated and just want to see the logic behind the answer, so even psuedo code would help me, so I can see what I did wrong. I only had a few minutes to do this on the spot, but here is what I tried: def find_partitions

How to sort an Array of Tuples?

隐身守侯 提交于 2019-11-29 15:42:52
问题 How do you implement (or create) an array sort of a list of tuples? The following was gleaned from my code. Essentially I created an array of tuples and populated it via for loop; after which I tried to sort it. var myStringArray: (String,Int)[]? = nil ... myStringArray += (kind,number) ... myStringArray.sort{$0 > $1} This is what Xcode gave me before I could build: test.swift:57:9: '(String, Int)[]?' does not have a member named 'sort' 回答1: You have two problems. First, myStringArray is an

Using Tuples in map, flatmap,… partial functions

别等时光非礼了梦想. 提交于 2019-11-29 14:55:01
If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { t => t._1 + t._2 } It's ok. If I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { case (b, n) => b + n } It's ok too. But if I do: val l = Seq(("un", ""), ("deux", "hehe"), ("trois", "lol")) l map { (b, n) => b + n } It will not work. Why should I use "case" keyword to use named tuples? The error message with 2.11 is more explanatory: scala> l map { (b, n) => b + n } <console>:9: error: missing parameter type Note: The expected type requires a one-argument function accepting a 2-Tuple. Consider