Python 将多个列表相同索引的元素进行拼接并输出
2021/7/19 14:37:06
本文主要是介绍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);
}
}
}
这篇关于Python 将多个列表相同索引的元素进行拼接并输出的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-14获取参数学习:Python编程入门教程
- 2024-11-14Python编程基础入门
- 2024-11-14Python编程入门指南
- 2024-11-13Python基础教程
- 2024-11-12Python编程基础指南
- 2024-11-12Python基础编程教程
- 2024-11-08Python编程基础与实践示例
- 2024-11-07Python编程基础指南
- 2024-11-06Python编程基础入门指南
- 2024-11-06怎么使用python 计算两个GPS的距离功能-icode9专业技术文章分享