Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103466 > unrolled thread

Re: Nested List question

Started by<grsmith@atlanticbb.net>
First post2016-02-24 17:33 -0500
Last post2016-02-24 17:33 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Nested List question <grsmith@atlanticbb.net> - 2016-02-24 17:33 -0500

#103466 — Re: Nested List question

From<grsmith@atlanticbb.net>
Date2016-02-24 17:33 -0500
SubjectRe: Nested List question
Message-ID<mailman.109.1456353212.20994.python-list@python.org>
Erik,

Works perfectly, thanks much !!!
Mark, I am tied to data structure.

for address in addresses:
    if isinstance(address, str):
        apt = None
        print(address, apt)
    else:
        address, apt = address
        print(address, apt)


99 First Street None
33 Broadway Avenue Apt 303
1 Park Avenue None


-----Original Message----- 
From: Erik
Sent: Wednesday, February 24, 2016 4:28 PM
To: grsmith@atlanticbb.net ; python-list
Subject: Re: Nested List question

On 24/02/16 20:59, grsmith@atlanticbb.net wrote:
> Can you have a phython list like:
> ['George',
> 'Soros',
> ['99 First Street',
>    '33 Broadway Avenue', ['Apt 303'],
>    '1 Park Avenue'],
>    'New York', 'NY']
>
> In other words how do you correctly nest the
> ['Apt 303'] so it goes with 33 Broadway Avenue.

The way to keep those particular items together is to have something like:

data = \
['George', 'Soros',
   [ '99 First Street',
     ['33 Broadway Avenue', 'Apt 303'],
     '1 Park Avenue'
   ],
   'New York', 'NY'
]

And then, guessing from the structure of your list, you'd do something like:

forename, surname, addresses, city, state = data

And then, something like:

for address in addresses:
   if type(address) == StringType:
     apt = None
   else:
     address, apt = address

That should work to pick apart with what you have presented above, but I
think you'd be better off presenting the data with a bit more structure
in the first place.

> Also, I tried several ways and could not figure out
> how to get the ['Apt 303'] out of the list. How can
> you do that.

It would be much easier to work out what you mean by this if you'd have
actually said what you'd tried and what you had expected to happen and
why what did happen wasn't right for you.

E. 

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web