Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.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 3.6中一个条件的多个列表_Python_List - Fatal编程技术网

创建满足Python 3.6中一个条件的多个列表

创建满足Python 3.6中一个条件的多个列表,python,list,Python,List,我有一个一流的城市: class City(object): #A city has two coordinates in a graph(x,y), and a set of order def __init__(self, name, x, y, orders): self.name = name self.x = x self.y = y self.orders = orders 我创建了一个城市列表: 例如

我有一个一流的城市:

class City(object):
    #A city has two coordinates in a graph(x,y), and a set of order
    def __init__(self, name, x, y, orders):
        self.name = name
        self.x = x
        self.y = y
        self.orders = orders
我创建了一个城市列表:

例如:

list_cities = [City1, City2, City3, City4]

City1 (0.0, 0.0) [1, 2]
City2 (1.0, 0.0) [2, 3]
City3 (2.0, 0.0) [1, 3]
City4 (3.0, 0.0) [2]
我想按顺序对城市进行分组,以便如下所示:

list1 = [City1, City3]
list2 = [City1, City2, City4]
list3 = [City2, City3]
tours = [[],[],[]]
我试过使用字典、列表和集合,但它仍然不能给我结果,我不知道如何才能得到它。 我给你留下一个我尝试过的代码:

tours = []
city = City.create(arrayCity)

for i in range(0,len(city.orders)):
    tours[city.orders[i]].append(city)
这将产生:

IndexError: list index out of range
我希望你能帮助我。
非常感谢

您将获得一个
索引器
,因为您试图访问未填充列表中的项目。我想你正在尝试填写更像这样的内容:

list1 = [City1, City3]
list2 = [City1, City2, City4]
list3 = [City2, City3]
tours = [[],[],[]]
这里还有一些其他问题。您正在通过从
1
开始的整数进行索引,这似乎是从您关于每个城市如何设置的示例中得到的。您需要调整偏移量。您不需要迭代城市的索引。订单实际上只需要值

下面的代码需要硬编码您需要填写的列表数量。如果知道它很容易,但如果它能改变就很烦人。下面的代码假设您的示例中有3次旅行

tours = [[],[],[]]
city = City.create(arrayCity)

for i in city.orders:
    tours[i-1].append(city)
理想情况下,您可能需要一个更具动态性的设置,您不必事先指定组数:

tours = []
city = City.create(arrayCity)

for i in city.orders:
    while len(tours) < i:
        tours.append([])
    tours[i-1].append(city)
tours=[]
城市=城市。创建(arrayCity)
对于我在城市里。订单:
而len(tours)

上面的代码引入了一个while循环,如果您还没有创建要填写的子列表,它会将列表添加到
tours

如果您希望
列表1
包含在
顺序中有
1
的城市,您可以进行测试并采取措施:

if 1 in city.order:
    list1.append(city)
同样地

if 2 in city.order:
    list2.append(city)
if 3 in city.order:
    list3.append(city)

什么是
city.orders
?错误是因为您试图将一个元素附加到某个尚不存在的东西上。首先,您需要创建列表列表,然后再执行for循环以进行追加。是的,但是对于另一个不同的城市列表,我不知道列表的大小。显示您需要多少组?或者您有未知的组?也不能有索引未排序的列表。我建议你使用字典。如果你愿意,你可以使用。是的,我们也可以这样做。