记录两个index之后交换他们的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3

import sys
from collections import defaultdict

for line in sys.stdin:
line = line.strip()
if not line:
continue

line_list = line.split(" ")

# key:value index:digit
numbers=defaultdict(int)
for index,word in enumerate(line_list):
if word.isdigit():
numbers[index] = word
# 现在numbers里面就是index:digit的键值对了

# 先判断找到了数字了吗
if not numbers:
print(line)
continue

# 然后交换最大的index和最小的index的值放回line
max_index = max(numbers.keys())
min_index = min(numbers.keys())

line_list[max_index],line_list[min_index] = line_list[min_index],line_list[max_index]
print(" ".join(line_list))

截取后对着学号计数就可以了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python3

import sys
from collections import defaultdict

dic = defaultdict(int)

for line in sys.stdin:
line = line.strip()
if not line:
continue
field = line.split("|")
id_and_name = field[1:3]

dic[field[1]] += 1

ans = []
for key in dic:
if dic[key] == 2:
ans.append(key)

ans.sort()
for id in ans:
print(id)
0%