Need better way to unpack a list

Discussion in 'Python' started by Nx, Aug 17, 2005.

  1. Nx

    Nx Guest

    Is there a better solution to following problem :

    I want to unpack a list into variables
    the list is created at runtime and I do not know
    how many items there will be , but I know not more than 25.
    The list unpacks into variables which are prev. defined as
    line1,line2....line25 which are actually the names of lineedit fields in
    a Qt gui application and which need to be populated.
    I currently use following to achieve that :

    c=0
    while c < len(mylinelist):
    c = c + 1
    if c==1:
    self.line1.setText(mylinelist[c])
    if c==2 :
    self.line2.setText(mylinelist[c])
    .
    .
    .
    if c==25:
    self.line25.setText(mylinelist[c])

    I rather have someting like
    pseudo code follows:

    self.line+"c"+.setText(mylinelist[c])

    How to do that ?

    Nx
     
    Nx, Aug 17, 2005
    #1
    1. Advertising

  2. Nx

    Peter Otten Guest

    Nx wrote:

    > Is there a better solution to following problem :
    >
    > I want to unpack a list into variables
    > the list is created at runtime and I do not know
    > how many items there will be , but I know not more than 25.
    > The list unpacks into variables which are prev. defined as
    > line1,line2....line25 which are actually the names of lineedit fields in
    > a Qt gui application and which need to be populated.
    > I currently use following to achieve that :
    >
    > c=0
    > while c < len(mylinelist):
    > c = c + 1
    > if c==1:
    > self.line1.setText(mylinelist[c])
    > if c==2 :
    > self.line2.setText(mylinelist[c])
    > .
    > .
    > .
    > if c==25:
    > self.line25.setText(mylinelist[c])
    >
    > I rather have someting like
    > pseudo code follows:
    >
    > self.line+"c"+.setText(mylinelist[c])
    >
    > How to do that ?


    for index, value in enumerate(mylinelist):
    getattr(self, "line%d" % (index + 1)).setText(value)

    A better approach would be to create a list of lineedit fields in the
    __init__() method instead of (or in addition to) the individual lineNNN
    attributes and then just write

    for edit, value in zip(self.lineedits, mylinelist):
    edit.setText(value)

    If you want to play it safe you might also want to clear the extra
    lineedits:

    from itertools import repeat, chain, izip
    for edit, value in izip(self.lineedits, chain(mylinelist, repeat(""))):
    edit.setText(value)

    Peter
     
    Peter Otten, Aug 17, 2005
    #2
    1. Advertising

  3. On 8/17/05, Nx <> wrote:
    > I want to unpack a list into variables
    > the list is created at runtime and I do not know
    > how many items there will be , but I know not more than 25.
    > The list unpacks into variables which are prev. defined as
    > line1,line2....line25 which are actually the names of lineedit fields in
    > a Qt gui application and which need to be populated.
    > I currently use following to achieve that :
    >
    > c=0
    > while c < len(mylinelist):
    > c = c + 1
    > if c==1:
    > self.line1.setText(mylinelist[c])
    > if c==2 :
    > self.line2.setText(mylinelist[c])
    > .
    > .
    > .
    > if c==25:
    > self.line25.setText(mylinelist[c])
    >
    > I rather have someting like
    > pseudo code follows:
    >
    > self.line+"c"+.setText(mylinelist[c])
    >
    > How to do that ?


    Lift would be much easier if you held your 'linenn' lineedit fields in
    a list instead. Is that possible? If so, you could do something like
    (untested):

    for index, line in enumerate(mylinelist):
    self.lines[index].setText(line)

    --
    Cheers,
    Simon B,
    ,
    http://www.brunningonline.net/simon/blog/
     
    Simon Brunning, Aug 17, 2005
    #3
  4. Nx

    Nx Guest

    > for index, value in enumerate(mylinelist):
    > getattr(self, "line%d" % (index + 1)).setText(value)


    Thanks ! This was the line which did it ....
    The correct code snippet now reads :

    # set the lineedits of the QT gui
    for index,value in enumerate(self.mylinelist):
    getattr(self, "lineEdit%d" % (index + 1)).setText(self.tr(value))


    Nx
     
    Nx, Aug 17, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Laax
    Replies:
    2
    Views:
    446
  2. srinivasan srinivas
    Replies:
    0
    Views:
    250
    srinivasan srinivas
    Sep 14, 2008
  3. Gary Herron
    Replies:
    2
    Views:
    315
    Tim Chase
    Sep 14, 2008
  4. Ian Kelly
    Replies:
    0
    Views:
    178
    Ian Kelly
    Oct 26, 2012
  5. Replies:
    2
    Views:
    57
    Mark H Harris
    May 13, 2014
Loading...

Share This Page