Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 列表索引超出范围,使用排序索引(key=lambda)_Python_Sorting_Lambda_Text Files - Fatal编程技术网

Python 列表索引超出范围,使用排序索引(key=lambda)

Python 列表索引超出范围,使用排序索引(key=lambda),python,sorting,lambda,text-files,Python,Sorting,Lambda,Text Files,所以我从一个文件中读取,我想按cy和cx元素排序。我从中读取的文本文件看起来像这样 <circle r="36" class="dot" cx="2150" cy="750" fill="#abae3b" style="fill: rgb(0, 82, 72);"></circle> <circle r="36" class=&

所以我从一个文件中读取,我想按cy和cx元素排序。我从中读取的文本文件看起来像这样

<circle r="36" class="dot" cx="2150" cy="750" fill="#abae3b" style="fill: rgb(0, 82, 72);"></circle>
<circle r="36" class="dot" cx="2950" cy="1350" fill="#e1e25b" style="fill: rgb(0, 82, 72);"></circle>
<circle r="36" class="dot" cx="2250" cy="2250" fill="#babd44" style="fill: rgb(0, 82, 72);"></circle>
<circle r="36" class="dot" cx="550" cy="1750" fill="#5a610d" style="fill: rgb(0, 82, 72);"></circle>
<circle r="36" class="dot" cx="950" cy="1550" fill="#70761a" style="fill: rgb(0, 82, 72);"></circle>
<circle r="36" class="dot" cx="650" cy="1950" fill="#606610" style="fill: rgb(195, 252, 241);"></circle>
<circle r="36" class="dot" cx="1350" cy="1050" fill="#858a26" style="fill: rgb(0, 82, 72);"></circle>
运行此代码时,我遇到以下错误:

Traceback (most recent call last):
  File "file.py", line 7, in <module>
    data.sort(key=lambda n: (n.split()[4],n.split()[3]))
  File "file.py", line 7, in <lambda>
    data.sort(key=lambda n: (n.split()[4],n.split()[3]))
IndexError: list index out of range
回溯(最近一次呼叫最后一次):
文件“File.py”,第7行,在
data.sort(key=lambda n:(n.split()[4],n.split()[3]))
文件“File.py”,第7行,在
data.sort(key=lambda n:(n.split()[4],n.split()[3]))
索引器:列表索引超出范围
当线被分割时,它应该看起来像这样,所以我不知道为什么索引超出范围

['<circle', 'r="36"', 'class="dot"', 'cx="450"', 'cy="1850"', 'fill="#555c0a"', 'style="fill:', 
'rgb(0,', '82,', '72);"></circle>']
[“”]

感谢您的帮助,谢谢

一,。您很可能在文件2的末尾有一个空行。看起来您应该使用一个html解析器,比如beautiful Soupy。当我运行它时,您的代码运行得很好。为什么文件名周围有r“…”?它不是正则表达式。我也同意@deepspace,这不是你问的问题,但是你可以简化你的代码:
data=datefile.readlines()
@FrankYellin在
r
中,不是一个普通的expression@DeepSpace. 嗯。谢谢我唯一一次看到它们被使用是为什么使用正则表达式,我把它和另一种语言混淆了。还是没必要。
['<circle', 'r="36"', 'class="dot"', 'cx="450"', 'cy="1850"', 'fill="#555c0a"', 'style="fill:', 
'rgb(0,', '82,', '72);"></circle>']