zoukankan      html  css  js  c++  java
  • 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);

      }

     }

    }

  • 相关阅读:
    [AT2064] [agc005_f] Many Easy Problems
    [AT2304] [agc010_c] Cleaning
    [AT2172] [agc007_e] Shik and Travel
    [AT2148] [arc063_c] Integers on a Tree
    [AT2363] [agc012_c] Tautonym Puzzle
    未能从程序集“netstandard, Version=2.0.0.0......”中加载类型“...”。
    Android Studio设置国内镜像代理
    新建一个浏览器APP
    Android Studio在Make Project时下载Grandle特别慢
    用JS添加和删除class类名
  • 原文地址:https://www.cnblogs.com/eosclover/p/15029739.html
Copyright © 2011-2022 走看看