Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Searching in the list
#1
Here is my list:
mylist=[((-6.952686099999967e-11+1.761756624e-08j), ('z', 'x'), 3),
 ((5.815437999999992e-11+4.8251177450000006e-09j), ('z', 'y'), 3),
 ((1.0000000046778146e-10-1.3938548489871208e-07j), ('z', 'z'), 3),
 (-1.0177872508275367e-14j, ('i', 'i'), 4),
 ((3.509199999997637e-11-2.8019842999999995e-08j), ('i', 'x'), 4),
 ((-5.506999999991924e-12-7.678366000000028e-09j), ('i', 'y'), 4),
 (2.856982212740396e-11j, ('i', 'z'), 4),
 ((-1.4805140019017436e-14+1.0000030317702802e-14j), ('x', 'i'), 4),
 ((2.797933399999993e-08+2.7334999999948807e-11j), ('x', 'x'), 4),
 ((7.658505000000086e-09+2.2608000000007e-11j), ('x', 'y'), 4),
 ((2.8569194859907727e-11+1.0000030317702802e-14j), ('x', 'z'), 4),
 ((0.00150217760805+1.06423431460468e-06j), ('y', 'i'), 4),
 ((-7.590475000000006e-09+9.621823159999999e-07j), ('y', 'x'), 4),
 ((-2.0047800000000196e-10-3.513209979e-06j), ('y', 'y'), 4),
 ((-0.0014830912399500002-1.06423566139532e-06j), ('y', 'z'), 4),
 ((-0.0007610956399999999-2.6349367689196727e-06j), ('z', 'i'), 4),
 ((-8.102180000000152e-10-4.84889551e-07j), ('z', 'x'), 4),
 ((-3.2791430000000017e-09+1.770466772e-06j), ('z', 'y'), 4),
 ((0.0066300876+2.6349082110803273e-06j), ('z', 'z'), 4),
 (9.28133670194649e-13j, ('i', 'i'), 5),
 ((-2.126432999999905e-10+3.529988499999997e-09j), ('i', 'x'), 5),
 ((7.76461500000003e-10+9.664799000000092e-10j), ('i', 'y'), 5),
 (5.169533670206653e-12j, ('i', 'z'), 5),
 ((9.2924723999865e-13+1.000000321264849e-15j), ('x', 'i'), 5),
 ((-3.5053348110000006e-09+2.1128481000000066e-10j), ('x', 'x'), 5),
 ((-9.598153699999995e-10-7.775324310000001e-10j), ('x', 'y'), 5),
 ((5.183447240001499e-12+1.000000321264849e-15j), ('x', 'z'), 5),
 ((-7.0749027767e-05-5.104494949128e-08j), ('y', 'i'), 5),
 ((-1.4009994999999953e-10-6.505460769e-09j), ('y', 'x'), 5),
 ((-6.740733099999971e-11+2.3753205370000002e-08j), ('y', 'y'), 5),
 ((7.243791743300001e-05+5.104493070872e-08j), ('y', 'z'), 5),
 ((0.0004369010896+1.2637836156867021e-07j), ('z', 'i'), 5),
 ((-6.231389999999666e-11+1.061803573e-07j), ('z', 'x'), 5),
 ((9.907709999999952e-11-3.8769503270000003e-07j), ('z', 'y'), 5),
 ((8.238726240000001e-05-1.263845238313298e-07j), ('z', 'z'), 5),
 ((-1.4962703510000001e-05-1.0089410326098014e-06j), ('i', 'i'), 6),
 ((-2.28946504e-09-7.802598799999998e-09j), ('i', 'x'), 6),
 ((-2.640385599999993e-09+2.7748931979999998e-08j), ('i', 'y'), 6),
 ((4.4179216299999996e-06+3.0279336739020104e-07j), ('i', 'z'), 6),
 ((-1.0120334000000066e-06+1.5008565252000001e-05j), ('x', 'i'), 6),
 ((-5.537917900000002e-09-1.7876646050000006e-09j), ('x', 'x'), 6),
 ((2.0592603814999998e-08+1.0002269000000035e-09j), ('x', 'y'), 6),
 ((3.036782000000117e-07-4.430818520000001e-06j), ('x', 'z'), 6),
 ((-1.9588212599999994e-06+2.779229759999999e-05j), ('y', 'i'), 6),
 ((-1.6906651105e-08-1.5394983900000003e-08j), ('y', 'x'), 6),
 ((6.54755323e-08+5.918856850000006e-10j), ('y', 'y'), 6), 
 ((1.6500015848e-05-0.0002449334572j), ('y', 'z'), 6),
 ((-3.7513685100000005e-06+5.54294812453902e-05j), ('z', 'i'), 6),
 ((-3.98069623e-08+6.100807459999999e-08j), ('z', 'x'), 6),
 ((1.285907654e-07+2.6119755079999998e-08j), ('z', 'y'), 6),
 ((3.7040802299999995e-06-5.47233335546098e-05j), ('z', 'z'), 6),
 ((4.3994853030000004e-11-2.9301111717999997e-11j), ('i', 'i'), 7),
 ((0.0002155719519-0.0001436161988j), ('i', 'x'), 7),
 ((5.91147226e-05-3.92209653e-05j), ('i', 'y'), 7),
 ((-6.980665000000098e-14+5.909438199999978e-14j), ('i', 'z'), 7),
 ((2.896651617e-11+4.3492057369999996e-11j), ('x', 'i'), 7),
 ((-0.000144218879-0.00021660066809999997j), ('x', 'x'), 7),
 ((-3.96102485e-05-5.9247126799999995e-05j), ('x', 'y'), 7),
 ((6.67734500000005e-14+8.100622999999956e-14j), ('x', 'z'), 7),
 ((-3.509643029999999e-12-5.206854250000002e-12j), ('y', 'i'), 7),
 ((1.7894210999999958e-06+2.693278199999993e-06j), ('y', 'x'), 7),
 ((4.952980000000065e-07+7.341504999999984e-07j), ('y', 'y'), 7),
 ((4.34031263e-12+6.85848503e-12j), ('y', 'z'), 7)]
What I want to do is:
1) find the all the numbers belong the same string like ('y', 'y') ('x', 'y'), or ('z', 'z') . For example: Let's consider ('y','y')
('y', 'y') has 4.952980000000065e-07+7.341504999999984e-07j and
('y', 'y') has(6.54755323e-08+5.918856850000006e-10j and
('y', 'y') has -2.0047800000000196e-10-3.513209979e-06j, and
('y', 'y') has -6.740733099999971e-11+2.3753205370000002e-08
2) multiply with theirself and then sum all of them:
4.952980000000065e-07+7.341504999999984e-07j * 4.952980000000065e-07+7.341504999999984e-07j + 6.54755323e-08+5.918856850000006e-10j*6.54755323e-08+5.918856850000006e-10j+ -2.0047800000000196e-10-3.513209979e-06j*-2.0047800000000196e-10-3.513209979e-06j + -6.740733099999971e-11+2.3753205370000002e-08 * -6.740733099999971e-11+2.3753205370000002e-08
3)Do the same things for all different name like yy, xy,yz etc..

How to do that in efficient way in python?
Reply
#2
You could use a dictionary
from collections import defaultdict
d = defaultdict(complex)
for c, name, n in mylist:
    d[name] += c * c

print(d)
quest likes this post
Reply
#3
Identical code with better names and as a function.

from collections import defaultdict


def calc(data):
    results = defaultdict(complex)
    for value, symbols, mysterious_number in data:
        results[symbols] += value ** 2
    return results
quest likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#4
DeaD_EyE Wrote:Identical code with better names and as a function.
«more descriptive» names would be more accurate,
Reply
#5
(Mar-02-2022, 06:47 AM)Gribouillis Wrote: You could use a dictionary
from collections import defaultdict
d = defaultdict(complex)
for c, name, n in mylist:
    d[name] += c * c

print(d)
Thank you very much
This does the job!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Linear search/searching for a word in a file/list kietrichards 3 3,473 Mar-08-2019, 07:58 PM
Last Post: Larz60+
  Searching through a list of dictionaries with a condition. Mr_Keystrokes 11 5,277 Oct-05-2018, 11:03 AM
Last Post: Mr_Keystrokes
  searching within specified variable list milo204 2 39,648 Feb-13-2018, 09:43 AM
Last Post: milo204
  Searching for items within a list Yobby 9 6,257 Jul-03-2017, 05:53 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020