Python中的zip函数

0 投票
2 回答
947 浏览
提问于 2025-04-17 10:11

在Python中,我有:

    nameh.append([area, hitratio])
    nameh_sorted = sorted (nameh, key=lambda nameh: nameh[0])

我根据区域名称的字母顺序对[area, hitratio]进行了排序。然后我想把排序后的“hitratio”和另一个列表“u”结合成[u1, hitratio1]、[u2, hitratio2]……但是我不知道在这种情况下怎么选择排序后的hitratio,*nameh_sorted[1]显然是错的……

user = zip (u,*nameh_sorted[1])
user_sorted = sorted (user, key=lambda user: user[0])
x5, y5 = zip(*user_sorted) 

有人能帮忙吗?非常感谢

2 个回答

0

试试这个:

user = zip(u, [x[1] for x in nameh_sorted])

这就是你想要的吗?

2
user = zip(u, (hitratio for area, hitratio in nameh_sorted))

当然可以!请把你想要翻译的内容发给我,我会帮你用简单易懂的语言解释清楚。

撰写回答