将数据转换为列表 [英] turning data into a list

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

问题描述

我使用我编写的程序合并了两个.txt文件.现在,一旦我将这两个.txt文件组合在一起,就剩下了未经排序的数据组合.

I have combined two .txt files using a program i wrote. Now once i combined these two .txt files i was left with an unsorted combination of data.

我的数据看起来像这样

Bud Abbott 51 92.3
Mary Boyd 52 91.4
Hillary Clinton 50 82.1
Don Adams 51 90.4
Jill Carney 53 76.3
Randy Newman 50 41.2

但是我希望它看起来像这样

however i want it to look like this

 ['Bud', 'Abbott', 51, 92.3]
 ['Don', 'Adams', 51, 90.4]
 ['Mary', 'Boyd', 52, 91.4]
 ['Jill', 'Carney', 53, 76.3]
 ['Hillary', 'Clinton', 50, 82.1]
 ['Randy', 'Newman', 50, 41.2]

我如何将自己拥有的变成我想要的

how do i transform what i have into what i want

通过这种方式,我必须获取第一组数据的代码看起来像这样

by the way the code i have to get my first set of data looks like this

def ReadAndMerge():

     library1=input("Enter 1st filename to read and merge:")
     with open(library1, 'r') as library1names:
          library1contents = library1names.read()
     library2=input("Enter 2nd filename to read and merge:")
     with open(library2, 'r') as library2names:
          library2contents = library2names.read()

     print(library1contents)
     print(library2contents)
     combined_contents = library1contents + library2contents  # concatenate text



     file = combined_contents
     lines = []

     for l in file:
          line = l.strip().split(" ")
          lines.append(line)


     lines.sort(key=lambda lines: lines[1])

     for line in lines:
          print(line)

print(combined_contents)
return(combined_contents)

ReadAndMerge()

ReadAndMerge()

和库1和库2分别看起来像这样

and library 1 and library 2 look like this respectively

Bud Abbott 51 92.3
Mary Boyd 52 91.4
Hillary Clinton 50 82.1



Don Adams 51 90.4
Jill Carney 53 76.3
Randy Newman 50 41.2

我如何对其进行排序并在列表中

How do i get it to be sorted and in a list

我用您的新行更新了程序,但是当我运行它时,我不断收到此错误

I updated my program with your new lines, but when i run it i keep getting this error

Traceback (most recent call last):
 File "C:/Users/Shane/Documents/Amer CISC/readandmerge.py", line 30, in <module>
ReadAndMerge()
 File "C:/Users/Shane/Documents/Amer CISC/readandmerge.py", line 23, in ReadAndMerge
lines.sort(key=lambda lines: lines[1])
 File "C:/Users/Shane/Documents/Amer CISC/readandmerge.py", line 23, in <lambda>
lines.sort(key=lambda lines: lines[1])
 IndexError: list index out of range
 >>> 

推荐答案

def getListOfLinesInFile(file): 
    file = open(file, "r")
    lines = []    
    for l in file:
        line = l.strip().split(" ")
        lines.append(line)
    file.close()
    return lines

要在程序中使用它,只需将ReadAndMerge-函数具有:

To use it in your program, just your ReadAndMerge -function with:

def ReadAndMerge():
    library1 = input("Enter 1st filename to read and merge: ")
    library1_list = getListOfLinesInFile(library1)

    library2 = input("Enter 2nd filename to read and merge: ")
    library2_list = getListOfLinesInFile(library1)

    library_merged = library1_list + library2_list
    library_merged.sort(key=lambda library_merged: library_merged[1])

    print(*library_merged)
    return library_merged

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

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