给出一组学生的学号姓名信息。请提取并输出所有的学号信息(学号长度固定8位)。
时间: 2024-03-09 20:51:13 浏览: 30
好的,根据您的要求,以下是提取学号信息的Python代码:
```python
students = [
{'学号': '20210001', '姓名': '张三'},
{'学号': '20210002', '姓名': '李四'},
{'学号': '20210003', '姓名': '王五'},
{'学号': '20210004', '姓名': '赵六'},
{'学号': '20210005', '姓名': '钱七'}
]
for student in students:
student_id = student['学号']
if len(student_id) == 8:
print(student_id)
```
输出结果为:
```
20210001
20210002
20210003
20210004
20210005
```
以上代码中,我们首先定义了一个学生信息列表,每个学生信息是一个字典,包含学号和姓名两个属性。然后通过for循环遍历每个学生信息,提取学号信息并检查其长度是否为8位,如果是则输出该学号。
相关问题
【问题描述】给出一组学生的学号姓名信息。请提取并输出所有的学号信息(学号长度固定8位)。 【输入形式】一行字符。包括一个或多个学生的学号姓名,以空格分隔(同一个学生的学号和姓名之间没有空格)。例如:20200001张三 20200002李四 20200003王麻子
【Python代码】
```python
str = input() # 输入学生学号姓名信息
res = [] # 定义列表res存储学号信息
for i in range(len(str)):
if str[i:i+8].isdigit() and len(str[i:i+8]) == 8: # 判断是否为8位数字
res.append(str[i:i+8]) # 若是,则添加到res中
for item in res:
print(item) # 输出所有的学号信息
```
【代码说明】
首先,我们通过`input()`函数获取学生学号姓名信息,保存在字符串`str`中。接着,我们定义一个空列表`res`,用于存储学号信息。
然后,我们使用一个`for`循环遍历字符串`str`中的每一个字符。对于每个字符,我们判断它是否为8位数字。如果是,则将它添加到`res`列表中。
最后,我们使用另一个`for`循环遍历`res`列表中的每个元素,并输出它们,即为所有的学号信息。
用汇编语言 从键盘输入一个长度不超过30的字符串,其中包含自己的班级名称+学号+姓名的拼音(班级和姓名的首字母大写),以“$”符号结束,之后完成以下任务(要求使用子程序): 屏幕显示输出该字符串; 将该字符串的奇数位置字符提取出来,组合成一个字符串显示输出。 将该字符串中的所有数字字符剔除,然后显示输出; 将该字符串中的英文大写字母变成小写字母,小写字母变成大写字母,显示输出; 统计该字符串中的大写字母、小写字母和数字的个数,保存在内存变量中,并在屏幕上显示; 将该字符串中的数字进行累加,将累加结果显示输出。
以下是汇编语言的代码实现:
```assembly
; 定义常量
MAX_LEN equ 30 ; 最大长度
CR equ 0dh ; 回车符
LF equ 0ah ; 换行符
; 定义变量
input_str db MAX_LEN+1 dup('$') ; 用于存储输入的字符串
output_str db MAX_LEN/2+1 dup('$') ; 用于存储奇数位置字符组成的字符串
count_upper db 0 ; 大写字母个数
count_lower db 0 ; 小写字母个数
count_digit db 0 ; 数字个数
sum db 0 ; 数字累加和
; 主程序
main:
call input_str ; 输入字符串
call output_str_all ; 输出原字符串
call output_str_odd ; 输出奇数位置字符组成的字符串
call remove_digit ; 剔除数字
call convert_case ; 大小写转换
call output_counts ; 统计并输出个数
call sum_digit ; 计算数字累加和
call output_sum ; 输出数字累加和
; 程序结束
mov ah, 4ch
int 21h
; 子程序:输入字符串
input_str:
; 重置输入计数器
mov cx, 0
; 显示提示信息
mov ah, 9
lea dx, msg_input
int 21h
; 循环读取字符
mov ah, 1
read_char:
int 21h
; 判断字符是否为回车符或长度超限
cmp al, CR
je end_input
cmp cx, MAX_LEN
je end_input
; 存储字符
mov [input_str+cx], al
inc cx
jmp read_char
end_input:
; 将结束符替换为$符号
mov byte ptr [input_str+cx], '$'
; 换行
mov ah, 2
mov dl, CR
int 21h
mov dl, LF
int 21h
ret
; 子程序:输出原字符串
output_str_all:
; 显示提示信息
mov ah, 9
lea dx, msg_output_all
int 21h
; 显示字符串
mov ah, 9
lea dx, input_str
int 21h
; 换行
mov ah, 2
mov dl, CR
int 21h
mov dl, LF
int 21h
ret
; 子程序:输出奇数位置字符组成的字符串
output_str_odd:
; 显示提示信息
mov ah, 9
lea dx, msg_output_odd
int 21h
; 提取奇数位置字符
mov si, 0 ; si为源字符串下标
mov di, 0 ; di为目标字符串下标
loop_odd:
mov al, [input_str+si]
cmp al, '$'
je end_loop_odd
inc si
mov ah, [si]
cmp ah, '$'
je end_loop_odd
mov [output_str+di], ah
inc di
jmp loop_odd
end_loop_odd:
; 显示字符串
mov ah, 9
lea dx, output_str
int 21h
; 换行
mov ah, 2
mov dl, CR
int 21h
mov dl, LF
int 21h
ret
; 子程序:剔除数字
remove_digit:
; 显示提示信息
mov ah, 9
lea dx, msg_remove_digit
int 21h
; 剔除数字
mov si, 0 ; si为源字符串下标
mov di, 0 ; di为目标字符串下标
loop_remove:
mov al, [input_str+si]
cmp al, '$'
je end_loop_remove
inc si
cmp al, '0'
jb skip_remove
cmp al, '9'
ja skip_remove
jmp store_char
skip_remove:
inc count_digit
jmp loop_remove
store_char:
mov [input_str+di], al
inc di
jmp loop_remove
end_loop_remove:
; 添加结束符
mov byte ptr [input_str+di], '$'
; 显示字符串
mov ah, 9
lea dx, input_str
int 21h
; 换行
mov ah, 2
mov dl, CR
int 21h
mov dl, LF
int 21h
ret
; 子程序:大小写转换
convert_case:
; 显示提示信息
mov ah, 9
lea dx, msg_convert_case
int 21h
; 转换大小写
mov si, 0 ; si为源字符串下标
loop_case:
mov al, [input_str+si]
cmp al, '$'
je end_loop_case
cmp al, 'a'
jb check_upper
cmp al, 'z'
ja check_upper
sub al, 20h ; 转换为大写字母
jmp store_case
check_upper:
cmp al, 'A'
jb store_case
cmp al, 'Z'
ja store_case
add al, 20h ; 转换为小写字母
store_case:
mov [input_str+si], al
cmp al, 'A'
jb skip_case
cmp al, 'Z'
ja skip_case
inc count_upper
jmp end_case
skip_case:
cmp al, 'a'
jb end_case
cmp al, 'z'
ja end_case
inc count_lower
end_case:
inc si
jmp loop_case
end_loop_case:
; 显示字符串
mov ah, 9
lea dx, input_str
int 21h
; 换行
mov ah, 2
mov dl, CR
int 21h
mov dl, LF
int 21h
ret
; 子程序:统计并输出个数
output_counts:
; 显示提示信息
mov ah, 9
lea dx, msg_output_counts
int 21h
; 显示大写字母个数
mov ah, 9
lea dx, msg_count_upper
int 21h
mov dl, count_upper
add dl, '0'
int 21h
; 换行
mov ah, 2
mov dl, CR
int 21h
mov dl, LF
int 21h
; 显示小写字母个数
mov ah, 9
lea dx, msg_count_lower
int 21h
mov dl, count_lower
add dl, '0'
int 21h
; 换行
mov ah, 2
mov dl, CR
int 21h
mov dl, LF
int 21h
; 显示数字个数
mov ah, 9
lea dx, msg_count_digit
int 21h
mov dl, count_digit
add dl, '0'
int 21h
; 换行
mov ah, 2
mov dl, CR
int 21h
mov dl, LF
int 21h
ret
; 子程序:计算数字累加和
sum_digit:
; 计算数字累加和
mov si, 0 ; si为字符串下标
loop_sum:
mov al, [input_str+si]
cmp al, '$'
je end_loop_sum
cmp al, '0'
jb skip_sum
cmp al, '9'
ja skip_sum
sub al, '0'
add sum, al
skip_sum:
inc si
jmp loop_sum
end_loop_sum:
ret
; 子程序:输出数字累加和
output_sum:
; 显示提示信息
mov ah, 9
lea dx, msg_output_sum
int 21h
; 显示数字累加和
mov ah, 2
mov dl, sum
add dl, '0'
int 21h
; 换行
mov ah, 2
mov dl, CR
int 21h
mov dl, LF
int 21h
ret
; 定义字符串常量
msg_input db '请输入一个长度不超过30的字符串,其中包含自己的班级名称 学号 姓名的拼音(班级和姓名的首字母大写),以“$”符号结束:$'
msg_output_all db '原字符串为:$'
msg_output_odd db '奇数位置字符组成的字符串为:$'
msg_remove_digit db '剔除数字后的字符串为:$'
msg_convert_case db '大小写转换后的字符串为:$'
msg_output_counts db '大写字母、小写字母和数字的个数分别为:$'
msg_count_upper db '大写字母:'
msg_count_lower db '小写字母:'
msg_count_digit db '数字:'
msg_output_sum db '数字累加和为:$'
```
相关推荐
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![xlsx](https://img-home.csdnimg.cn/images/20210720083732.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)