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


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

Building lists

Started bySeymore4Head <Seymore4Head@Hotmail.invalid>
First post2014-10-20 14:10 -0400
Last post2014-10-20 21:27 -0400
Articles 12 on this page of 32 — 11 participants

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


Contents

  Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 14:10 -0400
    Re: Building lists sohcahtoa82@gmail.com - 2014-10-20 11:18 -0700
      Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 14:21 -0400
        Re: Building lists sohcahtoa82@gmail.com - 2014-10-20 11:33 -0700
          Re: Building lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-20 20:55 +0100
    Re: Building lists MRAB <python@mrabarnett.plus.com> - 2014-10-20 20:40 +0100
      Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 15:49 -0400
        Re: Building lists Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-20 13:58 -0600
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 16:23 -0400
            Re: Building lists Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-20 14:45 -0600
              Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 16:55 -0400
            Re: Building lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-20 21:47 +0100
        Re: Building lists Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-20 20:57 +0100
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 16:24 -0400
        Re: Building lists Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-20 22:40 +0000
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 18:44 -0400
        Re: Building lists Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2014-10-21 10:55 +1100
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 20:05 -0400
        Re: Building lists Larry Hudson <orgnut@yahoo.com> - 2014-10-21 00:11 -0700
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-21 08:54 -0400
        Re: Building lists Larry Hudson <orgnut@yahoo.com> - 2014-10-21 00:40 -0700
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-21 08:55 -0400
      Re: Building lists Denis McMahon <denismfmcmahon@gmail.com> - 2014-10-20 22:30 +0000
        Re: Building lists MRAB <python@mrabarnett.plus.com> - 2014-10-20 23:57 +0100
    Re: Building lists John Gordon <gordon@panix.com> - 2014-10-20 20:48 +0000
      Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 17:25 -0400
        Re: Building lists Ian Kelly <ian.g.kelly@gmail.com> - 2014-10-20 15:34 -0600
        Re: Building lists Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-20 21:31 -0400
          Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-21 08:55 -0400
    Re: Building lists Gregory Ewing <greg.ewing@canterbury.ac.nz> - 2014-10-21 10:54 +1300
      Re: Building lists Seymore4Head <Seymore4Head@Hotmail.invalid> - 2014-10-20 17:57 -0400
    Re: Building lists Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2014-10-20 21:27 -0400

Page 2 of 2 — ← Prev page 1 [2]


#79372

FromLarry Hudson <orgnut@yahoo.com>
Date2014-10-21 00:40 -0700
Message-ID<bsydndqKI9xKkNvJnZ2dnUU7-e-dnZ2d@giganews.com>
In reply to#79328
On 10/20/2014 12:49 PM, Seymore4Head wrote:
> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB <python@mrabarnett.plus.com>
> wrote:
>
      <snip>

> Do you have to know the number of items the list will have before
> making it?
>

No.  Lists are NOT the same as arrays in other languages.  But it IS possible to create an 
initial list of a specific size:

myList = [None] * 50

This creates a 50-element list with each element set to None.  (BTW, that makes the indexes 
0-49, not 0-50.)  I have occasionally found this useful, but I emphasize it is only RARELY 
useful.  The .append() method is far more versatile.

As to your original problem:  my question to you is what is your purpose?

1)  To solve this particular problem, using Python.
   or
2)  To explore the usage of lists by applying them to this problem.

If your purpose is the first, then I agree with the advice you have already been given here -- 
dictionaries are a much better fit to this problem.

If your purpose is the second, then go ahead, have at it.  But realize that to more experienced 
Pythonistas this approach the very un-pythonic for this problem.  It would be even better to try 
multiple approaches -- lists, dictionaries, lists with dictionaries, dictionaries with lists or 
tuples...  or whatever combination you can come up with.  This will give you even more 
experience and allow you to evaluate these various techniques.

And no, I won't give you a ready-made "canned" answer.  For one thing your original description 
is much too vague.  But good luck!

      -=- Larry -=-

[toc] | [prev] | [next] | [standalone]


#79378

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-21 08:55 -0400
Message-ID<rslc4a9tfv0fb7vq5q4rq96ktat8adsgf6@4ax.com>
In reply to#79372
On Tue, 21 Oct 2014 00:40:06 -0700, Larry Hudson <orgnut@yahoo.com>
wrote:

>On 10/20/2014 12:49 PM, Seymore4Head wrote:
>> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB <python@mrabarnett.plus.com>
>> wrote:
>>
>      <snip>
>
>> Do you have to know the number of items the list will have before
>> making it?
>>
>
>No.  Lists are NOT the same as arrays in other languages.  But it IS possible to create an 
>initial list of a specific size:
>
>myList = [None] * 50
>
>This creates a 50-element list with each element set to None.  (BTW, that makes the indexes 
>0-49, not 0-50.)  I have occasionally found this useful, but I emphasize it is only RARELY 
>useful.  The .append() method is far more versatile.
>
>As to your original problem:  my question to you is what is your purpose?
>
>1)  To solve this particular problem, using Python.
>   or
>2)  To explore the usage of lists by applying them to this problem.
>
>If your purpose is the first, then I agree with the advice you have already been given here -- 
>dictionaries are a much better fit to this problem.
>
>If your purpose is the second, then go ahead, have at it.  But realize that to more experienced 
>Pythonistas this approach the very un-pythonic for this problem.  It would be even better to try 
>multiple approaches -- lists, dictionaries, lists with dictionaries, dictionaries with lists or 
>tuples...  or whatever combination you can come up with.  This will give you even more 
>experience and allow you to evaluate these various techniques.
>
>And no, I won't give you a ready-made "canned" answer.  For one thing your original description 
>is much too vague.  But good luck!
>
>      -=- Larry -=-

Also, I had no need to impose a limit on the list, but I asked that
question, just in case.

Thanks

[toc] | [prev] | [next] | [standalone]


#79344

FromDenis McMahon <denismfmcmahon@gmail.com>
Date2014-10-20 22:30 +0000
Message-ID<m242e3$mdn$2@dont-email.me>
In reply to#79325
On Mon, 20 Oct 2014 20:40:18 +0100, MRAB wrote:

> There are a number of stores, so that would be a list of stores. For
> each store you want the price of each item, so that would be a dict
> where the key is the item and the value is the price of that item. That
> means it would be a list of dicts.
> 
> Does that help?

It think it would be a dict of dicts:

shopping = { "store1" : { "item1": price1, "item2": price2, ...  }, 
"store2" : { "item3": price3, "item4": price4, ... }, ... }

-- 
Denis McMahon, denismfmcmahon@gmail.com

[toc] | [prev] | [next] | [standalone]


#79350

FromMRAB <python@mrabarnett.plus.com>
Date2014-10-20 23:57 +0100
Message-ID<mailman.15013.1413845879.18130.python-list@python.org>
In reply to#79344
On 2014-10-20 23:30, Denis McMahon wrote:
> On Mon, 20 Oct 2014 20:40:18 +0100, MRAB wrote:
>
>> There are a number of stores, so that would be a list of stores. For
>> each store you want the price of each item, so that would be a dict
>> where the key is the item and the value is the price of that item. That
>> means it would be a list of dicts.
>>
>> Does that help?
>
> It think it would be a dict of dicts:
>
> shopping = { "store1" : { "item1": price1, "item2": price2, ...  },
> "store2" : { "item3": price3, "item4": price4, ... }, ... }
>
The OP never said that the stores have names! :-)

[toc] | [prev] | [next] | [standalone]


#79337

FromJohn Gordon <gordon@panix.com>
Date2014-10-20 20:48 +0000
Message-ID<m23sfm$fto$1@reader1.panix.com>
In reply to#79320
In <obja4a5ij57rtuaivtvcuqsufhuhk3a2su@4ax.com> Seymore4Head <Seymore4Head@Hotmail.invalid> writes:

> Will Python work like this:

Python is a capable general-purpose language, so yes, it can pretty
much do anything you want.  The trick is knowing how to do it.

> Make a list of 0-50.
> Then can I add to that list so the second item will hold something
> like cheese, eggs, milk.

You want one item to have cheese, eggs, and milk?  What's the point
of calling it one "item" if it holds three things?

> Say then I want to add the price of cheese, eggs and milk.
> Say then I want to add another list of price of cheese, eggs milk from
> another store.

> Can this be done starting with just a list of numbers from 0-50?
> Please no hints, just answer directly how it is done.

Each list item could be a tuple consisting of the item name and a dict
containing the item's price at various stores, for example:

    # start with an empty list
    shopping_list = []

    # make a 'cheese' item
    cheese = ('Cheese', { 'Walmart' : 5.00,
                          'Publix': 5.50,
                          'Costco': 4.99 } )

    # add cheese to the shopping list
    shopping_list.append(cheese)

-- 
John Gordon         Imagine what it must be like for a real medical doctor to
gordon@panix.com    watch 'House', or a real serial killer to watch 'Dexter'.

[toc] | [prev] | [next] | [standalone]


#79339

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 17:25 -0400
Message-ID<glua4ald1elnllqms3mk0aghvgd8vo72av@4ax.com>
In reply to#79337
On Mon, 20 Oct 2014 20:48:54 +0000 (UTC), John Gordon
<gordon@panix.com> wrote:

>In <obja4a5ij57rtuaivtvcuqsufhuhk3a2su@4ax.com> Seymore4Head <Seymore4Head@Hotmail.invalid> writes:
>
>> Will Python work like this:
>
>Python is a capable general-purpose language, so yes, it can pretty
>much do anything you want.  The trick is knowing how to do it.
>
>> Make a list of 0-50.
>> Then can I add to that list so the second item will hold something
>> like cheese, eggs, milk.
>
>You want one item to have cheese, eggs, and milk?  What's the point
>of calling it one "item" if it holds three things?
>
>> Say then I want to add the price of cheese, eggs and milk.
>> Say then I want to add another list of price of cheese, eggs milk from
>> another store.
>
>> Can this be done starting with just a list of numbers from 0-50?
>> Please no hints, just answer directly how it is done.
>
>Each list item could be a tuple consisting of the item name and a dict
>containing the item's price at various stores, for example:
>
>    # start with an empty list
>    shopping_list = []
>
>    # make a 'cheese' item
>    cheese = ('Cheese', { 'Walmart' : 5.00,
>                          'Publix': 5.50,
>                          'Costco': 4.99 } )
>
>    # add cheese to the shopping list
>    shopping_list.append(cheese)

The thing is I am not really sure what I want.  I do know I need more
practice to find out.  Since I am taking a course now, I can't really
ask a direct question and my first example wasn't so good.

I think what I am going to have to have is a master list that keeps
track of several things and I will need to change some of them so I
know that rules out tuples.  

I need more practice examples with indexing and don't know where to
find it.

Thanks

It is hard to ask questions when you don't know all the terms yet.  I
also know that makes it even harder for you guys to answer them.  :)

[toc] | [prev] | [next] | [standalone]


#79340

FromIan Kelly <ian.g.kelly@gmail.com>
Date2014-10-20 15:34 -0600
Message-ID<mailman.15009.1413840929.18130.python-list@python.org>
In reply to#79339
On Mon, Oct 20, 2014 at 3:25 PM, Seymore4Head
<Seymore4Head@hotmail.invalid> wrote:
> I think what I am going to have to have is a master list that keeps
> track of several things and I will need to change some of them so I
> know that rules out tuples.

It sounds to me like what you really want is a list of class
instances. Define a class to contain the properties that you want to
have for one item in the list. Then create a list to contain multiple
instances of that class.

[toc] | [prev] | [next] | [standalone]


#79364

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-10-20 21:31 -0400
Message-ID<mailman.15022.1413855307.18130.python-list@python.org>
In reply to#79339
On Mon, 20 Oct 2014 17:25:31 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> declaimed the following:


>
>The thing is I am not really sure what I want.  I do know I need more
>practice to find out.  Since I am taking a course now, I can't really
>ask a direct question and my first example wasn't so good.
>
	Unfortunately, that puts your questions at the level of algorithm, not
language... Once you know the algorithm and data structures, THEN you can
figure out how to map them into Python (or Rexx, or any other language).
-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [next] | [standalone]


#79379

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-21 08:55 -0400
Message-ID<oulc4al9qint091amvg7shih1n2567tee6@4ax.com>
In reply to#79364
On Mon, 20 Oct 2014 21:31:05 -0400, Dennis Lee Bieber
<wlfraed@ix.netcom.com> wrote:

>On Mon, 20 Oct 2014 17:25:31 -0400, Seymore4Head
><Seymore4Head@Hotmail.invalid> declaimed the following:
>
>
>>
>>The thing is I am not really sure what I want.  I do know I need more
>>practice to find out.  Since I am taking a course now, I can't really
>>ask a direct question and my first example wasn't so good.
>>
>	Unfortunately, that puts your questions at the level of algorithm, not
>language... Once you know the algorithm and data structures, THEN you can
>figure out how to map them into Python (or Rexx, or any other language).

Thanks

[toc] | [prev] | [next] | [standalone]


#79341

FromGregory Ewing <greg.ewing@canterbury.ac.nz>
Date2014-10-21 10:54 +1300
Message-ID<calelhFsablU1@mid.individual.net>
In reply to#79320
Seymore4Head wrote:
> Say I want a
> Grocery list and it will have a maximum size of 50 items.

In Python it's actually easier to deal with variable-sized
lists having no maximum size. Instead of pre-creating a list
of a given size, just start with an empty list and append
things to it.

Unless there's some external reason for a size limit (e.g.
you can't fit more than 50 items in your shopping bag)
there's no need to impose limit at all. You should
certainly start without a limit and only add it later
if needed.

Some things to get you started:

items = [] # Start with an empty list
items.append("cheese") # Add some items to it
items.append("eggs")
items.append("milk")
for item in items: # Process the items
     print(item)

If you really need to limit the number of items, you
can do something like this:

def add_item(item):
     if len(items) < 50:
         items.append(item)
     else:
         raise Exception("Shopping list is full")

-- 
Greg

[toc] | [prev] | [next] | [standalone]


#79343

FromSeymore4Head <Seymore4Head@Hotmail.invalid>
Date2014-10-20 17:57 -0400
Message-ID<u81b4ahqbs95e2c6ncs302t5435gnh1pps@4ax.com>
In reply to#79341
On Tue, 21 Oct 2014 10:54:55 +1300, Gregory Ewing
<greg.ewing@canterbury.ac.nz> wrote:

>Seymore4Head wrote:
>> Say I want a
>> Grocery list and it will have a maximum size of 50 items.
>
>In Python it's actually easier to deal with variable-sized
>lists having no maximum size. Instead of pre-creating a list
>of a given size, just start with an empty list and append
>things to it.
>
>Unless there's some external reason for a size limit (e.g.
>you can't fit more than 50 items in your shopping bag)
>there's no need to impose limit at all. You should
>certainly start without a limit and only add it later
>if needed.
>
>Some things to get you started:
>
>items = [] # Start with an empty list
>items.append("cheese") # Add some items to it
>items.append("eggs")
>items.append("milk")
>for item in items: # Process the items
>     print(item)
>
>If you really need to limit the number of items, you
>can do something like this:
>
>def add_item(item):
>     if len(items) < 50:
>         items.append(item)
>     else:
>         raise Exception("Shopping list is full")

Thanks.

[toc] | [prev] | [next] | [standalone]


#79361

FromDennis Lee Bieber <wlfraed@ix.netcom.com>
Date2014-10-20 21:27 -0400
Message-ID<mailman.15021.1413854835.18130.python-list@python.org>
In reply to#79320
On Mon, 20 Oct 2014 14:10:49 -0400, Seymore4Head
<Seymore4Head@Hotmail.invalid> declaimed the following:

>
>Will Python work like this:
>I am trying to come up with an example and work to it.  Say I want a
>Grocery list and it will have a maximum size of 50 items.
>
	Python is dynamic -- why restrict the length to 50 "items" (note that
below you seem to conflate items, prices, and stores, so it is no longer
clear just what you are trying to track).

>I want to do this with one list.

	Why?

>Make a list of 0-50.
>Then can I add to that list so the second item will hold something
>like cheese, eggs, milk.
>Say then I want to add the price of cheese, eggs and milk.
>Say then I want to add another list of price of cheese, eggs milk from
>another store.
>
>Can this be done starting with just a list of numbers from 0-50?
>Please no hints, just answer directly how it is done.
>
>for store in range (50):
>	print store
>
>How can I add to store where it looks like this:
>(0,cheese, 1,eggs 2,milk , 3-50,blank for now)

	As defined, that is a tuple of 100 elements. (Lists use [ ] )

	You use an number to index into a list or tuple...

	theList[0]		is the first element of the list
	theList[1]		is the second element of the list...

	You go on to say you want to add the price... But how?

	"cheese, eggs, milk", 35.29

	{that's two elements in a list again}
or
	"cheese", 18.48, "eggs", 9.98, "milk", 5.87,

	{that's SIX elements -- and what if the next store doesn't have eggs,
will you put in an empty placeholder?

	"cheese", 9.31, "", , "milk", 2.49,
}

	I recommend you study dictionary rather than list...

	{ "store1" : {"cheese" : 18.48, "eggs" : 9.98, "milk" : 5.87},
		"store2" : {"cheese" : 9.31, "milk" : 2.49}	}


>	

-- 
	Wulfraed                 Dennis Lee Bieber         AF6VN
    wlfraed@ix.netcom.com    HTTP://wlfraed.home.netcom.com/

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

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


csiph-web