简体   繁体   中英

How do I make a condition to find out if the 2nd or 1st item in a list was selected?

I have these points

[(3, 4), (3, 6), (3, 8), (4, 5), (4, 7), (5, 1), (5, 5), (7, 3), (7, 5), (8, 5)]

and I conducted some operations on them, to finally get this list

[(6.0, 3.605551275463989), (6.324555320336759, 5.385164807134504), (7.211102550927978, 7.280109889280518), (5.0990195135927845, 5.0), (5.830951894845301, 6.708203932499369), (5.0, 4.0), (4.123105625617661, 5.656854249492381), (2.23606797749979, 6.324555320336759), (2.23606797749979, 7.211102550927978), (1.4142135623730951, 8.06225774829855)]

The first item in the list, for example (6.0, 3.605551275463989) corresponds to the first point, for example (3,4 ). I found the minimum between the two items in the first item of the list and came up with this:

[3.605551275463989, 5.385164807134504, 7.211102550927978, 5.0, 5.830951894845301, 4.0, 4.123105625617661, 2.23606797749979, 2.23606797749979, 1.4142135623730951]

what I want to do now is for example, if the minimum turned out to be the 2nd item, as it did in the first case, I want to write it as follows: (3,4) M2 , M2 meaning it was the 2nd item. How do I do this? I don't even know where to start with this problem because I haven't run into anything similar.

You mean something like this?

points = [(3, 4), (3, 6), (3, 8), (4, 5),
          (4, 7), (5, 1), (5, 5), (7, 3), (7, 5), (8, 5)]

results = [(6.0, 3.605551275463989), (6.324555320336759, 5.385164807134504), (7.211102550927978, 7.280109889280518), (5.0990195135927845, 5.0), (5.830951894845301, 6.708203932499369), (5.0, 4.0), (4.123105625617661, 5.656854249492381), (2.23606797749979, 6.324555320336759),
           (2.23606797749979, 7.211102550927978), (1.4142135623730951, 8.06225774829855)]

assert(len(points) == len(results))

for i, r in enumerate(results):
    if r[1] < r[0]:
        num = 2
    else:
        num = 1
    print(f'{points[i]}, M{num}')

a = [(6.0, 3.605551275463989), (6.324555320336759, 5.385164807134504), (7.211102550927978, 7.280109889280518), (5.0990195135927845, 5.0), (5.830951894845301, 6.708203932499369), (5.0, 4.0), (4.123105625617661, 5.656854249492381), (2.23606797749979, 6.324555320336759), (2.23606797749979, 7.211102550927978), (1.4142135623730951, 8.06225774829855)]
b = [(3, 4), (3, 6), (3, 8), (4, 5), (4, 7), (5, 1), (5, 5), (7, 3), (7, 5), (8, 5)]
for i in range(len(a)):
    j = list(a[i])
    print(f'{b[i]}-->{max(j)},M{j.index(max(j))+1}')

I converted every tuple to an list and use the list methods...

Output

(3, 4)-->6.0,M1
(3, 6)-->6.324555320336759,M1
(3, 8)-->7.280109889280518,M2
(4, 5)-->5.0990195135927845,M1
(4, 7)-->6.708203932499369,M2
(5, 1)-->5.0,M1
(5, 5)-->5.656854249492381,M2
(7, 3)-->6.324555320336759,M2
(7, 5)-->7.211102550927978,M2
(8, 5)-->8.06225774829855,M2

Use zip() to loop over all 3 lists in parallel. Then just compare the selected value from the item in the 2nd list with the corresponding value in the third list to know whether to print M1 or M2 .

l1 = [(3, 4), (3, 6), (3, 8), (4, 5), (4, 7), (5, 1), (5, 5), (7, 3), (7, 5), (8, 5)]
l2 = [(6.0, 3.605551275463989), (6.324555320336759, 5.385164807134504), (7.211102550927978, 7.280109889280518), (5.0990195135927845, 5.0), (5.830951894845301, 6.708203932499369), (5.0, 4.0), (4.123105625617661, 5.656854249492381), (2.23606797749979, 6.324555320336759), (2.23606797749979, 7.211102550927978), (1.4142135623730951, 8.06225774829855)]
l3 = [3.605551275463989, 5.385164807134504, 7.211102550927978, 5.0, 5.830951894845301, 4.0, 4.123105625617661, 2.23606797749979, 2.23606797749979, 1.4142135623730951]

for i1, (m1, m2), selected in zip(l1, l2, l3):
    if selected == m1:
        which = "M1"
    else:
        which = "M2"
    print(i1, which)

How about this

lst1 = [(3, 4), (3, 6), (3, 8), (4, 5), (4, 7), (5, 1), (5, 5), (7, 3), (7, 5), (8, 5)]
lst2 = [(6.0, 3.605551275463989), (6.324555320336759, 5.385164807134504), (7.211102550927978, 7.280109889280518), (5.0990195135927845, 5.0), (5.830951894845301, 6.708203932499369), (5.0, 4.0), (4.123105625617661, 5.656854249492381), (2.23606797749979, 6.324555320336759), (2.23606797749979, 7.211102550927978), (1.4142135623730951, 8.06225774829855)]

for i, pt in enumerate(lst2):
    argmin = pt.index(min(pt))
    print(lst1[i], 'M{}'.format(argmin + 1))

Output:

(3, 4) M2
(3, 6) M2
(3, 8) M1
(4, 5) M2
(4, 7) M1
(5, 1) M2
(5, 5) M1
(7, 3) M1
(7, 5) M1
(8, 5) M1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM