tuples

Python, split tuple items to single stuff

∥☆過路亽.° 提交于 2019-11-30 07:54:50
问题 I have tuple in Python that looks like this: tuple = ('sparkbrowser.com', 0, 'http://facebook.com/sparkbrowser', 'Facebook') and I wanna split it out so I could get every item from tuple independent so I could do something like this: domain = "sparkbrowser.com" level = 0 url = "http://facebook.com/sparkbrowser" text = "Facebook" or something similar to that, My need is to have every item separated. I tried with .split(",") on tuple but I've gotten error which says that tuple doesn't have

Swap values in a tuple/list inside a list in python?

假装没事ソ 提交于 2019-11-30 07:11:14
I have a tuple/list inside a list like this: [('foo','bar'),('foo1','bar1'),('foofoo','barbar')] What is the fastest way in python (running on a very low cpu/ram machine) to swap values like this... [('bar','foo'),('bar1','foo1'),('barbar','foofoo')] curently using: for x in mylist: self.maynewlist.append((_(x[1]),(x[0]))) Is there a better or faster way??? iabdalkader You could use map: map (lambda t: (t[1], t[0]), mylist) Or list comprehension: [(t[1], t[0]) for t in mylist] List comprehensions are preferred and supposedly much faster than map when lambda is needed, however note that list

Boost::Tuples vs Structs for return values

为君一笑 提交于 2019-11-30 06:26:22
问题 I'm trying to get my head around tuples (thanks @litb), and the common suggestion for their use is for functions returning > 1 value. This is something that I'd normally use a struct for , and I can't understand the advantages to tuples in this case - it seems an error-prone approach for the terminally lazy. Borrowing an example, I'd use this struct divide_result { int quotient; int remainder; }; Using a tuple, you'd have typedef boost::tuple<int, int> divide_result; But without reading the

How can I refer to a specific member of a Tuple of any size in F#

不羁的心 提交于 2019-11-30 06:07:41
问题 okay, this might be a silly question. So I have some tuples of size 4 so like (int,int,int,int) If it were a 2 tuple I could use fst(myTuple) to refer to the first element. How could I, say, refer to the third element of a 4 tuple? 回答1: Use pattern matching: let tup = 1, 2, 3, 4 let _,_,third,_ = tup printfn "%d" third // displays "3" This is described directly in the MSDN documentation for tuples: Tuples (F#) 回答2: Here's a version of @Daniels novel solution which calculates Rest offsets of

Single-element parethesized expressions/tuples vs common use of parentheses

半城伤御伤魂 提交于 2019-11-30 06:01:30
问题 Sorry if this is trivial - I am so new to swift, actually I have only looked at the language guide+reference for a few minutes. As far as I understand a parenthesized expression like (2,3) is used to construct a tuple, and (2) is a single-element tuple of type (Int) . But then what happens with common use of parentheses like (2+4) in expression (2+4)*5 ? Is this still a tuple of type (Int) multiplied by an Int ? 回答1: From Types in the Swift book: If there is only one element inside the

Is there a way to get the difference and intersection of tuples or lists in Python? [duplicate]

喜欢而已 提交于 2019-11-30 05:59:58
问题 This question already has answers here : Find intersection of two nested lists? (19 answers) Closed 5 years ago . If I have lists: a = [1, 2, 3, 4, 5] b = [4, 5, 6, 7, 8] c = a * b should give me: c = [4, 5] and c = a - b should give me: c = [1, 2, 3] Is this available for Python or do I have to write it myself? Would the same work for tuples? I will likely use lists as I will be adding them, but just wondering. 回答1: If the order doesn't matter, you can use set for this. It has intersection

Accessing a Specific Element in a Tuple

我们两清 提交于 2019-11-30 05:58:20
问题 Haskell-newbie reporting in. Question is as follows: In Haskell, we have fst and snd that return the first and the second elements of a 2-tuple. Why don't we have an easy way of accessing the i-th element from any tuple? Right now I have a 3-tuple, I want to read the 1st element and the only way of accomplishing this task is doing pattern-matching trickery. Why can't this be done easier? Or maybe there is some easy way? 回答1: What prevents the language from having the special construct you

how to add value to a tuple?

人盡茶涼 提交于 2019-11-30 05:56:51
问题 I'm working on a script where I have a list of tuples like ('1','2','3','4') . e.g.: list = [('1','2','3','4'), ('2','3','4','5'), ('3','4','5','6'), ('4','5','6','7')] Now I need to add '1234' , '2345' , '3456' and '4567' respectively at the end of each tuple. e.g: list = [('1','2','3','4','1234'), ('2','3','4','5','2345'), ('3','4','5','6','3456'), ('4','5','6','7','4567')] Is it possible in any way? 回答1: Tuples are immutable and not supposed to be changed - that is what the list type is

C# 7 tuples and lambdas

瘦欲@ 提交于 2019-11-30 05:34:12
With new c# 7 tuple syntax, is it possible to specify a lambda with a tuple as parameter and use unpacked values inside the lambda? Example: var list = new List<(int,int)>(); normal way to use a tuple in lambda: list.Select(value => value.Item1*2 + value.Item2/2); i expected some new sugar to avoid .Item1 .Item2 , like: list.Select((x,y) => x*2 + y/2); The last line does not work because it is treated as two parameters for lambda. I am not sure if there is a way to do it actually. EDIT: I tried double parentesis in lambda definition and it didn't work: ((x,y)) => ... , and maybe it was stupid

Python tuples sorting based on last element [duplicate]

血红的双手。 提交于 2019-11-30 05:31:13
问题 This question already has answers here : Tuple pairs, finding minimum using python (3 answers) Closed 6 years ago . Here is my question i have tuple1=[(1, 3), (3, 2), (2, 1)] i want to sort tuple based on last digit of each tuple so the resultant will look like this output=[(2, 1), (3, 2), (1, 3)] below is my code i=0 for x in tuples: c.append(x[len(x)-1]) last=sorted(c) for y in last.iteritems(): if(y in x[len(x)-1]): print x #b.insert(i,x) i=i+1 after running iam getting an error message