Arrays 按顺序对字符串排序
我想将列表排序,例如[stack、overflow、yeet、apple]到[aelpp、ackst、eety、efloorvw]。我有基数排序数字的代码:Arrays 按顺序对字符串排序,arrays,Arrays,我想将列表排序,例如[stack、overflow、yeet、apple]到[aelpp、ackst、eety、efloorvw]。我有基数排序数字的代码: def countingsort(nums, place): n = len(nums) output = [0 for i in range(0,n)] freq = [0 for i in range(0,10)] for i in range(0,n): freq[(nums[i]//place)%
def countingsort(nums, place):
n = len(nums)
output = [0 for i in range(0,n)]
freq = [0 for i in range(0,10)]
for i in range(0,n):
freq[(nums[i]//place)%10] += 1
for i in range(1,10):
freq[i] += freq[i - 1]
#Build the output array
for i in range(n-1,-1,-1):
output[freq[(nums[i]//place)%10] - 1] = nums[i]
freq[(nums[i]//place)%10] -= 1
#List will now be the new array
for i in range(0,n):
nums[i] = output[i]
我知道我可以使用ord()和-ord('a')来修改代码,使之符合我的要求,但是我不确定应该在哪里以及如何准确地实现它。有什么关于我应该如何处理的提示吗?
谢谢大家!
编辑,因为基数不会改变。以上逻辑可以通过以下方式实现:
list1 = ['stack', 'overflow', 'yeet', 'apple']
print(sorted([''.join(sorted(i)) for i in list1]))
但我不知道如何按字母顺序排列每个单词的字母。有什么关于我应该如何处理的提示吗
为了对单词本身进行排序,您应该在对整个列表进行排序之前进行排序,因为在您的示例中,我们按照它们的新版本进行排序,eety,efloorvw
,而不是相反(overfloword()
或不使用-strings是可比较的,因为它们已经是了。:)但是:我们会遇到一个问题,所有的10和除法在您的计数排序。所以我们需要修复它
第一:马克斯。我们想找到最长的东西,而不是最高的数字
第二:计数和索引。我们将在这里使用ord将“a”索引为0,将“z”索引为25。。。我们需要最后一个存储桶来存储在这里没有字符的字符串
def radixsort(nums):
n = len(nums)
max = len(nums[0])
#find max number in the array
for i in nums:
if max < len(i):
max = len(i)
#Counting sort is performed based on place.
#like ones place, tens place and so on.
place = 1 # we'll use it to index string
while max >= place:
countingsort(nums, place, max) # we need to know how many letters the longest string has
place += 1
def countingsort(nums, place, max):
n = len(nums)
output = [0 for i in range(0,n)]
#range of the letters is a-z and no character (giving 0-26 indexes) for each place considered.
freq = [0 for i in range(ord('a'),ord('z')+2)]
#count number of occurrences in freq array
for i in range(0,n):
if len(nums[i]) <= max-place:
freq[26] += 1 # too short number, we need to put it in "empty character" bucket
else:
freq[ord(nums[i][max-place])-ord('a')] += 1
#Change count[i] so that count[i] now contains actual
#position of this digit in output[]
for i in range(1,27):
freq[i] += freq[i - 1]
#Build the output array
for i in range(n-1,-1,-1):
if len(nums[i]) <= max-place:
index = 26
else:
index = ord(nums[i][max-place])-ord('a')
output[freq[index] - 1] = nums[i]
freq[index] -= 1
#List will now be the new array
for i in range(0,n):
nums[i] = output[i]
[天哪,这花了我太长时间,我犯了需要调试的愚蠢错误]这不是一个关于如何简化代码的问题。。。这可能是家庭作业的一部分,问题只与单词有关。我能用
numpy
来解决你的问题吗?你能用不同的排序方法来排序字母吗?还是只在这里使用排序?我已经写好了我的答案,但我想知道我能/我应该改变多少计数排序来正确处理这些字母。来吧,堆栈溢出!请不要破坏你的帖子,为别人做更多的工作。通过在Stack Exchange网络上发布,您已授予Stack Exchange在下不可撤销的权利,以分发该内容(即,无论您未来的选择如何)。根据堆栈交换策略,帖子的非破坏版本是分发的版本。因此,任何故意破坏行为都将恢复原状。如果您想了解有关删除帖子的更多信息,请参阅:谢谢您的更正,请删除代码中的最后一行,因为my_列表
已全部排序完毕,并且radixsort
获取int
Object@throwaway13哦,这花了我太长的时间去调试,即使我知道我的目标是什么(我用了n而不是max,但它不起作用,哈哈,我花了大约20分钟的时间试图看看哪里出了问题)。现在版本应该可以工作了(如果我复制了正确的版本,哈哈,我需要检查它-编辑:diff说只有空格和注释不同,所以它很好)当我运行它时,它将返回None。我打印了一些要调试的内容,在countingsort()的末尾,它将打印排序后的列表,但是列表的结尾是None。嗯,我将尝试分析代码!@throwaway13您发布的有问题的代码也不会打印任何内容。更改发生在适当的位置,因此打印(我的列表)
显示已排序的内容
my_list = ['stack', 'overflow', 'yeet', 'apple']
for i in range(len(my_list)):
sublist = list(my_list[i]) # convert string to list of characters - strings are immutable so we can't assign to their indexes
radixsort(sublist)
my_list[i] = "".join(sublist) # convert sorted list back to string
radixsort(my_list)