小编Nic*_*ckl的帖子

operator.itemgetter和sort()如何在Python中工作?

我有以下代码:

# initialize
a = []

# create the table (name, age, job)
a.append(["Nick", 30, "Doctor"])
a.append(["John",  8, "Student"])
a.append(["Paul", 22, "Car Dealer"])
a.append(["Mark", 66, "Retired"])    

# sort the table by age
import operator
a.sort(key=operator.itemgetter(1))    

# print the table
print(a)
Run Code Online (Sandbox Code Playgroud)

它创建一个4x3表,然后按年龄对其进行排序.我的问题是,到底是key=operator.itemgetter(1)做什么的?该operator.itemgetter函数是否返回项目的值?为什么我不能只输入那样的东西key=a[x][1]?或者我可以吗?与运营商怎么能打印等形式的一定值3x222

  1. Python如何对表进行排序?我可以反向排序吗?

  2. 如何根据第一个年龄段的两列对其进行排序,然后如果年龄与b名称相同?

  3. 我怎么能不这样做operator

python sorting operator-keyword

53
推荐指数
3
解决办法
12万
查看次数

在FreeBSD中使用一个命令更改密码

作为一个简单的用户,我如何在FreeBSD中用一个命令(一行)更改我自己的密码。我尝试使用,passwd --stdin但这似乎只是一个Linux命令。

passwords freebsd change-password user-accounts

1
推荐指数
1
解决办法
3935
查看次数