同我有城市的列表,每個城市都有一個名字,一個真或假的值,然後用它連接到其他城市的名單。我如何在Python中編寫函數來表示True,如果所有的城市都是True且False不是全部都是True?確定是否所有項目都在Python
下面是我的城市作了:
def set_up_cities(names=['City 0', 'City 1', 'City 2', 'City 3', 'City 4', 'City 5', 'City 6', 'City 7', 'City 8', 'City 9', 'City 10', 'City 11', 'City 12', 'City 13', 'City 14', 'City 15']):
"""
Set up a collection of cities (world) for our simulator.
Each city is a 3 element list, and our world will be a list of cities.
:param names: A list with the names of the cities in the world.
:return: a list of cities
"""
# Make an adjacency matrix describing how all the cities are connected.
con = make_connections(len(names))
# Add each city to the list
city_list = []
for n in enumerate(names):
city_list += [ make_city(n[1],con[n[0]]) ]
return city_list
嘗試[了'所有()'函數(http://docs.python.org/3/library/functions.html#all)。 – Ryan
我不會用一個列表作爲默認參數,肯定不會這麼長的一個。 –