我在使用python数组列表时遇到了一些麻烦。
我知道,python列表应保持其顺序,但我有脚本,这不是真的,我无法确定原因,所以我认为你可以帮助我。
有一些更复杂的脚本,即从CSV文件中获取2行,使用权重获取第一行,命令它们,然后根据第一行重新排序第二行:
csv_cleared = csv.reader(open(temp_csv_file), delimiter= ';')
vahy = csv_cleared.next()
vahy_ordered = sorted(vahy, key=int, reverse=True) # orders line
vahy_ordered_indexes = []
for vaha in vahy:
index_vahy = vahy_ordered.index(vaha)
if index_vahy in vahy_ordered_indexes: # if two mats have same weight
index_vahy = index_vahy+1 # increment index by one
vahy_ordered_indexes.append(index_vahy)
else:
vahy_ordered_indexes.append(index_vahy)
日志:
Vahy: ['49', '3', '19', '8', '19', '39', '11']
Vahy ordered: ['49', '39', '19', '19', '11', '8', '3']
Vaha is: 49 Index_vahy is: 0
Inserted index_vahy: 0 into vahy_ordered_indexes([0])
Vaha is: 3 Index_vahy is: 6
Inserted index_vahy: 6 into vahy_ordered_indexes([0, 6])
Vaha is: 19 Index_vahy is: 2
Inserted index_vahy: 2 into vahy_ordered_indexes([0, 6, 2])
Vaha is: 8 Index_vahy is: 5
Inserted index_vahy: 5 into vahy_ordered_indexes([0, 6, 2, 5])
Vaha is: 19 Index_vahy is: 2
Inserted index_vahy: 3 into vahy_ordered_indexes([0, 6, 2, 5, 3]) DOUBLED!!!
Vaha is: 39 Index_vahy is: 1
Inserted index_vahy: 1 into vahy_ordered_indexes([0, 6, 2, 5, 3, 1])
Vaha is: 11 Index_vahy is: 4
Inserted index_vahy: 4 into vahy_ordered_indexes([0, 6, 2, 5, 3, 1, 4])
这样可以正常工作,但问题会在以后出现:
new_line = line[:3]
line_materials = line[3:-1]
line_material = 0
for poradovnik in vahy_ordered_indexes:
poradie = poradovnik + 3
print "Poradie is: %s New line before insert is: %s"% (poradie, new_line)
print "Inserting material: %s into position: %s" % (line_materials[line_material], poradie)
new_line.insert(poradie, line_materials[line_material])
print "New line after insert is: %s \n"% (new_line)
line_material += 1
其中new_line.insert()函数插入带有正确索引的项目,但它们的位置不正确,如log:
Poradie is: 3 New line before insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.']
Inserting material: TESCO 5K into position: 3
New line after insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K']
Poradie is: 9 New line before insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K']
Inserting material: TESCO expres into position: 9
New line after insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'TESCO expres']
Poradie is: 5 New line before insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'TESCO expres']
Inserting material: BILLA hlavny into position: 5
New line after insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'TESCO expres', 'BILLA hlavny']
Poradie is: 8 New line before insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'TESCO expres', 'BILLA hlavny']
Inserting material: Ptacek into position: 8
New line after insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'TESCO expres', 'BILLA hlavny', 'Ptacek']
Poradie is: 6 New line before insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'TESCO expres', 'BILLA hlavny', 'Ptacek']
Inserting material: Merkury Market SK into position: 6
New line after insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'TESCO expres', 'BILLA hlavny', 'Merkury Market SK', 'Ptacek']
Poradie is: 4 New line before insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'TESCO expres', 'BILLA hlavny', 'Merkury Market SK', 'Ptacek']
Inserting material: NAY elektrodom into position: 4
New line after insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'NAY elektrodom', 'TESCO expres', 'BILLA hlavny', 'Merkury Market SK', 'Ptacek']
Poradie is: 7 New line before insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'NAY elektrodom', 'TESCO expres', 'BILLA hlavny', 'Merkury Market SK', 'Ptacek']
Inserting material: TOP Farma into position: 7
New line after insert is: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'NAY elektrodom', 'TESCO expres', 'BILLA hlavny', 'TOP Farma', 'Merkury Market SK', 'Ptacek']
似乎在某些情况下,物品被插入正确的位置,而其他情况则没有,但是我被困了几天已经没有找到它何时插入正确的位置而不是
答案 0 :(得分:3)
您无法在列表边界外插入项目,例如:
>>> l = [1, 2, 3]
>>> l.insert(10, 10)
>>> l
[1, 2, 3, 10]
将10
插入到3
的位置,而不是10
。
答案 1 :(得分:0)
正如安东所说,问题是你要在列表边界之外插入项目。有很多方法可以解决这个问题。这是一种可能的解决方案,将原始索引保持在有序的集合列表中:
vahy = ['49','3','19','8','19','39','11'] indexed_vahy = [(x,y) for x,y in enumerate(vahy)] ordered_vahy = sorted(indexed_vahy, key=lambda x: int(x[1]), reverse=True) new_line = ['Kod regionu', 'Nazov regionu', 'Platova kat.'] line_materials = ['TESCO 5K','TESCO expres', 'BILLA hlavny', 'Ptacek', 'Merkury Market', 'NAY elektrodom', 'TOP Farma'] output = new_line for index, weight in ordered_vahy: output.apped(line_material[index])
这就是日志:
Vahy: ['49','3','19','8','19','39','11'] Indexed vahy: [(0, '49'), (1, '3'), (2, '19'), (3, '8'), (4, '19'), (5, '39'), (6, '11')] Ordered vahy: [(0, '49'), (5, '39'), (2, '19'), (4, '19'), (6, '11'), (3, '8'), (1, '3')] Output: ['Kod regionu', 'Nazov regionu', 'Platova kat.', 'TESCO 5K', 'NAY elektrodom', 'BILLA hlavny', 'Merkury Market', 'TOP Farma', 'Ptacek', 'TESCO expres']