将列表分组为特定组 [英] Grouping Lists into specific groups

查看:84
本文介绍了将列表分组为特定组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能将清单转换成特定的组,以后再以表格格式将它们放置到这些组中.

I'm wondering if it is possible to convert the listings into a specific groups to which I could place them in a table format later on.

这是我需要分组的输出,我将它们转换为列表,以便可以轻松地以表格方式将其划分.

This is the output that I needed to group, I converted them into a list so that I could easily divide them in table manner.

f=open("sample1.txt", "r")
f.read()

这是输出:

'0245984300999992018010100004+14650+121050FM-12+004699999V0203001N00101090001CN008000199+02141+01971101171ADDAY141021AY241021GA1021+006001081GA2061+090001021GE19MSL   +99999+99999GF106991021999006001999999KA1120N+02111MD1210141+9999MW1051REMSYN10498430 31558 63001 10214 20197 40117 52014 70544 82108 333 20211 55062 56999 59012 82820 86280 555 60973=\n'

这是我已经完成的工作.我设法将其更改为一个列表,该列表产生了以下输出:

Here's what I have done already. I have managed to change it into a list which resulted in this output:

with open('sample1.txt', 'r') as file:
data = file.read().replace('\n', '')
print (list(data))

输出:

['0','2','4','5','9','8','4','3','0','0','9','9 ','9','9','9','2','0','1','8','0','1','0','1','0', '0','0','0','4','+','1','4','6','5','0','+','1','2 ','1','0','5','0','F','M','-','1','2','+','0','0', '4','6','9','9','9','9','9','V','0','2','0','3','0 ','0','1','N','0','0','1','0','1','0','9','0','0', '0','1','C','N','0','0','8','0','0','0','1','9','9 ','+','0','2','1','4','1','+','0','1','9','7','1', '1','0','1','1','7','1','A','D','D','A','Y','1','4 ','1','0','2','1','A','Y','2','4','1','0','2','1', 'G','A','1','0','2','1','+','0','0','6','0','0','1 ','0','8','1','G','A','2','0','6','1','+','0','9', '0','0','0','1','0','2','1','G','E','1','9','M','S' ','L',',',',',','+','9','9','9','9','9','+','9','9' ,"9","9","9","G","F","1","0","6","9","9","1","0","2","1","9","9","9","0","0","6","0","0","1","9","9','9','9','9','9','K','A','1','1','2','0','N','+' ,'0','2','1','1','1','M','D','1','2','1','0','1',' 4','1','+','9','9','9','9','M','W','1','0','5','1' ,"R","E","M","S","Y","N","1","0","4","9","8","4","3','0',',','3','1','5','5','8',','6','3','0','0',' 1',',','1','0','2','1','4','','2','0','1','9','7',' ','4','0','1','1','7',',','5','2','0','1','4',',','7 ','0','5','4','4','','8','2','1','0','8',','3','3 ','3','','2','0','2','1','1',','5','5','0','6','2 ",","5","6","9","9","9",","5","9","0","1","2"," ,'8','2','8','2','0',',','8','6','2','8','0','','5' ,'5','5',',','6','0','9','7','3','=']

['0', '2', '4', '5', '9', '8', '4', '3', '0', '0', '9', '9', '9', '9', '9', '2', '0', '1', '8', '0', '1', '0', '1', '0', '0', '0', '0', '4', '+', '1', '4', '6', '5', '0', '+', '1', '2', '1', '0', '5', '0', 'F', 'M', '-', '1', '2', '+', '0', '0', '4', '6', '9', '9', '9', '9', '9', 'V', '0', '2', '0', '3', '0', '0', '1', 'N', '0', '0', '1', '0', '1', '0', '9', '0', '0', '0', '1', 'C', 'N', '0', '0', '8', '0', '0', '0', '1', '9', '9', '+', '0', '2', '1', '4', '1', '+', '0', '1', '9', '7', '1', '1', '0', '1', '1', '7', '1', 'A', 'D', 'D', 'A', 'Y', '1', '4', '1', '0', '2', '1', 'A', 'Y', '2', '4', '1', '0', '2', '1', 'G', 'A', '1', '0', '2', '1', '+', '0', '0', '6', '0', '0', '1', '0', '8', '1', 'G', 'A', '2', '0', '6', '1', '+', '0', '9', '0', '0', '0', '1', '0', '2', '1', 'G', 'E', '1', '9', 'M', 'S', 'L', ' ', ' ', ' ', '+', '9', '9', '9', '9', '9', '+', '9', '9', '9', '9', '9', 'G', 'F', '1', '0', '6', '9', '9', '1', '0', '2', '1', '9', '9', '9', '0', '0', '6', '0', '0', '1', '9', '9', '9', '9', '9', '9', 'K', 'A', '1', '1', '2', '0', 'N', '+', '0', '2', '1', '1', '1', 'M', 'D', '1', '2', '1', '0', '1', '4', '1', '+', '9', '9', '9', '9', 'M', 'W', '1', '0', '5', '1', 'R', 'E', 'M', 'S', 'Y', 'N', '1', '0', '4', '9', '8', '4', '3', '0', ' ', '3', '1', '5', '5', '8', ' ', '6', '3', '0', '0', '1', ' ', '1', '0', '2', '1', '4', ' ', '2', '0', '1', '9', '7', ' ', '4', '0', '1', '1', '7', ' ', '5', '2', '0', '1', '4', ' ', '7', '0', '5', '4', '4', ' ', '8', '2', '1', '0', '8', ' ', '3', '3', '3', ' ', '2', '0', '2', '1', '1', ' ', '5', '5', '0', '6', '2', ' ', '5', '6', '9', '9', '9', ' ', '5', '9', '0', '1', '2', ' ', '8', '2', '8', '2', '0', ' ', '8', '6', '2', '8', '0', ' ', '5', '5', '5', ' ', '6', '0', '9', '7', '3', '=']

我的目标是将它们分为以下几类:

My goal is to group them into something like these:

0245,984300,99999,2018,01,01,0000,4,+1.... 

属于每一列的位数是预先确定的,例如,第一列总有4位,第二列总有6位,依此类推.

The number of digits belonging to each column is predetermined, for example there are always 4 digits for the first column and 6 for the second, and so on.

我当时正在考虑将它们串联起来.但是我不确定是否有可能.

I was thinking of concatenating them. But I'm not sure if it would be possible.

推荐答案

您可以使用operator.itemgetter

from operator import itemgetter

g = itemgetter(slice(0, 4), slice(4, 10))
with open('sample1.txt') as file:
    for line in file:
        print(g(line))

甚至更好的是,您可以使用zip和itertools.accumulate动态制作切片:

Or even better you can make the slices dynamically using zip and itertools.accumulate:

indexes = [4, 6, ...]
g = itemgetter(*map(slice, *map(accumulate, zip([0]+indexes, indexes))))

然后像以前一样

这篇关于将列表分组为特定组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆