{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 序列拼接与重复"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"字符串、元组与列表等序列类型都支持拼接与重复的操作。"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"序列拼接是通过“+”将两个相同类型的序列拼接为一个包含参与拼接的序列中所有元素的新序列。"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdin",
"output_type": "stream",
"text": [
" 45\n",
" 36\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"3645\n",
"3645\n",
"36 45\n"
]
}
],
"source": [
"# 字符串拼接\n",
"first_name = input() # 输入名\n",
"last_name = input() # 输入姓\n",
"\n",
"# 输出姓名\n",
"full_name = last_name + first_name # 将姓与名拼接为一个字符串\n",
"print(full_name) # 赵广辉, 拼接后是一个对象,输出时中间无空格\n",
"print(last_name + first_name) # 赵广辉, 拼接后是一个对象,输出时中间无空格\n",
"print(last_name, first_name) # 赵 广辉,输出多个对象时,默认中间有空格"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 列表拼接为新的列表对象"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['李明', 84, 80, 95, 88, 96, 76, 65, 85, 98, 55]\n"
]
}
],
"source": [
"stu_name = ['李明']\n",
"score_2021 = [84, 80, 95, 88]\n",
"score_2022 = [96, 76, 65, 85, 98, 55]\n",
"\n",
"stu_score = stu_name + score_2021 + score_2022 # 3个列表拼接到一起\n",
"print(stu_score) # ['李明', 84, 80, 95, 88, 96, 76, 65, 85, 98, 55]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"stu_name = ('李明',) # 注意括号中的逗号不可少,('李明')类型为字符串<class 'str'>\n",
"score_2021 = (84, 80, 95, 88)\n",
"score_2022 = (96, 76, 65, 85, 98, 55)\n",
"\n",
"stu_score = stu_name + score_2021 + score_2022\n",
"print(stu_score) # ('李明', 84, 80, 95, 88, 96, 76, 65, 85, 98, 55)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 序列重复 s * n 是将一个序列 s 乘以一个整数 n产生一个新序列,新序列是 s 中的元素重复 n 次。"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"------------------------------\n",
" 欢迎光临 \n",
"------------------------------\n"
]
}
],
"source": [
"print('-' * 30) # 字符串'-' 重复30次,得到新字符串'------------------------------'\n",
"print(f'{\"欢迎光临\":^30}') # 30表示占30字符宽度,^ 表示居中对齐\n",
"print('-' * 30)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 序列重复时,当 n 小于或等于0时会被当作 0 来处理,此时序列重复0 次的操作将产生一个空序列。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print('-' * 0) # 输出空字符串\n",
"print('-' * (-5)) # 输出空字符串"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 实例 6.5 数据脱敏 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"手机号属于个人隐私信息,编程将用户输入的手机号的4-7位用“*”替换。输入格式为: 11位数字的手机号码,如:13912345678。"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tel = input() # 输入13213213211\n",
"tel_mask = tel[:3] + '*' * 4 + tel[-4:] # 字符串切片、拼接和重复\n",
"print(tel_mask) # 132****3211"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 实例 6.6 约瑟夫环问题 "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"有20个人围坐在一张圆桌周围,从第1个人开始报数,数到3的那个人出列,他的下一个人又从1开始报数,数到3的那个人又出列; \n",
"依此规律重复下去,直到圆桌周围的人数少于3时结束,循环输出每次出列的人和剩下的人的序号。"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"20个人围成一个圈可以用一个列表来表示:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"joseph_ring = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"每次将列表的第3个元素去除,可以切片取前2个元素joseph_ring[:2],拼接到列表从第4个元素到末尾的切片joseph_ring[3:]:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"joseph_ring = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n",
"joseph_ring = joseph_ring[3:] + joseph_ring[:2] # 跳过第3个元素\n",
"print(joseph_ring) # [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"joseph_ring = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]\n",
"while len(joseph_ring) > 2: # 列表长度大于m-1时,去掉第m个元素\n",
" print(joseph_ring[2], end=' ') # 输出当前出列的元素\n",
" joseph_ring = joseph_ring[3:] + joseph_ring[:2] # 两个列表拼接为一个新列表\n",
" print(joseph_ring) # 输出列表中剩余元素"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<font face='楷体' color='red' size=5> 练一练 </font>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"约瑟夫环问题可以扩展为一圈共有n个人,从 1 开始报数,报到m的人出列,然后重新开始报数,剩余人数小于m时停止,输出最后剩余人的序号。"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"提示:\n",
"程序中用split()函数把用空格分隔的输入切分为包含2个值的列表,map()函数将这个列表中的所有值映射为参数指定的类型,此处为整数。\n",
"再用多变量赋值的方法将这两个整数值分别赋值给n和m。\n",
"约瑟夫环中的元素可以用range产生并用list()函数转为列表"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"n, m = map(int, input().split()) # 同一行内用空格分隔的输入切分为列表,并映射为整数分别赋值给n,m \n",
"joseph_ring = list(range(1, n + 1)) # 构造一个元素 1 到 n 的列表\n",
"# 补充你的代码\n",
"\n",
"\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"测试用例:\n",
"输入:\n",
"41 3\n",
"输出:\n",
"[16, 31]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}