簡體   English   中英

如何獲取二維數組/列表中項目的最長字符串?

[英]How to get the longest string for item in a 2D array/list?

首先,我很抱歉我不能很好地為這個問題提到主題名稱。 我試圖打印列長度的客戶記錄表,記錄長度將自動完美對齊。

customer_list = [["ctm1","jacon","jackson@gmail.com","+60166197213","15/5/1990","jadsfsd23"],["ctm1","jacksonmartin","jacksonmartinez@gmail.com","+60166197213","15/5/1990","jadsfsddfdf23"]]

# show records of customer account info
def view_customer_account():
     # initialize column length
     Ctm_ID_CL, Usrname_CL, Email_CL = 2,8,13
     # loop for longest data length and make it as column length
     # column length will remain as initial column length if longest data length is shorter than it
     for customer in customer_list:
          if len(customer[0]) > Ctm_ID_CL:
               Ctm_ID_CL = len(customer[0])
          if len(customer[1]) > Usrname_CL: 
               Usrname_CL = len(customer[1]) 
          if len(customer[2]) > Email_CL:
               Email_CL = len(customer[2])     
     # print column name
     # column name string will concatenate with a space which multiply with (column length - initial column length)
     print(f'| ID{" "*(Ctm_ID_CL-2)} | Username{" "*(Usrname_CL-8)} | Email Address{" "*(Email_CL-13)} | Phone number | Date of birth |')
     # print records
     # records(str) will concatenate with a space which multiply with (column length/initial column length) - records str length
     for customer in customer_list:
          print(f'| {customer[0]+" "*(Ctm_ID_CL-len(customer[0]))} | {customer[1]+" "*(Usrname_CL-len(customer[1]))} | {customer[2]+" "*(Email_CL-len(customer[2]))} | {customer[3]} | {customer[4]}'+" "*5+"|")

view_customer_account()

但是,我為列表中的每個項目查找最長字符串長度並使其作為列的長度會在有更多字段進入時重復很多的方式,這就是這部分:

 # initialize column length
 Ctm_ID_CL, Usrname_CL, Email_CL = 2,8,13
 # loop for longest data length and make it as column length
 # column length will remain as initial column length if longest data length is shorter than it
 for customer in customer_list:
      if len(customer[0]) > Ctm_ID_CL:
           Ctm_ID_CL = len(customer[0])
      if len(customer[1]) > Usrname_CL: 
           Usrname_CL = len(customer[1]) 
      if len(customer[2]) > Email_CL:
           Email_CL = len(customer[2])
      # more fields coming in, will have a lots if else statement

有沒有辦法解決這個問題?

我已經編輯了您的代碼並添加了column_length = [0,0,0,0,0]作為列長度列表,並使用for循環來評估長度。 希望它達到了你的目的。

customer_list = [["ctm1","jacon","jackson@gmail.com","+60166197213","15/5/1990","jadsfsd23"],["ctm1","jacksonmartin","jacksonmartinez@gmail.com","+60166197213","15/5/1990","jadsfsddfdf23"]]

# # show records of customer account info
def view_customer_account():
    # initialize column length
    column_length = [0,0,0,0,0]
    # loop for longest data length and make it as column length
    # column length will remain as initial column length if longest data length is shorter than it
    for customer in customer_list:
        for i in range(5):
            if len(customer[i]) > column_length[i]:
                column_length[i] = len(customer[i])    
    # print column name
    # column name string will concatenate with a space which multiply with (column length - initial column length)
    print(f'| ID{" "*(column_length[0]-2)} | Username{" "*(column_length[1]-8)} | Email Address{" "*(column_length[2]-13)} | Phone{" "*(column_length[3]-5)} | DOB{" "*(column_length[4]-3)} |')
    # print records
    # records(str) will concatenate with a space which multiply with (column length/initial column length) - records str length
    for customer in customer_list:
        print(f'| {customer[0]+" "*(column_length[0]-len(customer[0]))} | {customer[1]+" "*(column_length[1]-len(customer[1]))} | {customer[2]+" "*(column_length[2]-len(customer[2]))} | {customer[3]+" "*(column_length[3]-len(customer[3]))} | {customer[4]+" "*(column_length[4]-len(customer[4]))}'+" |")

view_customer_account()

Output:

| ID   | Username      | Email Address             | Phone        | DOB       |
| ctm1 | jacon         | jackson@gmail.com         | +60166197213 | 15/5/1990 |
| ctm1 | jacksonmartin | jacksonmartinez@gmail.com | +60166197213 | 15/5/1990 |

您可以為此使用制表模塊。

from tabulate import tabulate

customer_list = [["ctm1","jacon","jackson@gmail.com","+60166197213","15/5/1990","jadsfsd23"],["ctm1","jacksonmartin","jacksonmartinez@gmail.com","+60166197213","15/5/1990","jadsfsddfdf23"]]

# show records of customer account info
def view_customer_account():
    print(tabulate(customer_list, headers=['ID', 'Username', 'Email Address', 'Phone number', 'Date of birth', 'etc']))

view_customer_account()

結果是:

ID    Username       Email Address                Phone number  Date of birth    etc
----  -------------  -------------------------  --------------  ---------------  -------------
ctm1  jacon          jackson@gmail.com            +60166197213  15/5/1990        jadsfsd23
ctm1  jacksonmartin  jacksonmartinez@gmail.com    +60166197213  15/5/1990        jadsfsddfdf23

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM