python - Best way to turn a list into a dict, where the keys are a value of each object? -
I am trying to get a list of objects, and have changed that list to a single dot.
now The code works, but it is a bit ugly, and a little slow, does someone give examples of something that is fast / "better"? EDIT: OK, thanks for the answer. I should say that the hand made with the slow appears I am surprised to see more dragonically. edit2: OK, I updated the test code to make it more readable, with so many tests. Here's where we are in accordance with the code, I put the authors in the code and if I make any disturbances then please tell me. Client (IT) for the time of importing from ITROTOLES import: DF __It__ (Self, Name): Self NN = Name Object_list = [] Category (5): object_list.append (/) Object_list in object_dict [an_object.name ] = An_object def example_2 (): D = DEC provided by 'hyperborean' (zip ([o.name for o in object list], object_list) def example_3 (): 'provided by jason baker' d = dict (For an_object in object_list ([an_object.name, an_object]) def example_4 (): "Added izip hyper code to su Shed by Chris Cameron, Ted "D = Dick" (izip (o_name for o in object_list), object_list) DEF example_5 (): 'Improve by zip, John Foohi' D = Dik (zip for oo Object_list), object_list)) def (example_6): 'izip, improved by John Fuhhi' d = dict (izip (o.name for o in the object list), object_list) def define example_7 (): 'provided by Jason Baker Gone, deleted time foundation (time, time) For the time (example '% s ()'). % (For example, for example, for example, for example, for example,% _ '% s'% example_index)) for the category (7): timeit_object = timeits [i] Example # % S result: "% s" '% (i + 1, timeit_object) I get the result with 5 objects in the list: and finally, with 500 objects: Everyone answered that thanks! Im very surprised with the results. If there are any other tips for any faster way then I would love to hear them. Thanks all! In Dragon 3.0 you can use a word understanding: It is also possible in Python 2, but it is a bit stupid: some classes (name = 'A'), some classes (Name = 'c'), some class (name = 'c'), some class (object): def __init __ (self, name): self.name = name object_list = [SomeClass (name = 'a'), (Name = 'd'), someClass (name = 'e'),] object_dict = {} for the item_list for an_object: object_dict [an_object.name] = an_object
Example # 1 result: "[1.2428441047668457, 1.2431108951568604] Repeat "(2))" Examples # 2 Results: "[3.356775 9990692139, 3.3188660144 805908]" Example # 3 Result: "[2.8346641063690186, 2.8344728946685791]" Example # 4 Result: "[ 3.0710639953613281, 3.0573830604553223] "Example # 5 Results:" [5.2079 9 18384552002, 5.2170760631561279] "Example # 6 Results:" [3.240635871887207, 3.240212 9173278809] "Example # 7 Result:" [3.0856869220733643, 3.0688989162445068] "with <50 and <50>
ud Example # 1: "[96.682723999023438, 96.678673028945923]" Example # 2 Result: "[137.4 9 416589736938, 137.48705387115479]" Example # 3 Result: "[136.58069896697998, 136.5823769569397]" Example # 4 Result: "[115.0344090461731, 115.1088011264801] Example # 5: "[165.08325910568237, 165.06769108772278]" Example # 6 Result: "[128.951874 9 7138 9 77, 128.96077489852905]" Example # 7 Results: "[155.70515 9 90257263, 155.74126601219177]" < Code> {an_object.name: object_list for an_object in an_object}
[(An_object.name, an_object) for an_object in object (Object_list)
Comments
Post a Comment