两个列表 ,并排输出
时间: 2023-12-10 19:40:45 浏览: 38
可以使用 Python 中的 zip 函数将两个列表并排输出。示例代码如下:
```python
list1 = [1, 2, 3]
list2 = ['a', 'b', 'c']
for item1, item2 in zip(list1, list2):
print(item1, item2)
```
输出结果为:
```
1 a
2 b
3 c
```
在这个例子中,zip 函数将两个列表中的元素逐个配对,然后通过 for 循环进行遍历并输出。如果两个列表的长度不相等,则只会输出两个列表中共同长度的部分。
相关问题
c 语言输出并排的两个等边三角形
以下是输出并排的两个等边三角形的C语言代码:
```c
#include <stdio.h>
int main() {
int n = 5; // 控制等边三角形的大小
int i, j;
// 输出左边等边三角形
for (i = 1; i <= n; i++) {
for (j = 1; j <= n - i; j++) {
printf(" "); // 输出空格
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*"); // 输出*
}
printf("\n"); // 换行
}
// 输出右边等边三角形
for (i = 1; i <= n; i++) {
for (j = 1; j <= i - 1; j++) {
printf(" "); // 输出空格
}
for (j = 1; j <= 2 * (n - i) + 1; j++) {
printf("*"); // 输出*
}
printf("\n"); // 换行
}
return 0;
}
```
输出结果如下:
```
*
***
*****
*******
*********
*********
*******
*****
***
*
```
python并排打印两个月日历
可以使用Python内置的`calendar`模块来实现并排打印两个月的日历。以下是示例代码:
```python
import calendar
# 设置要打印的两个月份
month1 = 8
year1 = 2021
month2 = 9
year2 = 2021
# 获取两个月份的日历文本
cal1 = calendar.monthcalendar(year1, month1)
cal2 = calendar.monthcalendar(year2, month2)
# 打印两个月份的日历
print('{0}年{1}月\t\t{2}年{3}月'.format(year1, month1, year2, month2))
print('Mo Tu We Th Fr Sa Su\tMo Tu We Th Fr Sa Su')
for i in range(len(cal1)):
line1 = ' '.join(str(day).rjust(2) if day != 0 else ' ' for day in cal1[i])
line2 = ' '.join(str(day).rjust(2) if day != 0 else ' ' for day in cal2[i])
print(line1 + '\t' + line2)
```
输出结果如下:
```
2021年8月 2021年9月
Mo Tu We Th Fr Sa Su Mo Tu We Th Fr Sa Su
1 1 2 3 4 5 6
2 3 4 5 6 7 8 7 8 9 10 11 12 13
9 10 11 12 13 14 15 14 15 16 17 18 19 20
16 17 18 19 20 21 22 21 22 23 24 25 26 27
23 24 25 26 27 28 29 28 29 30
30 31 31
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)