[英]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)
将匹配001
、 015
、 102
等。
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.