如何在Python类中创建数据结构

时间:2017-02-26 16:47:07

标签: python class

class CD(object):
        def __init__(self,id,name,singer):
                self._id = id
                self.n = name
                self.s = singer

        def get_info(self):
                info = 'self._id'+':'+ ' self.n' +' self.s'
                return info

class collection(object):
        def __init__(self):
                cdfile = read('CDs.txt','r')

我有一个'CDs.txt'文件,它有一个元组列表,如下所示:

[
("Shape of you", "Ed Sheeran"),
("Shape of you", "Ed Sheeran"),
("I don't wanna live forever", "Zayn/Taylor Swift"),
("Fake Love", "Drake"),
("Starboy", "The Weeknd"),

......
]

现在在我的集合类中,我想为列表中的每个元组创建一个CD对象,并将它们保存在数据结构中。我希望每个元组都有一个唯一的ID号,它们无关紧要,它们需要有不同的id ....有谁可以帮我这个?

1 个答案:

答案 0 :(得分:1)

您可以使用enumerate进行简单循环。

Private Sub Create_Click()


  Dim FileExtStr As String
  Dim FileFormatNum As Long
  Dim dUMMY1 As Workbook
  Dim Destwb As Workbook
  Dim sh As Worksheet
  Dim DateString As String
  Dim FolderName As String

  Dim dt As String, wbNam As String



  With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
  End With

 'Copy every sheet from the workbook with this macro
  Set dUMMY1 = ThisWorkbook

 'Create new folder to save the new files in
 DateString = Format(Now, "yyyy-mm-dd hh-mm-ss")
 FolderName = dUMMY1.Path & "\" & dUMMY1.Name & " " & DateString
 MkDir FolderName

 'Copy every visible sheet to a new workbook
 For Each sh In dUMMY1.Worksheets

    'If the sheet is visible then copy it to a new workbook
    If sh.Visible = -1 Then
        sh.Copy

        'Set Destwb to the new workbook
        Set Destwb = ActiveWorkbook

        'Determine the Excel version and file extension/format
        With Destwb
            If Val(Application.Version) < 12 Then
                'You use Excel 97-2003
                FileExtStr = ".xls": FileFormatNum = -4143
            Else
                'You use Excel 2007-2013
                If dUMMY1.Name = .Name Then
                    MsgBox "Your answer is NO in the security dialog"
                    GoTo GoToNextSheet
                Else
                    Select Case dUMMY1.FileFormat
                    Case 51: FileExtStr = ".xlsx": FileFormatNum = 51
                    Case 52:
                        If .HasVBProject Then
                            FileExtStr = ".xlsm": FileFormatNum = 52
                        Else
                            FileExtStr = ".xlsx": FileFormatNum = 51
                        End If
                    Case 56: FileExtStr = ".xls": FileFormatNum = 56
                    Case Else: FileExtStr = ".xlsb": FileFormatNum = 50
                    End Select
                End If
            End If
        End With

        'Change all cells in the worksheet to values if you want
        If Destwb.Sheets(1).ProtectContents = False Then
            With Destwb.Sheets(1).UsedRange
                .Cells.Copy
                .Cells.PasteSpecial xlPasteValues
                .Cells(1).Select
            End With
            Application.CutCopyMode = False
        End If


        'Save the new workbook and close it
         With Destwb
            .SaveAs FolderName _
                  & "\" & Destwb.Sheets(1).Name & FileExtStr, _
                    FileFormat:=FileFormatNum
            .Close False
        End With


    End If
GoToNextSheet:
  Next sh





 MsgBox "You can find the files in " & FolderName

 With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .Calculation = xlCalculationAutomatic
 End With



End Sub


Private Sub Send_Click()

  Dim OutApp As Object
  Dim OutMail As Object

  Application.OnTime TimeValue("15:45:00"), "Auto_Update"
  Set OutApp = CreateObject("Outlook.Application")
  Set OutMail = OutApp.CreateItem(0)

  On Error Resume Next
  With OutMail
    .to = "abc@def.com"
    .CC = ""
    .BCC = ""
    .Subject = "Testing "
    .Body = ""
    .Attachments.Add ActiveWorkbook.FullName

    .Send
  End With
  On Error GoTo 0

 Set OutMail = Nothing
 Set OutApp = Nothing
End Sub

现在,您已将所有CD放在一个漂亮,干净的列表中

如果您的文件只是# I don't know what you mean by 'file has list of tuples', # so I assume you loaded it somehow tuples = [("Shape of you", "Ed Sheeran"), ("Shape of you", "Ed Sheeran"), ("I don't wanna live forever", "Zayn/Taylor Swift"), ("Fake Love", "Drake"), ("Starboy", "The Weeknd")] cds = [] for i, (title, author) in enumerate(tuples): cds.append(CD(i, title, author)) 格式的列表,那么您只需评估其内容即可加载它:

[('title', 'author')]