tuples

converting string to tuple in python

 ̄綄美尐妖づ 提交于 2019-12-02 04:18:57
I have a string returnd from a software like "('mono')" from that I needed to convert string to tuple . that I was thinking using ast.literal_eval("('mono')") but it is saying malformed string. Since you want tuples, you must expect lists of more than element in some cases. Unfortunately you don't give examples beyond the trivial (mono) , so we have to guess. Here's my guess: "(mono)" "(two,elements)" "(even,more,elements)" If all your data looks like this, turn it into a list by splitting the string (minus the surrounding parens), then call the tuple constructor. Works even in the single

Oracle invalid identifier doesnt understand string

南笙酒味 提交于 2019-12-02 04:02:30
I'm having an issue with my query not working. This is the command variable. When it executes it should be retrieving the tuples that have BSc as their degree. I have tested this in oracle directly and the query returns these. It is identical to the command statement. When I print out command , the line looks exactly the same as my command that worked in oracle. SELECT distinct fname, lname, student_id FROM student where degree='BA'; Yet, it should be printing out to the screen. The tables are loaded into oracle already. I've been racking my brain with this issue but can't seem to find a fix!

convert tuple keys of dict into a new dict

北战南征 提交于 2019-12-02 04:00:28
I have a dict like this: { ('America', 25, 'm', 'IT'): 10000, ('America', 22, 'm', 'IT'): 8999, ('Japan', 24, 'f', 'IT'): 9999, ('Japan', 23, 'f', 'IT'): 9000 } Now, I want to get all result with key ('America', 'm', 'IT') , in this example. In the above, that would be: {25: 10000, 22: 8999} My current solution is below: res = dict() for key, cnt in stats.items(): country, age, sex, job = key try: res[(country, sex, job)][age] = cnt except KeyError as e: res[(country, sex, job)] = {} res[(country, sex, job)][age] = cnt print res['America', 'm', 'IT'] Do I have better ways to do is? since this

Is the empty tuple in Python a “constant” [duplicate]

懵懂的女人 提交于 2019-12-02 03:47:50
This question already has an answer here: compare object to empty tuple with the 'is' operator in Python 2.x 4 answers I want to make my code more (memory-)efficient. Right now we have a lot of functions that take an iterable as parameter like: def foo(para,meter,iterable): #... pass and sometimes we have to provide it an empty list to do its work properly: foo(14,25,[]) . The problem is that each time a new list is constructed: it requires to allocate on the heap, and a list seems to 64 bytes of memory (on my own machine, tested with sys.getsizeof([]) ) whereas the empty tuple only takes a

Getting unique tuples out of a python set

ぃ、小莉子 提交于 2019-12-02 03:12: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? Okay, so you've got a set {c1, c2, c3, ...} , where each cN is itself a collection of some sort. If you don't care about the order of the elements in cN , but do care that it is unique (disregarding order), then cN

Sorting a list of tuples with 3 elements in python

為{幸葍}努か 提交于 2019-12-02 03:08:11
问题 I have a list of some tuples. Each tuple has three elements. I need to sort the list. To break tie between two tuples, first element of tuple is looked then if still tied then the second element. List is like below. L = [(1, 14, 0), (14, 1, 1), (1, 14, 2), (14, 2, 3), (2, 4, 4), (4, 11, 5), (11, -1000, 6)] In C the sort function takes a compare function and that does everything simply. But I couldn't figure it out after trying for sometimes in python . Can anybody help me? 回答1: Just sort the

Swift Tuple index using a variable as the index?

醉酒当歌 提交于 2019-12-02 02:49:39
Swift Tuple index using a variable as the index? Anyone know if it is possible to use a variable as the index for a Swift tuple index. I wish to select and item from a tuple using a random number. I have the random number as a variable but cannot see how to use that as the index for a tuple. I have searched various places already Make sure you've chosen the correct data structure If you've reached a point where you need to access tuple members as if "indexed", you should probably look over your data structure to see if a tuple is really the right choice for you in this case. As MartinR

Create a single element tuple of tuple

大憨熊 提交于 2019-12-02 02:14:00
I just noticed that if you want to create a tuple with single element, being a tuple itself, you cannot do it with constructor tuple only with (,) syntax. Why is that? Example: >>> tuple(list('abc')) ('a', 'b', 'c') >>> tuple(tuple(list('abc'))) ('a', 'b', 'c') >>> (tuple(list('abc')),) (('a', 'b', 'c'),) But then it holds for a list >>> tuple([1],) (1,) >>> tuple([1]) (1,) I don't really see the issue, this adheres to the documentation: class tuple(object) | tuple() -> empty tuple | tuple(iterable) -> tuple initialized from iterable's items | | If the argument is a tuple, the return value is

python sort list of tuple

大兔子大兔子 提交于 2019-12-02 02:13:30
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! First of all, it sounds like you want to do this: Given a list of (name, score) tuples, return a list of names from highest score to lowest

Why single element tuple is interpreted as that element in python?

倾然丶 夕夏残阳落幕 提交于 2019-12-02 01:40:38
Could anyone explain why single element tuple is interpreted as that element in Python? And Why don't they just print the tuple (1,) as (1) ? See the examples below: >>> (1) 1 >>> ((((1)))) 1 >>> print(1,) 1 >>> print((1,)) (1,) A single element tuple is never treated as the contained element. Parentheses are mostly useful for grouping, not for creating tuples; a comma does that. Why don't they just print (1,) as (1)? Probably because printing a builtin container type gives a representation that can be used to recreate the container object via , say eval : The docs for __repr__ provides some