Discuss / Python / 交作业

### 交作业

Topic source

#### 杨飞wb

#1 Created at ... [Delete] [Delete and Lock User]
``````第一题
# -*- coding: utf-8 -*-

L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]

def by_name(t):
a = []
for i in t:
a.append(str(i))
return a

L2 = sorted(L, key=by_name)
print(L2)

# -*- coding: utf-8 -*-

L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]
def by_score(t):
a = []
for i in t:
a.append(str(i))
return a[::-1]

L2 = sorted(L, key=by_score, reverse=True)
print(L2)
``````

#### 楚云

#2 Created at ... [Delete] [Delete and Lock User]

#### wongdir

#3 Created at ... [Delete] [Delete and Lock User]

@楚云 L = [('Bob', 75), ('Adam', 92), ('Bart', 66), ('Lisa', 88)]

'''声明一个by_name的函数，参数为t''' def by_name(t): '''声明一个变量名为a的list空数据''' a = [] '''for遍历传参t''' for i in t: '''把t遍历的元素一个个装进list变量a中，并以str类型''' a.append(str(i)) '''返回list变量a''' return a '''L2等于，把L内的每一个元素以by_name函数返回的a变量也就是名子list为排序规则元素进行排序，并返回排序后的L''' L2 = sorted(L, key=by_name) print(L2)

#### wongdir

#4 Created at ... [Delete] [Delete and Lock User]

'''上面by_naem函数返回的a变量是每一个元素作为字符串整体的list

["('Bob', 75)", "('Adam', 92)", "('Bart', 66)", "('Lisa', 88)"]

sort:('A->('B->('B->('L

'''

#### 其实我对你很在乎的

#5 Created at ... [Delete] [Delete and Lock User]

#### 帛子先生

#6 Created at ... [Delete] [Delete and Lock User]

• 1