[Solved] How can i convert a string to tuple in python

Use ast.literal_eval to convert the string to a tuple. >>> s = “(‘Who is Shaka Khan?’,{‘entities’: [(7, 17, ‘PERSON’)]}),” >>> import ast >>> t = ast.literal_eval(s) >>> t[0] (‘Who is Shaka Khan?’, {‘entities’: [(7, 17, ‘PERSON’)]}) >>> t[0][0] ‘Who is Shaka Khan?’ >>> t[0][1] {‘entities’: [(7, 17, ‘PERSON’)]} Optionally, you can convert it to a … Read more

[Solved] Spanning repeatable keys

In Elixir it’s quite easy with Enum.group_by/3: iex> Enum.group_by(values, fn {key, _} -> key end, fn {_, value} -> value end) %{ “Caerus1” => [“Ramses Refiner”, “Jupiter Refiner”, “Jupiter Other”, “Trader 13”, “Cathode Supplier 4”], “Dionysus3” => [“Cathode Supplier 4”, “Ramses Refiner”, “Trader 13”, “Jupiter Refiner”, “Jupiter Other”], “Prometheus2” => [“Jupiter Other”, “Ramses Refiner”, “Trader … Read more

[Solved] Creating numbered list of output

You’d still use enumerate(); you didn’t show how you used it but it but it solves your issue: for index, (value,num) in enumerate(sorted_list, start=1): print(“{}.\t{:<5}\t{:>5}”.format(index, value,num)) I folded your str.ljust() and str.rjust() calls into the str.format() template; this has the added advantage that it’ll work for any value you can format, not just strings. Demo: … Read more

[Solved] Iterate over list

You need to break up the rows and convert each value to an integer. At the moment you are looking for the presence of the string “3” which is why strings like “2;13” pass the test. Try something like this: list_6 = [“4;99”, “3;4;8;9;14;18”, “2;3;8;12;18”, “2;3;11;18”, “2;3;8;18”, “2;3;4;5;6;7;8;9;11;12;15;16;17;18”, “2;3;4;8;9;10;11;13;18”, “1;3;4;5;6;7;13;16;17”, “2;3;4;5;6;7;8;9;11;12;14;15;18”, “3;11;18”, “2;3;5;8;9;11;12;13;15;16;17;18”, “2;5;11;18”, “1;2;3;4;5;8;9;11;17;18”, … Read more

[Solved] How to change this list tuple into list only in Python? [closed]

test = [[(u’hello’,), (u’hello’,)], [(u’hello’,)]] for i, part_i in enumerate(test): for j, part_j in enumerate(part_i): test[i][j] = str(part_j[0]) Or, if you prefer the one-line version: test = [[(u’hello’,), (u’hello’,)], [(u’hello’,)]] result = [[str(j[0]) for j in i] for i in test] solved How to change this list tuple into list only in Python? [closed]

[Solved] Count occurences of all items of a list in a tuple

Being NumPy tagged, here’s a NumPy solution – In [846]: import numpy as np In [847]: t = (1,5,2,3,4,5,6,7,3,2,2,4,3) In [848]: a = [1,2,3] In [849]: np.in1d(t,a).sum() Out[849]: 7 # Alternatively with np.count_nonzero for summing booleans In [850]: np.count_nonzero(np.in1d(t,a)) Out[850]: 7 Another NumPy one with np.bincount for the specific case of positive numbered elements in … Read more

[Solved] Tuple Errors Python

It is saying that on this line: med_sev =(accidents[quotient][1] + accidents[quotient+2][1])/2 you are trying to index something that doesn’t exist. I imagine it is on the part accidents[quotient+2][1] because this is the greater indexed one. What does this mean? Well suppose that accidents is something like accidents = [[thing0, thing1], [thing2, thing3]] now say the … Read more

[Solved] Haskell, tuple (double, string) [closed]

You can implement it using mapM_/traverse_ or the flipped argument versions: forM_/for_. I prefer for_ since it looks more like the “enhanced for-loop” from languages like Java. import Data.Foldable (for_) myPutStr :: [(Double,String)] -> IO () myPutStr vals = do for_ vals $ \(num, str) -> do putStr str putStr “: ” print (num * … Read more

[Solved] Python Defining Lists in Lists

Lists don’t take name-value pairs. You probably want dictionaries here instead: definitions = { ‘title_label’: { ‘text’: (236, 218, 51), ‘background’: (125, 142, 246) }, ‘start_button’: { ‘text’: (32, 40, 145), ‘background’: (236, 235, 136), ‘pressed’: (44, 51, 112) }, ‘quit_button’: { ‘text’: (166, 21, 13), ‘background’: (48, 61, 188), ‘pressed’: (31, 40, 129) } … Read more