相关疑难解决方法(0)

在python中按整数后缀排序字符串列表

我有一个字符串列表:

[song_1, song_3, song_15, song_16, song_4, song_8]
Run Code Online (Sandbox Code Playgroud)

我想用最后的#对它们进行排序,不幸的是,由于较低的数字不是"08"并且是"8",它们在字典顺序中被视为大于15.

我知道我必须将一个密钥传递给sort函数,我在这个网站的某个地方看到这个字符串的十进制数字:

sorted(the_list, key=lambda a:map(int,a.split('.'))
Run Code Online (Sandbox Code Playgroud)

但那是"1.2,2.5,2.3",但我没有那种情况.我想过要替换'.' 使用'_'但是根据我的理解,它将双方转换为整数,这将失败,因为_的左侧是一个字符串.

任何帮助,将不胜感激

编辑:我忘了提到所有的前缀是相同的(本例中的歌曲)

python

6
推荐指数
2
解决办法
7866
查看次数

按顺序依次读取文件

我在文件夹中有许多文件,其名称遵循约定:

0.1.txt, 0.15.txt, 0.2.txt, 0.25.txt, 0.3.txt, ...
Run Code Online (Sandbox Code Playgroud)

我需要逐个阅读它们并操纵它们内部的数据.目前我用命令打开每个文件:

import os
# This is the path where all the files are stored.
folder path = '/home/user/some_folder/'
# Open one of the files,
for data_file in os.listdir(folder_path):
    ...
Run Code Online (Sandbox Code Playgroud)

不幸的是,它没有按特定的顺序读取文件(不确定它是如何选择它们)而我需要从具有最小数字作为文件名的那个开始读取它们,然后是具有直接较大数字的那个,依此类推直到最后一个.

python file-io

5
推荐指数
1
解决办法
9953
查看次数

如何用数字拆分混合字符串

我在文本文件中有一个数据,其中包含"Test DATA_g004,Test DATA_g003,Test DATA_g001,Test DATA_g002".

是否可以在没有"Test DATA_"字样的情况下对其进行排序,以便将数据分类为g001,g002,g003等?

我试过这个.split("Test DATA_")方法,但它不起作用.

def readFile():
    #try block will execute if the text file is found
    try:
        fileName = open("test.txt",'r')
        data = fileName.read().split("\n")
        data.sort (key=alphaNum_Key) #alternative sort function
        print(data)
    #catch block will execute if no text file is found
    except IOError:
        print("Error: File do not exist")
        return

#Human sorting
def alphaNum(text):
    return int(text) if text.isdigit() else text

#Human sorting
def alphaNum_Key(text):
    return [ alphaNum(c) for c in re.split('(\d+)', text) ]
Run Code Online (Sandbox Code Playgroud)

python sorting string file-io

5
推荐指数
1
解决办法
414
查看次数

带有负数的 Python 排序列表

为了通过实践学习python,我正在尝试使用python实现和测试快速排序算法。

实现本身并不难,但是排序的结果有点令人费解:

当我对列表进行排序时

['35', '-1', '-2', '-7', '-8', '-3', '-4', '20', '-6', '53']

结果给了我

['-1', '-2', '-3', '-4', '-6', '-7', '-8', '20', '35', '53']

因此列表已排序,但负整数以相反的顺序排序。

我怀疑这可能是我正在对从文件中读取的整数列表进行排序这一事实的问题,并且 int 的类型实际上不是 int 而是其他的东西(也许是字符串。)我能做些什么来解决这个问题?

这是快速排序实现的代码

#quicksort -> the conquer part of the algorithm
def quicksort(input_list, start_index, end_index):
    if start_index < end_index:
        #partition the list of integers.
        pivot = partition(input_list, start_index, end_index)
        #recursive call on both sides of the pivot, that is excluding the pivot itself
        quicksort(input_list, start_index, pivot-1)
        quicksort(input_list, pivot+1, end_index)
    return input_list

#divide …
Run Code Online (Sandbox Code Playgroud)

python sorting quicksort negative-number python-2.7

5
推荐指数
1
解决办法
1万
查看次数

在Python中对包含路径的列表进行排序.

我有一个列表paths_list,其中包含特定文件夹的文件(图像)的路径.例:

['/home/username/images/s1/4.jpg', '/home/username/images/s1/7.jpg', '/home/username/images/s1/6.jpg', '/home/username/images/s1/3.jpg', '/home/username/images/s1/5.jpg', '/home/username/images/s1/10.jpg', '/home/username/images/s1/9.jpg', '/home/username/images/s1/1.jpg', '/home/username/images/s1/2.jpg', '/home/username/images/s1/12.jpg', '/home/username/images/s1/11.jpg', '/home/username/images/s1/8.jpg']

我想顺序[/1.jpg ,2.jpg .....,/12.jpg] 排序:既不按长度排序也不按字母顺序排序都有帮助.这应该做什么?

python sorting python-2.7

5
推荐指数
3
解决办法
3520
查看次数

如何按数字顺序对python中的文件进行排序?

我正在从图像创建 pdf 文件,但在按数字顺序对 jpg 文件进行排序时遇到问题我有 20 个文件,从 1.jpg 到 20.jpg 我正在使用下面的代码按顺序对所有文件进行排序

import os
sorted(os.listdir('path/to/jpg/files'))
Run Code Online (Sandbox Code Playgroud)

但它会打印 1.jpg、11.jpg、12.jpg 等等。

有任何想法吗?

python sorting

5
推荐指数
1
解决办法
947
查看次数

如何在Unix中按字母数字排序?比看起来更复杂

我正在尝试使用unix sort命令以"直观"/自然的方式按字母顺序对一串字母和数字进行排序,但无法正确排序.我有这个文件:

$ cat ~/headers 
@42EBKAAXX090828:6:100:1699:328/2
@42EBKAAXX090828:6:10:1077:1883/2
@42EBKAAXX090828:6:102:785:808/2
Run Code Online (Sandbox Code Playgroud)

我想用字母数字来排序,直觉上@42EBKAAXX090828:6:10:...是第一个(因为10小于100102),第二个是@42EBKAAXX090828:6:100...第三个@42EBKAAXX090828:6:102:204:1871/2.

我知道建议对线内的特定位置进行排序,但:这里的位置可能会有所不同,因此这不是一个通用且可行的解决方案.

我试过了:

sort --stable -k1,1 ~/headers > foo
Run Code Online (Sandbox Code Playgroud)

与各种组合-n-u参数,但它没有给出正确的顺序.

如何从bash使用sort或从Python中有效地完成这项工作?我想将它应用于大小为4-5 GB的文件,因此包含数百万行.

谢谢!

python unix sorting bash shell

4
推荐指数
1
解决办法
983
查看次数

如何在python中通过regex获取dict值

dict1={'s1':[1,2,3],'s2':[4,5,6],'a':[7,8,9],'s3':[10,11]}
Run Code Online (Sandbox Code Playgroud)

我如何获得与's'关键的所有值?喜欢dict1['s*']得到的结果是dict1['s*']=[1,2,3,4,5,6,10,11]

python regex dictionary

4
推荐指数
1
解决办法
2148
查看次数

在python中以glob.glob命令

我有一个文件列表:

foo_00.txt
foo_01.txt
foo_02.txt
foo_03.txt
foo_04.txt
foo_05.txt
foo_06.txt
foo_07.txt
foo_08.txt
foo_09.txt
foo_10.txt
foo_11.txt
.........
.........
foo_100.txt
foo_101.txt
Run Code Online (Sandbox Code Playgroud)

当我使用

import glob
PATH = "C:\testfoo"
listing = glob.glob(os.path.join(PATH, '*.txt'))
Run Code Online (Sandbox Code Playgroud)

我有这个订单

    foo_00.txt
    foo_01.txt
    foo_02.txt
    foo_03.txt
    foo_04.txt
    foo_05.txt
    foo_06.txt
    foo_07.txt
    foo_08.txt
    foo_09.txt
    foo_100.txt
    foo_101.txt
    .........
    .........
    foo_10.txt
    foo_11.txt
    .........
Run Code Online (Sandbox Code Playgroud)

我也试过sorted(glob.glob(os.path.join(PATH, '*.txt')))但没有解决我的问题,因为我希望有正确的序列.在foo_09.txt之后我想导入foo_10.txt而不是foo_100.txt等等.

python glob list

4
推荐指数
1
解决办法
8449
查看次数

Python对包含整数和内部有整数的字符串的列表进行排序

我如何使用 python 对列表格式进行排序

format=["12 sheet","4 sheet","48 sheet","6 sheet", "busrear", "phonebox","train"]
Run Code Online (Sandbox Code Playgroud)

像这样

format =["4 sheet", "6 sheet", "12 sheet", "48 sheet", "busrear", "phonebox", "train"]
Run Code Online (Sandbox Code Playgroud)

答案是Python对内部有整数的字符串数组进行排序

但是如果数组是列表的列表那么我们怎样才能像这样做到这一点

format=[[1, '12 sheet', 0],[2, '4 sheet', 0], [3, '48 sheet', 0], [4, '6 sheet', 0 [5, 'Busrear', 0], [6, 'phonebox', 0], [7, 'train', 0]]
Run Code Online (Sandbox Code Playgroud)

我需要这样的结果

format=[[2, '4 sheet', 0],[4, '6 sheet', 0],[1, '12 sheet', 0],[3, '48 sheet', 0],[5, 'Busrear', 0], [6, 'phonebox', 0], [7, 'train', 0]]
Run Code Online (Sandbox Code Playgroud)

python sorting list

4
推荐指数
1
解决办法
2万
查看次数