我正在编写一些代码来创建一个未排序的列表,但每当我尝试使用insert方法插入一个列表时,我得到的'方法'对象不是可订阅的错误.不知道如何解决它.谢谢.
class UnsortedList:
def __init__(self):
self.theList = list()
def __getitem__(self, i):
print(self.theList[i])
def insert(self, lst):
for x in lst:
try:
self.theList.append(float(x))
except:
print("oops")
myList = UnsortedList()
myList.insert[1, 2, 3]
Run Code Online (Sandbox Code Playgroud) class City:
def __init__(self, string):
self._string = string.split(',')
self._name = self._string[0]
self._state = self._string[1]
self._latitude = self._string[2]
self._longitude = self._string[3]
self._location = [self._latitude, self._longitude]
def name(self):
return self._name
def location(self):
return self._location
self.hand.sort(key=lambda x: x.longitude)
def __lt__(self, other):
if self._longitude < other._longitude:
return True
if self._longitude > other._longitude:
return False
if self._longitude == other._longitude:
if self._latitude < other._latitude:
return True
if self._latitude > other._latitude:
return False
citystrings = ["Charleston,WV,38.35,81.63",
"Charlotte,NC,35.23,80.83",
"Cheyenne,WY,41.15,104.87",
"Chicago,IL,41.83,87.62",
"Cincinnati,OH,39.13,84.50",
"Cleveland,OH,41.47,81.62",
"Columbia,SC,34.00,81.03",
"Columbus,OH,40.00,83.02",
"Dallas,TX,32.77,96.77",
"Denver,CO,39.75,105.00"]
westtoeastnames = …Run Code Online (Sandbox Code Playgroud)