Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.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

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

Python 如何将文件从最低到最高排序(包括负数)?

Python 如何将文件从最低到最高排序(包括负数)?,python,sorting,Python,Sorting,我想按升序对这两个文件进行排序,包括负值 Value.txt= -0.0059 0.0716 -0.0058 -0.0059 -0.1139 -0.1139 -0.0312 -0.0759 -0.0341 0.0047 -0.1813 -0.185 -0.06585 -0.023 -0.1438 0.05921 0.0854 -0.2039 -0.1813 0.05921 Character.txt= man king new one text gorilla zilla dulla mell

我想按升序对这两个文件进行排序,包括负值

Value.txt=

-0.0059
0.0716
-0.0058
-0.0059
-0.1139
-0.1139
-0.0312
-0.0759
-0.0341
0.0047
-0.1813
-0.185
-0.06585
-0.023
-0.1438
0.05921
0.0854
-0.2039
-0.1813
0.05921
Character.txt=

man
king
new
one
text
gorilla
zilla
dulla
mella
killa
anw
testing
worry
no
time
test
kiss
queue
mouse
like
预期输出-这是我希望收到的代码

queue   -0.20399
testing -0.185
mouse   -0.181378
anw -0.1813
time    -0.1438
text    -0.1139
gorilla -0.1139
dulla   -0.0759
worry   -0.06585
mella   -0.0341
zilla   -0.0312
no  -0.023
man -0.0059
one -0.0059
new -0.0058
killa   0.0047
like    0.05921
test    0.0592104
king    0.0716
kiss    0.08544
我的代码:我试图构建此代码,但它不起作用

with open("all.txt", "w+") as outfile:
        value= open("value.txt","r").read().splitlines()
        character= open("character.txt","r").readlines()
        a = sorted(list(zip(value,character)))
        for x in a:
            line = " ".join(str(uu) for uu in x)
            outfile.write("{}".format(line))
不知何故,我的输出错误如下:

-0.0058 new
-0.0059 man
-0.0059 one
-0.023 no
-0.0312 zilla
-0.0341 mella
-0.06585 worry
-0.0759 dulla
-0.1139 gorilla
-0.1139 text
-0.1438 time
-0.1813 anw
-0.181378 mouse
-0.185 testing
-0.20399 queue
0.0047 killa
0.05921 like
0.0592104 test
0.0716 king
0.08544 kiss
我尝试了许多其他的方法,但仍然无法达到预期的效果。谁能帮我一下吗

这是一种方法

演示:

with open(filename) as infile, open(filename1) as infile_1:
    value =  [float(line.strip()) for line in infile.readlines()]
    character =  [line.strip() for line in infile_1.readlines()]

data = zip(value, character)
for i in sorted(data, key=lambda x: x[0], reverse=True)[::-1]:
    print( "{1} = {0}".format(*i) )
queue = -0.2039
testing = -0.185
mouse = -0.1813
anw = -0.1813
time = -0.1438
gorilla = -0.1139
text = -0.1139
dulla = -0.0759
worry = -0.06585
mella = -0.0341
zilla = -0.0312
no = -0.023
one = -0.0059
man = -0.0059
new = -0.0058
killa = 0.0047
like = 0.05921
test = 0.05921
king = 0.0716
kiss = 0.0854
输出:

with open(filename) as infile, open(filename1) as infile_1:
    value =  [float(line.strip()) for line in infile.readlines()]
    character =  [line.strip() for line in infile_1.readlines()]

data = zip(value, character)
for i in sorted(data, key=lambda x: x[0], reverse=True)[::-1]:
    print( "{1} = {0}".format(*i) )
queue = -0.2039
testing = -0.185
mouse = -0.1813
anw = -0.1813
time = -0.1438
gorilla = -0.1139
text = -0.1139
dulla = -0.0759
worry = -0.06585
mella = -0.0341
zilla = -0.0312
no = -0.023
one = -0.0059
man = -0.0059
new = -0.0058
killa = 0.0047
like = 0.05921
test = 0.05921
king = 0.0716
kiss = 0.0854
这是一种方法

演示:

with open(filename) as infile, open(filename1) as infile_1:
    value =  [float(line.strip()) for line in infile.readlines()]
    character =  [line.strip() for line in infile_1.readlines()]

data = zip(value, character)
for i in sorted(data, key=lambda x: x[0], reverse=True)[::-1]:
    print( "{1} = {0}".format(*i) )
queue = -0.2039
testing = -0.185
mouse = -0.1813
anw = -0.1813
time = -0.1438
gorilla = -0.1139
text = -0.1139
dulla = -0.0759
worry = -0.06585
mella = -0.0341
zilla = -0.0312
no = -0.023
one = -0.0059
man = -0.0059
new = -0.0058
killa = 0.0047
like = 0.05921
test = 0.05921
king = 0.0716
kiss = 0.0854
输出:

with open(filename) as infile, open(filename1) as infile_1:
    value =  [float(line.strip()) for line in infile.readlines()]
    character =  [line.strip() for line in infile_1.readlines()]

data = zip(value, character)
for i in sorted(data, key=lambda x: x[0], reverse=True)[::-1]:
    print( "{1} = {0}".format(*i) )
queue = -0.2039
testing = -0.185
mouse = -0.1813
anw = -0.1813
time = -0.1438
gorilla = -0.1139
text = -0.1139
dulla = -0.0759
worry = -0.06585
mella = -0.0341
zilla = -0.0312
no = -0.023
one = -0.0059
man = -0.0059
new = -0.0058
killa = 0.0047
like = 0.05921
test = 0.05921
king = 0.0716
kiss = 0.0854

我不知道这是不是最有效的方法。但如果我靠近您的代码,我会添加几行代码来转换
value
中的项目:

with open("all.txt", "w+") as outfile:
        value= open("value.txt","r").read().splitlines()
        # additional 2 lines:
        for i in range(len(value)):
            value[i] = float(value[i])
        character= open("character.txt","r").readlines()
        a = sorted(list(zip(value,character)))
        print(a)
        for x in a:
            line = " ".join(str(uu) for uu in x)
            outfile.write("{}".format(line))

我不知道这是不是最有效的方法。但如果我靠近您的代码,我会添加几行代码来转换
value
中的项目:

with open("all.txt", "w+") as outfile:
        value= open("value.txt","r").read().splitlines()
        # additional 2 lines:
        for i in range(len(value)):
            value[i] = float(value[i])
        character= open("character.txt","r").readlines()
        a = sorted(list(zip(value,character)))
        print(a)
        for x in a:
            line = " ".join(str(uu) for uu in x)
            outfile.write("{}".format(line))

我建议检查
value
中存储的数据类型。具体来说,您可能需要确保它的元素是整数类型而不是字符串。很抱歉,它存储在字符串中。如何将其转换为整数?是的,但该列表中各个元素的类型是什么?抱歉,该列表中各个元素的类型是整数。请仔细检查您的数据类型,我刚刚运行了您的代码(使用数组中的数据)我建议检查
value
中存储的数据类型。具体来说,您可能需要确保它的元素是整数类型而不是字符串。很抱歉,它存储在字符串中。如何将其转换为整数?是的,但该列表中各个元素的类型是什么?抱歉,该列表中各个元素的类型是整数。请仔细检查您的数据类型,我刚刚运行了您的代码(使用数组中的数据),它会吐出您想要的内容。先生,如果允许,你能告诉我使用{1}{0}.format(i)和{}{0}.format(i)的区别吗。。。“这个”是干什么用的?谢谢我明确地说元素1应该在结果字符串中的元素0之前。使用
print({1}={0}.format(*i))print({}={}.format(*i))
谢谢您的解释。先生,如果允许的话,你能告诉我使用{1}{0}.format(I)和{}{}.format(I)的区别吗。。。“这个”是干什么用的?谢谢我明确地说元素1应该在结果字符串中的元素0之前。使用
print({1}={0}.format(*i))print({}={}.format(*i))
谢谢您的解释。我试试看。