Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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 - Fatal编程技术网

Python 对字符串排序并返回它

Python 对字符串排序并返回它,python,Python,我有一段代码,我想对字符串进行排序并返回它 def sort_int_string(one_string): one_string = one_string.replace("\n", ' ') one_string = one_string.replace("\t", ' ') one_string = one_string.strip() if len(one_string) == 0: ret

我有一段代码,我想对字符串进行排序并返回它

    def sort_int_string(one_string):

        one_string = one_string.replace("\n", ' ')
        one_string = one_string.replace("\t", ' ')
        one_string = one_string.strip()

       if len(one_string) == 0:
            return ''
       else: 
            one_string = one_string.split(" ")
            one_string = [int(i) for i in one_string]
            one_string.sort()
            one_string = [str(i) for i in one_string]
            return ' '.join(one_string) 
但是当我试着用这个来运行它的时候

    hello = "\t42    4    -17\n"
    print(sort_int_string(hello)
我得到以10为基数的int()的无效文本错误:“”
不知道我为什么会这样。请提供帮助。

您得到的错误是因为
i
的几个值是
'
(空字符串),您试图将它们转换为整数。正如上面的评论所建议的,使用
strip
split
删除空白。然后排序:

def sort_int_字符串(一个字符串):
返回排序(一个字符串.strip().split())
hello=“\t42 4-17\n”
输出=排序\整数\字符串(hello)
打印(输出)#返回['-17',4',42']
如果要将输出重新组合成字符串,可以使用
'.join(output)
或类似的方法。

试试这个

您不必将
\n
\t
替换为
strip()
将删除前导空格和尾随空格

def sort_int_string(one_string):
  string_lst = one_string.strip().split(" ")
  string_lst.sort()
  return ''.join(string_lst)

hello = "\t42    4    -17\n"
print(sort_int_string(hello))
输出:
-17442

您的代码的问题是数组中存在空字符串,当检查是否返回错误时,因为它无法转换为int。因此只需执行一个条件来检查i值是否不是空字符串

def sort_int_字符串(一个字符串):
一个字符串=一个字符串。替换(“\n”,”)
一个字符串=一个字符串。替换(“\t”,”)
一个字符串=一个字符串。strip()
如果len(一个字符串)=0:
返回“”
其他:
一个字符串=一个字符串。拆分(“”)
一个字符串=[int(i)表示一个字符串中的i,如果i!=“”]
一个字符串。sort()
一个字符串=[str(i)表示一个字符串中的i]
返回“”。加入(一个字符串)
hello=“\t42 4-17\n”
打印(排序\输入\字符串(hello))
输出


-17 4 42

之后,我实现了一些列表项

一个字符串=一个字符串。拆分(“”)

都是空引号,所以我循环使用one_string,通过将其存储在新列表中来消除它们。循环之后,我用新列表重写旧列表,如下所示

def sort_int_string(one_string):

    one_string = one_string.replace("\n", ' ')
    one_string = one_string.replace("\t", ' ')
    one_string = one_string.strip()


    if len(one_string) == 0:
        return ''
    else:
        one_string = one_string.split(" ")

        new_one_string = [];

        for num in one_string:
            if num != '':
                new_one_string.append(num)

        one_string = new_one_string;

        one_string = [int(i) for i in one_string]
        one_string.sort()
        one_string = [str(i) for i in one_string]

        return ' '.join(one_string)
输出:


-17 4 42

使用
一个字符串.strip().split(“”
.split(“”)
在每个单独的空格上拆分-由于数字之间有多个空格,这会导致列表中出现一堆空字符串
.split()
(不带参数)在空格(或其他空白字符)上拆分,这可能是您在此处需要的。除了该错误消息外,回溯还包含更多有用的信息,可以帮助我们。你的文章,并添加它。