对来自txt fi的数据进行分组时遇到问题

2024-09-30 14:25:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个初学者编码和项目,我有需要我分类一个文本文件。 我打开的txt文件是这样的: (这并不是txt文件看起来的样子,只是当我复制并通过它时,它看起来太乱了。因为某种原因,在另一个专栏里只写了“地图”一词)

MAG     UTC DATE-TIME             LAT         LON       DEPTH    Region
 4.3    2014/03/12 20:16:59       25.423     -109.730   10.0     GULF OF CALIFORNIA                 
 5.2    2014/03/12 20:09:55       36.747      144.050   24.2     JAPAN
 5.0    2014/03/12 20:08:25       35.775      141.893   24.5     JAPAN
 4.8    2014/03/12 19:59:01       38.101      142.840   17.6     Japan
 4.6    2014/03/12 19:55:28       37.400      142.384   24.7     JAPAN
 5.0    2014/03/12 19:45:19       -6.187      154.385   62.0     GUINEA

我希望输出是这样的:

[[japan,'4.3','5.2','5.0','4.8','4.6'],[Gulf of California,4.3],[Guinea,5.0]]

我当前的编码: (第一个for循环中的vlist[7:]给出了区域名称,第二个for循环中的j[1]给出了mactitude编号。)

def myOpen(filepointer):
    header = filepointer.readline()
    regions = []#gathers up all the names of the regions without repeating them
    maglist = []#matchs with naems and numbers
    filelines = []#list of lines in txt file


    for aline in filepointer:#reades each line
       vlist = aline.split()#turns lines into lists
       filelines.append(vlist)
       if not vlist[7:] in regions:#makes list of names without repeat
            regions.append(vlist[7:])
            regions.sort()

   for j in filelines:#gets each file line
        for names in regions:#each name
           if names == j[7:]:
               num = j[1]
               names.append(float(num))
               mags.append(names)
   return maglist
def main():
    myFile = open('earthquakes.txt','r')
    quakes = myOpen(myFile)
    myFile.close()
    print(quakes)

main()

给出以下输出:

[[japan,'4.3'],[Gulf of California,4.3],[Guinea,5.0]]

我想知道为什么它只得到其他区域出现的第一个震级数,而不是其他区域。你知道吗


Tags: ofintxt区域编码fornamesmyfile
1条回答
网友
1楼 · 发布于 2024-09-30 14:25:49

给你:使用itertools.groupbylambdamapstr.splitstr.lowerstr.join

如果您的文件如下所示:

MAG     UTC DATE-TIME             LAT         LON      DEPTH    Region
4.3    2014/03/12 20:16:59       25.423     -109.730   10.0     GULF OF CALIFORNIA
5.2    2014/03/12 20:09:55       36.747      144.050   24.2     JAPAN
5.0    2014/03/12 20:08:25       35.775      141.893   24.5     JAPAN
4.8    2014/03/12 19:59:01       38.101      142.840   17.6     Japan
4.6    2014/03/12 19:55:28       37.400      142.384   24.7     JAPAN
5.0    2014/03/12 19:45:19       -6.187      154.385   62.0     GUINEA

以下是工作代码:

>>> import itertools
>>> f = open('file.txt')
>>> [[" ".join(x),list(map(lambda z:z[0],list(y)))] for x,y in itertools.groupby(sorted(list(map(str.split,map(str.lower,list(f)[1:]))),key=lambda x:" ".join(x[6:])),key=lambda x:x[6:])]
[['guinea', ['5.0']], ['gulf of california', ['4.3']], ['japan', ['5.2', '5.0', '4.8', '4.6']]]

我来解释一下:

>>> f = open('file.txt')
>>> k = list(map(str.lower,list(f)[1:]))  # convert all lines to lower case and leave 1st line
>>> k
['4.3    2014/03/12 20:16:59       25.423     -109.730   10.0     gulf of california\n', '5.2    2014/03/12 20:09:55       36.747      144.050   24.2     japan\n', '5.0    2014/03/12 20:08:25       35.775      141.893   24.5     japan\n', '4.8    2014/03/12 19:59:01       38.101      142.840   17.6     japan\n', '4.6    2014/03/12 19:55:28       37.400      142.384   24.7     japan\n', '5.0    2014/03/12 19:45:19       -6.187      154.385   62.0     guinea\n']
>>> k = list(map(str.split,k))   # it will split the lines on whitespaces
>>> k
[['4.3', '2014/03/12', '20:16:59', '25.423', '-109.730', '10.0', 'gulf', 'of', 'california'], ['5.2', '2014/03/12', '20:09:55', '36.747', '144.050', '24.2', 'japan'], ['5.0', '2014/03/12', '20:08:25', '35.775', '141.893', '24.5', 'japan'], ['4.8', '2014/03/12', '19:59:01', '38.101', '142.840', '17.6', 'japan'], ['4.6', '2014/03/12', '19:55:28', '37.400', '142.384', '24.7', 'japan'], ['5.0', '2014/03/12', '19:45:19', '-6.187', '154.385', '62.0', 'guinea']] 
>>> k = sorted(k,key = lambda x:" ".join(x[6:]))  # it will sort the k on Region
>>> k
[['5.0', '2014/03/12', '19:45:19', '-6.187', '154.385', '62.0', 'guinea'], ['4.3', '2014/03/12', '20:16:59', '25.423', '-109.730', '10.0', 'gulf', 'of', 'california'], ['5.2', '2014/03/12', '20:09:55', '36.747', '144.050', '24.2', 'japan'], ['5.0', '2014/03/12', '20:08:25', '35.775', '141.893', '24.5', 'japan'], ['4.8', '2014/03/12', '19:59:01', '38.101', '142.840', '17.6', 'japan'], ['4.6', '2014/03/12', '19:55:28', '37.400', '142.384', '24.7', 'japan']]
>>> [[" ".join(x),list(map(lambda z:z[0],list(y)))] for x,y in itertools.groupby(k,key = lambda x:x[6:])]
[['guinea', ['5.0']], ['gulf of california', ['4.3']], ['japan', ['5.2', '5.0', '4.8', '4.6']]]

相关问题 更多 >