Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python:如何在没有排序函数的情况下对列表中的字母表进行排序?_Python_String_List_Sorting_Alphabetical - Fatal编程技术网

Python:如何在没有排序函数的情况下对列表中的字母表进行排序?

Python:如何在没有排序函数的情况下对列表中的字母表进行排序?,python,string,list,sorting,alphabetical,Python,String,List,Sorting,Alphabetical,这不是基于效率,必须只使用非常基本的python知识(字符串、元组、列表基础知识)来完成,因此不需要导入函数或使用排序/排序。(这使用的是Python 2.7.3) 例如,我有一个列表: unsort_list = ["B", "D", "A", "E", "C"] sort_list = [] 排序列表需要能够打印出来: "A, B, C, D, E" 我可以用数字/整数来做,对字母顺序的字符串有类似的方法吗?如果不是,您会推荐什么(即使效率不高)而不导入或排序函数。这里是Python中算

这不是基于效率,必须只使用非常基本的python知识(字符串、元组、列表基础知识)来完成,因此不需要导入函数或使用排序/排序。(这使用的是Python 2.7.3)

例如,我有一个列表:

unsort_list = ["B", "D", "A", "E", "C"]
sort_list = []
排序列表需要能够打印出来:

"A, B, C, D, E"

我可以用数字/整数来做,对字母顺序的字符串有类似的方法吗?如果不是,您会推荐什么(即使效率不高)而不导入或排序函数。

这里是Python中算法的一个非常简短的实现:

def quicksort(lst):
    if not lst:
        return []
    return (quicksort([x for x in lst[1:] if x <  lst[0]])
            + [lst[0]] +
            quicksort([x for x in lst[1:] if x >= lst[0]]))
更简单的是:

dc = { }
for a in unsorted_list:
  dc[a] = '1'

sorted_list = dc.keys()
只是为了好玩:

from random import shuffle
unsorted_list = ["B", "D", "A", "E", "C"]

def is_sorted(iterable):
  for a1,a2 in zip(iterable, iterable[1:]):
     if a1 > a2: return False
  return True

sorted_list = unsorted_list
while True:
   shuffle(sorted_list)
   if is_sorted(sorted_list): break
平均复杂度应该是阶乘的,最坏的情况是无限的

u=[“B”、“D”、“A”、“E”、“C”]
u = ["B", "D", "A", "E", "C"]
y=[]
count=65
while len(y)<len(u):
    for i in u:
        if ord(i)==count:
            y.append(i)
            count+=1
print(y)
y=[] 计数=65
而len(y)仅使用
min()
内置和
list
对象方法:

unsort_list = ["B", "D", "A", "E", "C"]
sort_list = []

while unsort_list:
    smallest = min(unsort_list)
    sort_list.append(smallest)
    unsort_list.pop(unsort_list.index(smallest))

print sort_list

它会破坏未排序的列表,因此您可能希望复制并使用它。

list\u val=['c'、'd'、'e'、'a'、'r']

for passnum in range(len(list_val)-1, 0, -1):
  for i in range(passnum):
    if list_val[i] > list_val[i+1]:
      list_val[i], list_val[i+1] = list_val[i+1], list_val[i]
打印列表\u val

该库实现了一个名为的合并排序算法


Python已经知道哪个字符串是第一个,哪个是下一个,这取决于它们的ASCII值

例如:

"A"<"B"
 True
结果:

sorted list:['A', 'B', 'C', 'D', 'E']

有许多可用的标准库,可以使用一行代码来完成。

我已经尝试过类似的方法,但我不确定程序的时间复杂性

l=['a','c','b','f','e','z','s']
l1=[]
while l:
    min=l[0]
    for i in l:
        # here **ord** means we get the ascii value of particular character.
        if ord(min)>ord(i):
            min=i
     l1.append(min)
     l.remove(min)
print(l1)
def快速排序(lst):
如果不是lst:
返回[]
返回(快速排序([x代表lst[1:]中的x,如果x=lst[0]]))
unsort_list=['B','D','A','E','C']
排序列表=快速排序(取消排序列表)
排序表

['A','B','C','D','E']

u=[“B”、“D”、“A”、“F”、“C”]
y=[]
计数=65

而len(y)。选择或泡泡是最容易实现的。这是你的家庭作业,这是你自己做的!如果可以使用数字,那么就可以使用字符串,而无需对代码进行任何更改。在python中,您可以像数字一样使用
等来比较字符串。Ascii字符代码的组织方式是
ord(“A”)
ord()
是顺序函数。如果整数和字符串之间存在一对一的映射,那么可以使用与数字完全相同的算法对字母进行排序。filter+lambda?不确定我是否喜欢这个。@thg435好的,我也不喜欢。我将它改为使用理解这只适用于
int
,因为在这种情况下,哈希是
int
本身,非常慢,tho:-)当u将包含非连续元素时,此代码将导致无限循环,例如当u=[“B”、“D”、“A”、“F”、“C”]。原因是计数从65开始,它对应于ascii值“A”,并且对于数组中的下一个值递增。在给定列表中找到D后,它将无法找到“E”,因此检查ord(“G”)==68将永远不会给出真正的结果,因此它将导致无限循环,因为len(y)永远不会到达len(u)。如果您解释您提供的代码如何回答问题,这将是一个更好的答案。
unsort_list = ["B", "D", "A", "E", "C"]
def sortalfa(unsort_list):
    for i in range(len(unsort_list)-1):
        for j in range(i+1,len(unsort_list)):
            if unsort_list[i]>unsort_list[j]:
                temp = unsort_list[i]
                unsort_list[i] = unsort_list[j]
                unsort_list[j] = temp
    print("sorted list:{}".format(unsort_list))

sortalfa(["B", "D", "A", "E", "C"])
sorted list:['A', 'B', 'C', 'D', 'E']
l=['a','c','b','f','e','z','s']
l1=[]
while l:
    min=l[0]
    for i in l:
        # here **ord** means we get the ascii value of particular character.
        if ord(min)>ord(i):
            min=i
     l1.append(min)
     l.remove(min)
print(l1)
u = ["B", "D", "A", "F", "C"]

y=[]

count=65

while len(y)<len(u):

    for i in u:

        if ord(i)==count:

            y.append(i)

    count+=1

print(y)