You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按第2列对列表数据排序并导出至.csv文件?

按第二列(品牌)排序产品数据的Python解决方案

看起来你在尝试按品牌列(第2列)排序产品数据时遇到了逻辑和语法问题,我来帮你梳理清楚问题所在并给出可行的解决方案。

你的原始数据与现有代码

首先,先明确你的原始产品数据(方便后续参考):

import datetime

product_data = [
    [1167234, 'Apple ', 'phone', 534, datetime.datetime(2022, 2, 1, 0, 0), ''],
    [2390112, 'Dell', 'laptop', 799, datetime.datetime(2022, 7, 2, 0, 0), ''],
    [9034210, 'Dell', 'tower', 345, datetime.datetime(2022, 5, 27, 0, 0), ''],
    [7346234, 'Lenovo', 'laptop', 239, datetime.datetime(2022, 9, 1, 0, 0), 'damaged'],
    [3001265, 'Samsung', 'phone', 1200, datetime.datetime(2023, 12, 1, 0, 0), ''],
    [2347800, 'Apple ', 'laptop', 999, datetime.datetime(2022, 7, 3, 0, 0), ''],
    [1009453, 'Lenovo', 'tower', 599, datetime.datetime(2023, 10, 1, 0, 0), '']
]

你尝试的代码存在两个核心问题:

  • 你在循环单个ID时,把单条数据转成了字符串x = str(list(...)),然后尝试对字符串排序——这会按字符顺序排序,完全不符合你的需求。
  • 排序操作应该在所有数据收集完成后统一执行,而不是在循环里逐个处理单条数据。

正确的解决方案

步骤1:组装完整数据集(如果用字典存储数据)

如果你的数据是分散在ManDict、typeDict等字典中的,先把所有数据组装成一个统一的列表:

# 假设你的字典定义如下(根据你的代码推断)
ID = [1167234, 2390112, 9034210, 7346234, 3001265, 2347800, 1009453]
ManDict = {1167234:'Apple ', 2390112:'Dell', 9034210:'Dell', 7346234:'Lenovo', 3001265:'Samsung', 2347800:'Apple ', 1009453:'Lenovo'}
typeDict = {1167234:'phone', 2390112:'laptop', 9034210:'tower', 7346234:'laptop', 3001265:'phone', 2347800:'laptop', 1009453:'tower'}
PriceDict = {1167234:534, 2390112:799, 9034210:345, 7346234:239, 3001265:1200, 2347800:999, 1009453:599}
ServiceDatesDict = {1167234:datetime.datetime(2022,2,1,0,0), 2390112:datetime.datetime(2022,7,2,0,0), 9034210:datetime.datetime(2022,5,27,0,0), 7346234:datetime.datetime(2022,9,1,0,0), 3001265:datetime.datetime(2023,12,1,0,0), 2347800:datetime.datetime(2022,7,3,0,0), 1009453:datetime.datetime(2023,10,1,0,0)}
DamagedDict = {1167234:'', 2390112:'', 9034210:'', 7346234:'damaged', 3001265:'', 2347800:'', 1009453:''}

# 组装成统一的产品列表
product_data = []
for ids in ID:
    product = [
        ids,
        ManDict.get(ids),
        typeDict.get(ids),
        PriceDict.get(ids),
        ServiceDatesDict.get(ids),
        DamagedDict.get(ids)
    ]
    product_data.append(product)

步骤2:按第2列(品牌)排序

使用sorted()函数,指定key=lambda x: x[1].strip()——这里的.strip()是为了去除'Apple '末尾的多余空格,避免排序时出现分类错误:

# 按品牌列升序排序(A-Z)
sorted_products = sorted(product_data, key=lambda x: x[1].strip())

# 如果需要降序排序(Z-A),添加reverse=True参数
# sorted_products = sorted(product_data, key=lambda x: x[1].strip(), reverse=True)

步骤3:写入文件

遍历排序后的列表,将内容写入目标文件:

with open('output.txt', 'w') as yikes:
    for item in sorted_products:
        yikes.write(str(item) + '\n')

完整可运行代码

把以上步骤整合起来,就是完整的解决方案:

import datetime

# 定义字典数据
ID = [1167234, 2390112, 9034210, 7346234, 3001265, 2347800, 1009453]
ManDict = {1167234:'Apple ', 2390112:'Dell', 9034210:'Dell', 7346234:'Lenovo', 3001265:'Samsung', 2347800:'Apple ', 1009453:'Lenovo'}
typeDict = {1167234:'phone', 2390112:'laptop', 9034210:'tower', 7346234:'laptop', 3001265:'phone', 2347800:'laptop', 1009453:'tower'}
PriceDict = {1167234:534, 2390112:799, 9034210:345, 7346234:239, 3001265:1200, 2347800:999, 1009453:599}
ServiceDatesDict = {1167234:datetime.datetime(2022,2,1,0,0), 2390112:datetime.datetime(2022,7,2,0,0), 9034210:datetime.datetime(2022,5,27,0,0), 7346234:datetime.datetime(2022,9,1,0,0), 3001265:datetime.datetime(2023,12,1,0,0), 2347800:datetime.datetime(2022,7,3,0,0), 1009453:datetime.datetime(2023,10,1,0,0)}
DamagedDict = {1167234:'', 2390112:'', 9034210:'', 7346234:'damaged', 3001265:'', 2347800:'', 1009453:''}

# 组装数据集
product_data = []
for ids in ID:
    product = [
        ids,
        ManDict.get(ids),
        typeDict.get(ids),
        PriceDict.get(ids),
        ServiceDatesDict.get(ids),
        DamagedDict.get(ids)
    ]
    product_data.append(product)

# 按品牌列排序
sorted_products = sorted(product_data, key=lambda x: x[1].strip())

# 写入文件
with open('output.txt', 'w') as yikes:
    for item in sorted_products:
        yikes.write(str(item) + '\n')

内容的提问来源于stack exchange,提问作者AMedium Ramen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 23:18:11