简体   繁体   English

如何遍历顺序字符串值并附加到嵌套列表

[英]How to iterate through sequential string values and append to a nested list

I have a list containing filenames of a dataset, in the form of a number followed by some descriptive text (which is different for each file):我有一个包含数据集文件名的列表,以数字形式后跟一些描述性文本(每个文件都不同):

a = ['001_sometext', '002_sometext', ..., '162_sometext', '001_sometext', ..., '162_sometext]

The list cycles from '001' to '162' multiple times, but the list also doesn't follow a perfect sequence, some numbers are missing.该列表从'001''162'多次循环,但该列表也没有遵循完美的顺序,缺少一些数字。

My intention is to read all files containing '001' and append them to another list, and then do the same for '002' and so on, such that I end up with a nested list containing a separate list for each number in the sequence.我的意图是读取所有包含'001'文件并将它们附加到另一个列表,然后对'002'执行相同的操作,依此类推,这样我最终得到一个嵌套列表,其中包含序列中每个数字的单独列表.

My current attempt:我目前的尝试:

phrases = []
xi = []
for digits in range(0, 162):
    for x in a:
        if str(digits) in x:
            xi.append(x)
    phrases.append(xi)

However, this gives me a nested list of the entire list over and over again, rather than a list for each number.但是,这会一遍又一遍地给我整个列表的嵌套列表,而不是每个数字的列表。

Edit:编辑:

The loop above is reading all files containing just a '0' , which brings back hundreds of files and adds them to a list.上面的循环正在读取仅包含'0'所有文件,这会带回数百个文件并将它们添加到列表中。 A minor fix is that I've made a loop for each order of magnitude:一个小的修复是我为每个数量级制作了一个循环:

phrases = []
for digits in range(1, 10):
    xi = []
    for x in a:
        if '00' + str(digits) in x:
            xi.append(x)
        else: None
    phrases.append(xi)

and

phrases = []
for digits in range(10, 100):
    xi = []
    for x in a:
        if '0' + str(digits) in x:
            xi.append(x)
        else: None
    phrases.append(xi)

and

phrases = []
for digits in range(100, 162):
    xi = []
    for x in a:
        if str(digits) in x:
            xi.append(x)
        else: None
    phrases.append(xi)

You have a few issues with your code, firstly you need to clear xi on each loop;您的代码有一些问题,首先您需要在每个循环中清除xi then you need to iterate in the range 1 to 163 (ie 1 to 162 inclusive) and finally you can't use str(digits) in x because (for example) str(1) would match against 001 , 015 , 102 etc.那么您需要在 1 到 163 范围内进行迭代(即 1 到 162 包括在内),最后您不能str(digits) in x因为(例如) str(1)将匹配001015102等。

Something like this should work:像这样的东西应该工作:

for digits in range(1, 163):
    xi = []
    srch = f'{digits:03d}'
    for x in a:
        if x.startswith(srch):
            xi.append(x)
    phrases.append(xi)

Alternatively you could use a nested list comprehension:或者,您可以使用嵌套列表推导:

phrases = [ [f for f in a if f.startswith(f'{n:03d}')] for n in range(1, 163)]

If如果

a = ['001_sometext', '002_sometext', '162_sometext', '001_someothertext', '162_someothertext']

both of these give a result of:这两个都给出了以下结果:

[['001_sometext', '001_someothertext'], ['002_sometext'], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], [], ['162_sometext', '162_someothertext']]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Pandas 遍历字符串并将值附加到列表中 - Iterate through a string and append values to a list using pandas 如何遍历列表并将它们添加到嵌套字典中的值? - How to iterate through a list and add them to the values inside nested dictionary? 如何使用 for 循环遍历字典列表、选择键并将值附加到新列表 - How to use a for loop to iterate through a list of dictionaries, select a key, and append the values to a new list 遍历列表时如何 append 顺序元素? - How do you append sequential elements when looping through a list? 如何遍历 python 中的嵌套列表? - How to iterate through a nested list in python? 如何在嵌套的 for 循环中遍历列表和字典 - How to iterate through a list and a dictionary in a nested for loop 如何获取嵌套循环函数以遍历整个(至少2个)列表变量并将其追加到新的列表输出中? - How do I get my nested loop function to iterate through the entire (at least 2) list variables and append to a new list output? 具有字符串连接的顺序嵌套列表 - Sequential nested list with string concatenation 如何将嵌套的二进制数字列表和 append 的十进制数字迭代到列表中? 例如:[[0,0,1,0,1],[1,0,0,0,1]]==[5, 17] - How do I iterate through nested lists of binary digits and append their decimal number to a list? ex: [[0,0,1,0,1],[1,0,0,0,1]]==[5, 17] 遍历嵌套列表并计算元素的平均值 - Iterate through nested list and calculate the average values of elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM