Python 將多個列表相同索引的元素進行拼接並輸出


將多個列表相同索引的元素進行拼接並輸出

Python:

a=['阿莫西林克拉維酸鉀分散片', '*清肺消炎丸', '*小兒咳喘靈口服液', '阿法骨化醇軟膠囊']

b=['5/', '5/', '2/', '0.25ug/']

c=['08:00,12:00,18:00,10:46,10:58',

  '16:04',

  '08:00,11:43,11:39,11:20,11:26',

  '08:00,12:00,16:00,16:01,16:21,16:10']

len_a=len(a)

len_b=len(b)

len_c=len(c)

if len_a !=len_b or len_a != len_c:

    print("葯品、葯品規格、時間個數不一致")

all_list=[]

for i  in range(len_a):

    #| 分隔

    all_list.append(a[i]+"|"+ b[i] +"|" +c[i])

print(all_list)

 

Java:

import java.util.Arrays;

public class demoList {

public static void main(String[] args) throws Exception {

  String a[] = { "阿莫西林克拉維酸鉀分散片", "*清肺消炎丸", "*小兒咳喘靈口服液", "阿法骨化醇軟膠囊" };

  String b[] = { "5/", "5/", "2/", "0.25ug/" };

  String c[] = { "08:00,12:00,18:00,10:46,10:58", "16:04", "08:00,11:43,11:39,11:20,11:26",

    "08:00,12:00,16:00,16:01,16:21,16:10" };

  int aLength = a.length;

  int bLength = b.length;

  int cLength = c.length;

  //三個數組元素不一致則拋異常

  if (aLength != bLength || aLength != cLength) {

   throw new Exception("葯品、葯品規格、時間個數不一致");

  }   

  String result[] = new String[aLength];

  //每個元素拼接格式

  String formatStr = "'%s'-'%s'-'%s'";

  //拼接三個數組元素

  for(int i = 0 ; i<aLength ; i++) {

   result[i] = String.format(formatStr, a[i], b[i], c[i]);

  }

  

  //打印成一行

  System.out.println(Arrays.toString(result));

  System.out.println("==========================================================");

  //每個元素分開打印

  for (String string : result) {

   System.out.println(string);

  }

 }

 

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM