将列表中的唯一元素追加到新列表

时间:2019-11-17 02:01:39

标签: python list indexing

  1. 编写一个Python函数sort_a_list,该函数接受一个由数字或单个字符组成的列表,并返回一个已排序的列表。

a。您不能使用python函数sort(),但可以使用min()和/或max()

b。如果输入参数包含重复值,则返回仅包含唯一值的列表(注意:大写字母和小写字母不相同)

c。例子:

i。 sort_a_list([2,-3])-> [-3,2]

ii。 sort_a_list([2,3,2])-> [2,3]

iii。 sort_a_list([2,3,1])-> [1,2,3]

iv。 sort_a_list(['z','a'])-> ['a','z']

v。 sort_a_list(['H','B'])-> ['B','H']

vi。 sort_a_list(['a','B'])-> ['B','a']

    if s == []:
        return []

    number_list = []
    letter_list = []
    final_list = []

    if type(s[0])==int:
        my_list = 'numbers'
    else:
        my_list = 'letters'
        i=0
        while i < len(s):
            #convert letters to ascii
            s[i]=ord(s[i])
            i+=1
    if my_list == 'numbers':

        i = 0
        while 0 != len(s):
            #sort the list
            number_list.append(min(s))
            s.remove(min(s))
            i+=1

            j=0
            while j < len(number_list)-1:
                #remove duplicates
                if number_list[j]==number_list[j+1]:
                    number_list.remove(number_list[j+1])
                j+=1
        return number_list

    else:
        #sorts through the letters
        i = 0
        while 0 < len(s):
            #sort the list by ascii value
            letter_list.append(min(s))
            s.remove(min(s))
            #take out the minimum value to get the second lowest etc.
            i+=1

            j=0
            while j < len(letter_list)-1:
                #remove duplicates
                if letter_list[j]==letter_list[j+1]:
                    letter_list.remove(letter_list[j+1])
                j+=1

                while 0<len(letter_list):
                    final_list.append(chr(letter_list[0]))
                    letter_list.remove(letter_list[0])

                    while len(letter_list)!=0:
                        final_list.append(letter_list[0])
                        letter_list.pop()
                        final_list[-1]=chr(final_list[-1])

    return final_list,letter_list[0],len(letter_list)

print(sort_a_list(['a','B','d','K','e']))

我得到的输出是[['B','K','a','d'],101,1)

我如何从letter_list中取出101,将其从ASCII转换为字符并将其附加到final_list?在尝试了多种方法之后,我不断收到类型错误和索引错误。

1 个答案:

答案 0 :(得分:0)

只要弄清楚怎么做,没关系!

def sort_a_list(s):
    if s == []:
        return []

    number_list = []
    letter_list = []
    final_list = []

    if type(s[0])==int:
        my_list = 'numbers'
    else:
        my_list = 'letters'

    if my_list == 'numbers':

        i = 0
        while 0 != len(s):
            #sort the list
            number_list.append(min(s))
            s.remove(min(s))
            i+=1

            j=0
            while j < len(number_list)-1:
                #remove duplicates
                if number_list[j]==number_list[j+1]:
                    number_list.remove(number_list[j+1])
                j+=1
        return number_list

    else:
        #sorts through the letters
        i = 0
        while 0 < len(s):
            letter_list.append(min(s))
            s.remove(min(s))
            #take out the minimum value to get the second lowest etc.
            i+=1

            j=0
            while j < len(letter_list)-1:
                #remove duplicates
                if letter_list[j]==letter_list[j+1]:
                    letter_list.remove(letter_list[j+1])
                j+=1

    return letter_list

print(sort_a_list(['a','B','d','K','e','e','B']))