简体   繁体   中英

To find array with smallest length among multiple 1D arrays [PYTHON]

Is there any inbuilt function in Python to achieve this, or i have to do this the traditional way. Eg

A1 = [1,2,3]
A2 = [1]
A3 = [1.2]

OUTPUT - A2

Try this,

A1 = [1,2,3]
A2 = [1]
A3 = [1.2]
print(min([A1,A2,A3],key=len))

Output:

[1]

Try this.

A1 = [1,2,3]
A2 = [1]
A3 = [1,2]
A = []
A = [A1, A2, A3] # make list of lists
min = 10000000 # some threshold 
for a in A: # iterate bigger list
    if(len(a) < min): # compare at each step
        min = len(a) # change value of min if condition is met
print(len(a)) # print reqd result

Output:

1

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