リストを並べ替えようとしていますが、最初にdocument
としてからstartPos
をソートしようとしています。 これは私のコードの抽出物が2つの要素でリストをソートしたいが、範囲外になっている
#!/usr/bin/env python
if tokens[0] == tokens2[0]:
document = tokens[1]
startPos = tokens[2]
endPos = tokens[3]
s = []
if document not in s:
s.append(document)
if startPos not in s:
s.append(startPos)
if endPos not in s:
s.append(endPos)
li = s
sorted(li, key = operator.itemgetter(0,1))
print >> fmatches, li
私は理想的
File "./match.py", line 48, in <module>
sorted(li, key = operator.itemgetter(0,1))
IndexError: string index out of range
を取得していますが、私は持っていると思います。私が間違ってやっている何
['source-document01211.txt', '4842', '4851']
['source-document01222.txt', '3162', '3171']
['source-document01222.txt', '20802', '20811']
['source-document01229.txt', '32586', '32595']
['source-document01245.txt', '8670', '8679']
?
'sorted()'はインプレースで動作しないので、その行は役に立たないものです。 – TigerhawkT3
それはどういう意味ですか? – robins02
** sorted **は新しいオブジェクトを返し、引数として与えたオブジェクトをソートします。使用しようとしているパラメータは**ソート**のためのものです。 ** li.sort(...)**を代わりに使用することもできます。 – Prune