擅长:python、mysql、java
<p>这是一个按字母顺序递增姓名的示例,随机生成学生的姓名和分数</p>
<pre><code>import string
import random
li = []
print("")
N = int(input("Enter The Number of Students in the class : "))
print("")
def getRandomList():
for i in range (N):
newName = ''.join(random.choices(string.ascii_uppercase,k=3))
name=str(newName)
mark = random.randint(0, 100)
li.append((name, mark))
sortedbyname = sorted(li, key=lambda tp: tp[0])
print("Print the names and their marks sorted by name alphabetically")
print("")
print('{:5s} {:15s} ' .format('Name','Marks'))
print("")
for i in range(N):
print(f"{sortedbyname[i][0]} {sortedbyname[i][1]}")
print("")
getRandomList()
</code></pre>
<p>样本随机列表的输出如下所示</p>
<pre><code>Enter The Number of Students in the class: 10
Print the names and their marks sorted by name alphabetically
Name Marks
CFN 90
DIL 96
MIA 26
OIG 35
SNH 11
TBU 45
THW 3
VIR 26
XYS 62
YIU 9
</code></pre>