问题排序列表,列表乱序
Issues Sorting a list, list out of order
嗨,我正在尝试使用 python 和 pytq5 编写一个基本的 GUI,它显示从 API 调用接收到的信息,当信息从 API 它返回时似乎出了问题,我不知道如何在将数据发送到 GUI 之前对数据进行排序,该列表是字典列表,
interfaces = [
{'name': 'GigabitEthernet 0/0'},
{'name': 'GigabitEthernet 1/0/1'},
{'name': 'GigabitEthernet 1/0/10'},
{'name': 'GigabitEthernet 1/0/11'},
...
]
任何建议将不胜感激,从图像中我希望对数据进行排序,以便 1/0/1 - 1/0/9 都在 1/0/10 之前
谢谢
试试这个:
# Your list of dicts
interfaces = [
{'name': 'GigabitEthernet 0/0'},
{'name': 'GigabitEthernet 1/0/11'},
{'name': 'GigabitEthernet 1/0/10'},
{'name': 'GigabitEthernet 1/0/1'},
{'name': 'GigabitEthernet 1/0/2'}
]
new_interfaces = []
# Create a list of dicts whose value corresponding to the 'name' key would be a list
# containing the ethernet type (in this case, 'GigabitEthernet') and a list of 3
# elements representing the date (like '[1, 0, 11]')
for entry in [i["name"] for i in interfaces]:
new_interfaces.append({'name': [entry.split()[0], entry.split()[1].split("/")]})
# If one of those lists representing the date contains 2 elements, add a third
# element with a value of '0'
for entry in new_interfaces:
if len(entry['name'][1]) == 2:
entry['name'][1].append('0')
# Sort the list according to the date
new_interfaces = sorted(new_interfaces, key=lambda d: int(''.join(d['name'][1])))
# Re-create the original list with sorted values and original structure
interfaces = []
for entry in new_interfaces:
interfaces.append({'name': f"{entry['name'][0]} {'/'.join(entry['name'][1])}"})
print(interfaces)
输出:
[{'name': 'GigabitEthernet 0/0/0'}, {'name': 'GigabitEthernet 1/0/1'}, {'name': 'GigabitEthernet 1/0/2'}, {'name': 'GigabitEthernet 1/0/10'}, {'name': 'GigabitEthernet 1/0/11'}]
此算法将日期(或任何它是 lol)转换为不带 '/' 的字符串,以便它可以转换为整数以便能够正确地相互比较。
示例:
1/0/10 将是 1010
1/0/1 将是 101
并且由于 101 小于 1010 作为数字,sorted()
方法将其放在前面。
嗨,我正在尝试使用 python 和 pytq5 编写一个基本的 GUI,它显示从 API 调用接收到的信息,当信息从 API 它返回时似乎出了问题,我不知道如何在将数据发送到 GUI 之前对数据进行排序,该列表是字典列表,
interfaces = [
{'name': 'GigabitEthernet 0/0'},
{'name': 'GigabitEthernet 1/0/1'},
{'name': 'GigabitEthernet 1/0/10'},
{'name': 'GigabitEthernet 1/0/11'},
...
]
任何建议将不胜感激,从图像中我希望对数据进行排序,以便 1/0/1 - 1/0/9 都在 1/0/10 之前
谢谢
试试这个:
# Your list of dicts
interfaces = [
{'name': 'GigabitEthernet 0/0'},
{'name': 'GigabitEthernet 1/0/11'},
{'name': 'GigabitEthernet 1/0/10'},
{'name': 'GigabitEthernet 1/0/1'},
{'name': 'GigabitEthernet 1/0/2'}
]
new_interfaces = []
# Create a list of dicts whose value corresponding to the 'name' key would be a list
# containing the ethernet type (in this case, 'GigabitEthernet') and a list of 3
# elements representing the date (like '[1, 0, 11]')
for entry in [i["name"] for i in interfaces]:
new_interfaces.append({'name': [entry.split()[0], entry.split()[1].split("/")]})
# If one of those lists representing the date contains 2 elements, add a third
# element with a value of '0'
for entry in new_interfaces:
if len(entry['name'][1]) == 2:
entry['name'][1].append('0')
# Sort the list according to the date
new_interfaces = sorted(new_interfaces, key=lambda d: int(''.join(d['name'][1])))
# Re-create the original list with sorted values and original structure
interfaces = []
for entry in new_interfaces:
interfaces.append({'name': f"{entry['name'][0]} {'/'.join(entry['name'][1])}"})
print(interfaces)
输出:
[{'name': 'GigabitEthernet 0/0/0'}, {'name': 'GigabitEthernet 1/0/1'}, {'name': 'GigabitEthernet 1/0/2'}, {'name': 'GigabitEthernet 1/0/10'}, {'name': 'GigabitEthernet 1/0/11'}]
此算法将日期(或任何它是 lol)转换为不带 '/' 的字符串,以便它可以转换为整数以便能够正确地相互比较。
示例:
1/0/10 将是 1010
1/0/1 将是 101
并且由于 101 小于 1010 作为数字,sorted()
方法将其放在前面。