我如何在列表中获取第二个元素并将其追加到不同的列表中?

问题描述 投票:0回答:1

我有一个包含学生ID和分数的列表。我希望只将分数附加到不同的列表中,但每次我试图只打印第二个元素时,它都会给我整行(学生ID和分数)。我应该怎样做呢?

[['10000', '43\n'],
 ['10001', '26\n'],
 ['10002', '43\n'],
 ['10003', '57\n'],
 ['10004', '100\n'],
 ['10005', '57\n'],
 ['10006', '55\n'],
 ['10007', '60\n'],
 ['10008', '37\n'],
 ['10009', '30\n'],
 ['10010', '50\n'],
 ['10011', '17\n'],
 ['10012', '59\n'],
 ['10013', '57\n'],
 ['10014', '27\n'],
 ['10015', '6\n'],
 ['10016', '49\n'],
 ['10017', '30\n'],
 ['10018', '57\n'],
 ['10019', '56\n']]
python list append element
1个回答
-1
投票
def get_ids(lists):
    score_list = list()
    for lis in lists:
        score = lis[0]
        score_list.append(score)
    return score_list


list_of_lists = [
    ["10000", "43\n"],
    ["10001", "26\n"],
    ["10002", "43\n"],
    ["10003", "57\n"],
    ["10004", "100\n"],
    ["10005", "57\n"],
    ["10006", "55\n"],
    ["10007", "60\n"],
    ["10008", "37\n"],
    ["10009", "30\n"],
    ["10010", "50\n"],
    ["10011", "17\n"],
    ["10012", "59\n"],
    ["10013", "57\n"],
    ["10014", "27\n"],
    ["10015", "6\n"],
    ["10016", "49\n"],
    ["10017", "30\n"],
    ["10018", "57\n"],
    ["10019", "56\n"],
]

print(get_ids(list_of_lists))

做了什么。

  1. 创建一个函数来获取id列表。
  2. 用逗号分割列表。
© www.soinside.com 2019 - 2024. All rights reserved.