【问题标题】:Python rearranging a list into a tuplePython将列表重新排列成元组
【发布时间】:2013-11-27 13:38:34
【问题描述】:

我有以下代码:

def ex1():

b= input("Please enter a file name to be opened: ")
a = (b+".txt")

c =[]

def employee(lanme,oname,num,title,salary):
    c.append(employee)

def readfile(a):
    try:
        data =[]
        check = open(a, 'r')
        line =check.readlines()
        for items in line:
            breakup= items.split()
            data.append(breakup)
    except IOError as e :
        print("Failed to open", fileName)




readfile(a)

ex1()

它所做的基本上是读取一个包含员工信息的文本文件。 例如: 文本文件进来的格式是:(num,salary,position,oname,lname)

15674 24000 Manager Gregory the 1st John , 
14522 24500 Team Leader Baker George ,
22422 24352 Crew member house bob

我需要从文本文件中获取信息并将其重新排列为 def employee 函数中的格式。 这是(lanme,oname,num,title,salary)来自原始版本(num,salary,position,oname,lname)。

我知道这样做的方法,但它需要存储为一个元组,并且可以单独或作为一个整体访问。

如果这个问题似乎没有得到很好的解释,我深表歉意,但我已尽力而为。

【问题讨论】:

    标签: python tuples


    【解决方案1】:
    for items in data:
      id, salary, title, lastname, firstname = items
      employee(lastna,e, firstname, id, title, salary)
    

    【讨论】:

    • 我很欣赏快速反馈,但是在我实现循环并确保变量正确之后,我有一个 valuerror,它说它试图解压缩太多东西。它需要 5 个,但有 5 个。我还根据反馈编辑了我的员工职能。
    【解决方案2】:

    除了 Alex 所说的之外,您的 employee 函数肯定没有按照您的意愿行事。现在它将对自身的引用附加到c,所以c 将是一个类似[<employee function>, <employee function>, ...] 的列表 如果你想要一个用元组表示的员工,让函数返回元组。

    def employee(lanme, oname, num,title,salary):
        return (lanme, oname, num, title, salary)
    

    编辑:

    为了解释 Alex 的回答,我们将列表解压缩为变量,并从解压缩的列表中创建一个新元组。这是一个简单的例子

    mylist = [0, 1, 2, 3, 4]
    a, b, c, d, e = mylist # a=0, b=1, ...
    mytuple = (e, d, c, b, a) # mytuple = (4, 3, 2, 1, 0)
    

    通过这些更改,您的脚本将如下所示

    b = input("Please enter a file name to be opened: ")
    a = b + '.txt'
    
    def employee(lname, oname, num, title, salary):
        return (lname, oname, num, title, salary)
    
    def readfile(a):
    
        # Instead of the try/except block
        # you can use a with statement
        with open(a, 'r') as check:
            data = []
    
            # You can also skip the readlines()
            # and iterate over the file directly
            for items in check:
    
                # This is unpacking, see the note above
                # Also, based on your example file **this won't work**, see below note
                num, salary, position, oname, lname = items.split()
                newemployee = employee(lname, oname, num, title, salary)
                data.append(newemployee)
        return data
    
    myemployees = readfile(a)
    

    最后,items.split() 也可能不是你想要的。 如果你有15674 24000 Manager Gregory the 1st John , 行,那么使用split 会给你['15674', '24000', 'Manager', 'Gregory', 'the', '1st', 'John' ','] 而你想要的列表是`['15674', '24000', 'Manager', 'Gregory the 1st', 'John'] .

    获取该列表取决于您的文件结构,这似乎不一致,因此很难。用空格分隔的字段,有些字段是多个单词,有时尾随逗号。

    【讨论】:

    • 我已经关注了反馈,但我是 python 新手,我不确定如何实施/理解回复的哪些部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多