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-24Python编程基础详解
- 2024-11-21Python编程基础教程
- 2024-11-20Python编程基础与实践
- 2024-11-20Python编程基础与高级应用
- 2024-11-19Python 基础编程教程
- 2024-11-19Python基础入门教程
- 2024-11-17在FastAPI项目中添加一个生产级别的数据库——本地环境搭建指南
- 2024-11-16`PyMuPDF4LLM`:提取PDF数据的神器
- 2024-11-16四种数据科学Web界面框架快速对比:Rio、Reflex、Streamlit和Plotly Dash
- 2024-11-14获取参数学习:Python编程入门教程