采用python,生成15个城市坐标,按照x坐标的大小,由大到小,若x相同,则按照y坐标大小,从大到小输出坐标代表的城市
时间: 2023-05-30 19:06:07 浏览: 68
python中tkinter窗口位置\坐标\大小等实现示例
代码如下:
```python
import random
# 生成15个城市坐标,x和y坐标均为整数
cities = [(random.randint(0, 100), random.randint(0, 100)) for i in range(15)]
# 按照x坐标的大小,由大到小排序,若x相同,则按照y坐标大小,从大到小排序
cities = sorted(cities, key=lambda city: (-city[0], -city[1]))
# 输出排序后的城市坐标
for i, city in enumerate(cities):
print(f"City {i+1}: ({city[0]}, {city[1]})")
```
输出结果类似如下:
```
City 1: (97, 95)
City 2: (95, 44)
City 3: (94, 63)
City 4: (93, 41)
City 5: (92, 20)
City 6: (77, 63)
City 7: (61, 69)
City 8: (51, 98)
City 9: (46, 55)
City 10: (44, 51)
City 11: (38, 92)
City 12: (30, 99)
City 13: (25, 38)
City 14: (24, 87)
City 15: (18, 97)
```
阅读全文