About conditional branching using a two-dimensional array in python

Asked 2 years ago, Updated 2 years ago, 21 views

2D array li available

 li = [[0,0,0],
    [0,2,0],
    [0,1,0],
    [0,0,0],
    [0,3,0],
    [0,0,0]]

The number in the middle column of the two-dimensional array li (0,2,1,0,3,0),
(Question 1) Decide if at least one number is greater than zero,
(Question 2) All 0s,
I would like to do with if statement.

(Question 3) Also, I would like to know the number in the middle column where the number above 0 appeared in the last line (in this case, the fifth where 3 appeared).

Thank you for your cooperation.

python

2022-09-30 19:33

5 Answers

I interpreted that I wanted to make all the decisions at once.
Let's say "center column" refers to the second element of "row" (1 in index).

I'm looking at the list from the end because I only need to know how many lines it appeared on.
If you find it, pull out the loop there.
The index of the list starts with zero, so you can display the number of rows by adding one to the index.

found_target=False
for i in range (len(li)-1,-1,-1):
    if li[i][1]>0:
        print("The last target is ", li[i][1], ", i+1, "line")
        found_target=True
        break
if not found_target:
    print("No Target")

iterated

import intertools as it
target=list(it.islice((i,v[1])for i,vin enumerate(reversed(li))if v[1]>0),1))
if target:
    print("The last target is ", target[0][1], ",len(li)-target[0][0], "line")
else:
    print("No Target")


2022-09-30 19:33

Try using numpy.

import numpy as np

li = [[0,0,0], [0,2,0], [0,1,0], [0,0,0], [0,3,0], [0,0,0]]
x = np.array([tuple(l)for lin li],
             dtype = [('l', 'int', ('c', 'int')], ('r', 'int')])

# Q1
any(x['c']>0)

# Q2
all(x['c'] == 0)

# Q3
np.where(x['c']>0)[0][-1]


2022-09-30 19:33

I tried writing using basic functions as much as possible.
Returns -1 if not found in question 3.

 li = [[0,0,0], [0,2,0], [0,1,0], [0,0,0], [0,3,0], [0,0,0]]

# Q1
print(reduce(lambdax,y:(y[1]>0)or x, li, False))
# Q2
print(reduce(lambdax,y:(y[1]>0) and x, li, True))
# Q3
print(reduce(lambdax,y:(lambdaz:(x[0]+1,z))(x[0]if y[1]>0 else x[1]), li,(0,-1)))[1]


2022-09-30 19:33

(Question 1) Decide if even one number is greater than zero

print sum(zip(*li)[1])>0

(Question 2) Decide all zeroes

 print sum(zip(*li)[1]) == 0

(Question 3) Also, on which line did the number above zero appear in the middle column?

for i, e in reversed (list(enumerate(zip(*li)[1]))):
    if > 0:
        print i+1,e
        break
else:
    print "not found"

Question 3 has a better answer.

EDIT: Combine tips from other people's answers.

center=zip(*li)[1]
# Q1
print any(map(lambdax:x>0,center))
# Q2
print all(map(lambdax:x==0,center))


2022-09-30 19:33

Answer A

>>>#Question 1
>>>any_positive=False
>> for x in li:
...     if x [1] > 0:
...         any_positive=True
...         break
... 
>>>any_positive
True

>>#Question 2
>>>all_zero=True
>> for x in li:
...     if x[1]!=0:
...         all_zero=False
...         break
... 
>>all_zero
False

>>#Question 3
>>>last_positive=None
>> for idx, x in enumerate (li):
...     if x [1] > 0:
...         last_positive=idx
... 
>>>last_positive
4

Answer B

>>>#Question 1
>>>any(x[1]>0 for x inli)
True

>>#Question 2
>>>all(x[1]==0 for x in li)
False

>>#Question 3
>>[i for i, x in enumerate(li) if x[1]>0][-1]
4


2022-09-30 19:33

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.