基于 Python 中特定索引处的最大值对列表列表进行排序,无需使用预构建的函数或方法

发布于 2025-01-12 11:56:10 字数 1773 浏览 0 评论 0原文

我有一个列表列表,我想学习如何按索引 1 处的元素对列表进行排序,但如果索引 1 对于两个项目返回相同的数字,也如何按索引 2 处的元素排序。

我想在不使用内置函数和方法的情况下执行此操作,以便我可以继续加深对列表以及如何操作它们的理解。

回顾一下:

  • 我有一个列表列表
  • 每个子列表都有相同数量的元素
  • 我试图通过创建一个新的未排序列表来按降序对它们进行排序,该列表是原始列表的副本(我不想修改原始列表)未排序列表)并循环未排序列表的副本以获取最高数字(从索引 1 开始),然后将其附加到新创建的 Sorted_lists 变量中,
  • 然后从原始未排序列表中删除该列表
  • 我重复该过程,直到通过一个具有最高值的剩余列表被添加到新的排序列表中,并从原始列表中删除

我尝试了一些不同的方法,但无法让它工作。任何帮助将不胜感激。

# initialising list
food_list = (
    ["banana", 10, "f", "yellow"],
    ["apple", 12, "f", "red"],
    ["pear", 60, "f", "green"],
    ["mango", 5, "f", "yellow"],
    ["lettuce", 3, "v", "green"],
    ["beans", 20, "v", "green"],
    ["red capsicum", 1, "v", "red"],
    ["corn", 20, "v", "yellow"],
)

unsorted_food_list_copy = food_list
sorted_food_list = []

while len(unsorted_food_list_copy) != 0:
    maximum = 0
    for food in unsorted_food_list_copy:
        if food[1] > maximum:
            maximum = food[1]
    sorted_food_list.append(maximum)
    unsorted_food_list_copy.remove(maximum)

我也尝试过这个:

# initialising list
food_list = (
    ["banana", 10, "f", "yellow"],
    ["apple", 12, "f", "red"],
    ["pear", 60, "f", "green"],
    ["mango", 5, "f", "yellow"],
    ["lettuce", 3, "v", "green"],
    ["beans", 20, "v", "green"],
    ["red capsicum", 1, "v", "red"],
    ["corn", 20, "v", "yellow"],
)

unsorted_food_list_copy = food_list
sorted_food_list = []

while unsorted_food_list_copy:
    min = unsorted_food_list_copy[1]
    for x in unsorted_food_list_copy:
        if x < min:
            min = x
    sorted_food_list.append(min)
    unsorted_food_list_copy.remove(min)

I have a list of lists and I want to learn how to sort the list by the element at index 1 but also sorted by the element at index 2 if index 1 returns the same number for two items.

I want to do this without the use of inbuilt functions and methods so that I can continue to develop my understanding of lists and how to manipulate them.

To recap:

  • I have a list of lists
  • Each sublist has the same number of elements
  • I am trying to sort them in descending order by creating a new unsorted list which is a copy of the original list (I don't want to modify the original unsorted list) and looping over the copy of the unsorted list to grab the highest number in (from index 1) and then appending that to a newly created sorted_lists variable
  • I then remove that list from the original unsorted list
  • I repeat the process until one by one the remaining lists with the highest value is added to the new sorted list and removed from the original list

I have tried a few different things but cannot get it to work. Any help would be appreciated.

# initialising list
food_list = (
    ["banana", 10, "f", "yellow"],
    ["apple", 12, "f", "red"],
    ["pear", 60, "f", "green"],
    ["mango", 5, "f", "yellow"],
    ["lettuce", 3, "v", "green"],
    ["beans", 20, "v", "green"],
    ["red capsicum", 1, "v", "red"],
    ["corn", 20, "v", "yellow"],
)

unsorted_food_list_copy = food_list
sorted_food_list = []

while len(unsorted_food_list_copy) != 0:
    maximum = 0
    for food in unsorted_food_list_copy:
        if food[1] > maximum:
            maximum = food[1]
    sorted_food_list.append(maximum)
    unsorted_food_list_copy.remove(maximum)

I have also tried this:

# initialising list
food_list = (
    ["banana", 10, "f", "yellow"],
    ["apple", 12, "f", "red"],
    ["pear", 60, "f", "green"],
    ["mango", 5, "f", "yellow"],
    ["lettuce", 3, "v", "green"],
    ["beans", 20, "v", "green"],
    ["red capsicum", 1, "v", "red"],
    ["corn", 20, "v", "yellow"],
)

unsorted_food_list_copy = food_list
sorted_food_list = []

while unsorted_food_list_copy:
    min = unsorted_food_list_copy[1]
    for x in unsorted_food_list_copy:
        if x < min:
            min = x
    sorted_food_list.append(min)
    unsorted_food_list_copy.remove(min)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

z祗昰~ 2025-01-19 11:56:10

在您的代码示例中,您将 food_list 定义为 tuple,同时提及列表的列表。为了使用列表功能,如删除、复制或追加,您需要在列表周围添加括号。

首先,您的 food_list 应该这样定义:

food_list = [
    ['banana', 10, 'f', 'yellow'],
    ['apple', 12, 'f', 'red'],
    ['pear', 60, 'f', 'green'],
    ['mango', 5, 'f', 'yellow'],
    ['lettuce', 3, 'v', 'green'],
    ['beans', 20, 'v', 'green'],
    ['red capsicum', 1, 'v', 'red'],
    ['corn', 20, 'v', 'yellow'],
]

其次,您在迭代列表之前设置最小值,您考虑列表的第一个元素以便开始寻找较小的整数。

minValue = unsorted_food_list_copy[0]

完整的解决方案:

# initializing list
food_list = [
    ['banana', 10, 'f', 'yellow'],
    ['apple', 12, 'f', 'red'],
    ['pear', 60, 'f', 'green'],
    ['mango', 5, 'f', 'yellow'],
    ['lettuce', 3, 'v', 'green'],
    ['beans', 20, 'v', 'green'],
    ['red capsicum', 1, 'v', 'red'],
    ['corn', 20, 'v', 'yellow'],
]

unsorted_food_list_copy = food_list.copy()
sorted_food_list = []

for i in range(len(unsorted_food_list_copy)):
    minValue = unsorted_food_list_copy[0]
    for x in unsorted_food_list_copy:
        if x[1] < minValue[1]:
            minValue = x
    sorted_food_list.append(minValue)
    unsorted_food_list_copy.remove(minValue)

sorted_food_list_descending = sorted_food_list[::-1]
print(sorted_food_list_descending)

# Ouput
    [['pear', 60, 'f', 'green'],
    ['corn', 20, 'v', 'yellow'],
    ['beans', 20, 'v', 'green'],
    ['apple', 12, 'f', 'red'],
    ['banana', 10, 'f', 'yellow'],
    ['mango', 5, 'f', 'yellow'],
    ['lettuce', 3, 'v', 'green'],
    ['red capsicum', 1, 'v', 'red']]

In your code sample, you define food_list as a tuple while mentioning a list of list. In order to use list function like remove, copy or append, you need to add brackets around your lists.

Firstly, your food_list should be defined this way :

food_list = [
    ['banana', 10, 'f', 'yellow'],
    ['apple', 12, 'f', 'red'],
    ['pear', 60, 'f', 'green'],
    ['mango', 5, 'f', 'yellow'],
    ['lettuce', 3, 'v', 'green'],
    ['beans', 20, 'v', 'green'],
    ['red capsicum', 1, 'v', 'red'],
    ['corn', 20, 'v', 'yellow'],
]

Secondly you set up your minimum value before iterating over the list, you consider the 1st element of your list in order to start looking for a lower integer.

minValue = unsorted_food_list_copy[0]

Complete solution :

# initializing list
food_list = [
    ['banana', 10, 'f', 'yellow'],
    ['apple', 12, 'f', 'red'],
    ['pear', 60, 'f', 'green'],
    ['mango', 5, 'f', 'yellow'],
    ['lettuce', 3, 'v', 'green'],
    ['beans', 20, 'v', 'green'],
    ['red capsicum', 1, 'v', 'red'],
    ['corn', 20, 'v', 'yellow'],
]

unsorted_food_list_copy = food_list.copy()
sorted_food_list = []

for i in range(len(unsorted_food_list_copy)):
    minValue = unsorted_food_list_copy[0]
    for x in unsorted_food_list_copy:
        if x[1] < minValue[1]:
            minValue = x
    sorted_food_list.append(minValue)
    unsorted_food_list_copy.remove(minValue)

sorted_food_list_descending = sorted_food_list[::-1]
print(sorted_food_list_descending)

# Ouput
    [['pear', 60, 'f', 'green'],
    ['corn', 20, 'v', 'yellow'],
    ['beans', 20, 'v', 'green'],
    ['apple', 12, 'f', 'red'],
    ['banana', 10, 'f', 'yellow'],
    ['mango', 5, 'f', 'yellow'],
    ['lettuce', 3, 'v', 'green'],
    ['red capsicum', 1, 'v', 'red']]
慈悲佛祖 2025-01-19 11:56:10

要按降序执行此操作,您可以执行以下操作:

food_list = [
    ['banana', 10, 'f', 'yellow'],
    ['apple', 12, 'f', 'red'],
    ['pear', 60, 'f', 'green'],
    ['mango', 5, 'f', 'yellow'],
    ['lettuce', 3, 'v', 'green'],
    ['beans', 20, 'v', 'green'],
    ['red capsicum', 1, 'v', 'red'],
    ['corn', 20, 'w', 'yellow'],
]
food_list_copy = food_list.copy()
new_list = []

while food_list_copy:
    hi = food_list_copy[0]
    pi = 0
    for i, e in enumerate(food_list_copy[1:], 1):
        if e[1] > hi[1]:
            hi = e
            pi = i
        elif e[1] == hi[1]:
            if e[2] > hi[2]:
                hi = e
                pi = i
    new_list.append(hi)
    food_list_copy.pop(pi)
print(new_list)

输出:

[['pear', 60, 'f', 'green'],
 ['corn', 20, 'w', 'yellow'],
 ['beans', 20, 'v', 'green'],
 ['apple', 12, 'f', 'red'],
 ['banana', 10, 'f', 'yellow'],
 ['mango', 5, 'f', 'yellow'],
 ['lettuce', 3, 'v', 'green'],
 ['red capsicum', 1, 'v', 'red']]

To do this in descending order you could do this:

food_list = [
    ['banana', 10, 'f', 'yellow'],
    ['apple', 12, 'f', 'red'],
    ['pear', 60, 'f', 'green'],
    ['mango', 5, 'f', 'yellow'],
    ['lettuce', 3, 'v', 'green'],
    ['beans', 20, 'v', 'green'],
    ['red capsicum', 1, 'v', 'red'],
    ['corn', 20, 'w', 'yellow'],
]
food_list_copy = food_list.copy()
new_list = []

while food_list_copy:
    hi = food_list_copy[0]
    pi = 0
    for i, e in enumerate(food_list_copy[1:], 1):
        if e[1] > hi[1]:
            hi = e
            pi = i
        elif e[1] == hi[1]:
            if e[2] > hi[2]:
                hi = e
                pi = i
    new_list.append(hi)
    food_list_copy.pop(pi)
print(new_list)

Output:

[['pear', 60, 'f', 'green'],
 ['corn', 20, 'w', 'yellow'],
 ['beans', 20, 'v', 'green'],
 ['apple', 12, 'f', 'red'],
 ['banana', 10, 'f', 'yellow'],
 ['mango', 5, 'f', 'yellow'],
 ['lettuce', 3, 'v', 'green'],
 ['red capsicum', 1, 'v', 'red']]
荆棘i 2025-01-19 11:56:10

根据指定(用户定义的)键对列表进行排序的 Python 方法是使用带有 key 参数的 sort

food_list = [
    ["banana", 10, "f", "yellow"],
    ["apple", 12, "f", "red"],
    ["pear", 60, "f", "green"],
    ["mango", 5, "f", "yellow"],
    ["lettuce", 3, "v", "green"],
    ["beans", 20, "v", "green"],
    ["red capsicum", 1, "v", "red"],
    ["corn", 20, "w", "yellow"],
]

# create a copy (you can also use `copy.deepcopy`) and sort
sorted_food_list = [list(i) for i in food_list]
sorted_food_list.sort(key=lambda i: (-i[1], i[2]))

输出

[['pear', 60, 'f', 'green'],
 ['beans', 20, 'v', 'green'],
 ['corn', 20, 'w', 'yellow'],
 ['apple', 12, 'f', 'red'],
 ['banana', 10, 'f', 'yellow'],
 ['mango', 5, 'f', 'yellow'],
 ['lettuce', 3, 'v', 'green'],
 ['red capsicum', 1, 'v', 'red']]

The pythonic way to sort a list according to a specify (user-defined) key is to use sort with the key parameter.

food_list = [
    ["banana", 10, "f", "yellow"],
    ["apple", 12, "f", "red"],
    ["pear", 60, "f", "green"],
    ["mango", 5, "f", "yellow"],
    ["lettuce", 3, "v", "green"],
    ["beans", 20, "v", "green"],
    ["red capsicum", 1, "v", "red"],
    ["corn", 20, "w", "yellow"],
]

# create a copy (you can also use `copy.deepcopy`) and sort
sorted_food_list = [list(i) for i in food_list]
sorted_food_list.sort(key=lambda i: (-i[1], i[2]))

output

[['pear', 60, 'f', 'green'],
 ['beans', 20, 'v', 'green'],
 ['corn', 20, 'w', 'yellow'],
 ['apple', 12, 'f', 'red'],
 ['banana', 10, 'f', 'yellow'],
 ['mango', 5, 'f', 'yellow'],
 ['lettuce', 3, 'v', 'green'],
 ['red capsicum', 1, 'v', 'red']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文