假設(shè)您有兩個(gè)列表:x = [Jack, John, Suzie, Bill, James]y = [93, 88, 100, 72, 82]我編寫了一個(gè)代碼來平均 y 中的數(shù)字,并刪除低于該平均值的所有數(shù)字。有沒有辦法也刪除列表 x 中的相應(yīng)名稱?那么,如果 88、72 和 82 從 y 中刪除,我如何使用它從列表 x 中刪除 John、Bill 和 James?這是我當(dāng)前代碼的片段: newName = nameList + listName newGrade = gradeList + listGrade print(newName) print(newGrade) avg = getAverage(newGrade) print('The average of the list is:', avg) gradeAvg = [i for i in newGrade if i > avg] nameAvg = 我已經(jīng)隔離了gradeAvg 中的所有元素,并且需要從nameAvg 中取出那些相同的元素。
3 回答

汪汪一只貓
TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊
您也可以嘗試使用字典。
嘗試查看此https://docs.python.org/3/tutorial/datastructures.html#dictionaries
但是如果您需要處理單獨(dú)的列表,您可以比較它們的索引。即獲取列表 y 中 88 的索引,并從 x 中刪除相同索引處的值。
檢查此以查找索引查找列表中項(xiàng)目的索引

慕田峪7331174
TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超13個(gè)贊
為此,只需將已刪除值的索引保留在 y 中,然后刪除 x 中相同索引處的值。像這樣:
x = ['Bill' , 'Bob', 'Jane', 'Karen']
y = [12, 24, 19, 45]
for element in enumerate(y):
if element[1] < your_value:
del y[element[0]]
del x[element[0]]
添加回答
舉報(bào)
0/150
提交
取消