Re: Building lists
On 10/20/2014 12:49 PM, Seymore4Head wrote: On Mon, 20 Oct 2014 20:40:18 +0100, MRAB pyt...@mrabarnett.plus.com wrote: snip Do you have to know the number of items the list will have before making it? No, it is not necessary, 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 That creates a 50-element list with each element set to None. (BTW, the indexes are from 0-49, not 0-50.) I have found this occasionally useful, but I'll emphasize, it's 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, 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 and use this for your exploration. But realize that to more experienced Pythonistas this would be a very un-pythonic approach. Even better would be to try multiple approaches -- lists, dictionaries, lists with dictionaries, dictionaries with lists or tuples... And any other combinations you can come up with. This will give you even more experience, and allow you to evaluate the different approaches. And no, I will not give you a ready-made canned answer. For one thing, your description is too vague to effectively do that. Good luck. -=- Larry -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On 10/20/2014 12:49 PM, Seymore4Head wrote: On Mon, 20 Oct 2014 20:40:18 +0100, MRAB pyt...@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 -=- -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Tue, 21 Oct 2014 00:11:38 -0700, Larry Hudson org...@yahoo.com wrote: On 10/20/2014 12:49 PM, Seymore4Head wrote: On Mon, 20 Oct 2014 20:40:18 +0100, MRAB pyt...@mrabarnett.plus.com wrote: snip Do you have to know the number of items the list will have before making it? No, it is not necessary, 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 That creates a 50-element list with each element set to None. (BTW, the indexes are from 0-49, not 0-50.) I have found this occasionally useful, but I'll emphasize, it's 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, 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 and use this for your exploration. But realize that to more experienced Pythonistas this would be a very un-pythonic approach. Even better would be to try multiple approaches -- lists, dictionaries, lists with dictionaries, dictionaries with lists or tuples... And any other combinations you can come up with. This will give you even more experience, and allow you to evaluate the different approaches. And no, I will not give you a ready-made canned answer. For one thing, your description is too vague to effectively do that. Good luck. -=- Larry -=- The concept I was asking about was a master list with my example of 1,2,3 as a index for the second and third items. It was suggested to make my task easier. It turns out that it didn't. Thanks for all the suggestions, though. -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Tue, 21 Oct 2014 00:40:06 -0700, Larry Hudson org...@yahoo.com wrote: On 10/20/2014 12:49 PM, Seymore4Head wrote: On Mon, 20 Oct 2014 20:40:18 +0100, MRAB pyt...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 21:31:05 -0400, Dennis Lee Bieber wlfr...@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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Monday, October 20, 2014 11:13:17 AM UTC-7, Seymore4Head wrote: I haven't had a lot of practice doing this. If anyone knows of a site I would appreciate it. 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. I want to do this with one list. 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) Please no hints, just answer directly how it is done. That's pretty rude. We're not going to do your homework for you. -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 11:18:26 -0700 (PDT), sohcahto...@gmail.com wrote: On Monday, October 20, 2014 11:13:17 AM UTC-7, Seymore4Head wrote: I haven't had a lot of practice doing this. If anyone knows of a site I would appreciate it. 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. I want to do this with one list. 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) Please no hints, just answer directly how it is done. That's pretty rude. We're not going to do your homework for you. I said please -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Monday, October 20, 2014 11:23:36 AM UTC-7, Seymore4Head wrote: On Mon, 20 Oct 2014 11:18:26 -0700 (PDT), sohcahtoa82 wrote: On Monday, October 20, 2014 11:13:17 AM UTC-7, Seymore4Head wrote: I haven't had a lot of practice doing this. If anyone knows of a site I would appreciate it. 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. I want to do this with one list. 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) Please no hints, just answer directly how it is done. That's pretty rude. We're not going to do your homework for you. I said please Oh shit! It's the magic word! I better get right on it! -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On 2014-10-20 19:10, Seymore4Head wrote: I haven't had a lot of practice doing this. If anyone knows of a site I would appreciate it. 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. I want to do this with one list. 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) The grocery list would be a list of the things you want to buy. 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? -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 20:40:18 +0100, MRAB pyt...@mrabarnett.plus.com wrote: On 2014-10-20 19:10, Seymore4Head wrote: I haven't had a lot of practice doing this. If anyone knows of a site I would appreciate it. 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. I want to do this with one list. 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) The grocery list would be a list of the things you want to buy. 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? No. I am pretty new to Python. For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? Do you have to know the number of items the list will have before making it? -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On 20/10/2014 19:33, sohcahto...@gmail.com wrote: Oh shit! It's the magic word! I better get right on it! While you're at it would you please access this list via https://mail.python.org/mailman/listinfo/python-list or read and action this https://wiki.python.org/moin/GoogleGroupsPython to prevent us seeing double line spacing and single line paragraphs, thanks. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head Seymore4Head@hotmail.invalid wrote: For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? You mean like this? the_list = ['first_item'] the_list.append('second_item') the_list ['first_item', 'second_item'] -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On 20/10/2014 20:49, Seymore4Head wrote: On Mon, 20 Oct 2014 20:40:18 +0100, MRAB pyt...@mrabarnett.plus.com wrote: On 2014-10-20 19:10, Seymore4Head wrote: I haven't had a lot of practice doing this. If anyone knows of a site I would appreciate it. 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. I want to do this with one list. 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) The grocery list would be a list of the things you want to buy. 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? No. I am pretty new to Python. For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? Do you have to know the number of items the list will have before making it? Python doesn't have commands and no. Perhaps you'd care to (re)read the tutorial. -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 20:57:52 +0100, Mark Lawrence breamore...@yahoo.co.uk wrote: On 20/10/2014 20:49, Seymore4Head wrote: On Mon, 20 Oct 2014 20:40:18 +0100, MRAB pyt...@mrabarnett.plus.com wrote: On 2014-10-20 19:10, Seymore4Head wrote: I haven't had a lot of practice doing this. If anyone knows of a site I would appreciate it. 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. I want to do this with one list. 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) The grocery list would be a list of the things you want to buy. 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? No. I am pretty new to Python. For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? Do you have to know the number of items the list will have before making it? Python doesn't have commands and no. Perhaps you'd care to (re)read the tutorial. Thank you -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 13:58:46 -0600, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head Seymore4Head@hotmail.invalid wrote: For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? You mean like this? the_list = ['first_item'] the_list.append('second_item') the_list ['first_item', 'second_item'] a=(1,2,3) b=(Red, Green, Blue) c=(a.b,c) d=(1,red,a 2,green,b 3,blue,c) Something like that. -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, Oct 20, 2014 at 2:23 PM, Seymore4Head Seymore4Head@hotmail.invalid wrote: On Mon, 20 Oct 2014 13:58:46 -0600, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head Seymore4Head@hotmail.invalid wrote: For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? You mean like this? the_list = ['first_item'] the_list.append('second_item') the_list ['first_item', 'second_item'] a=(1,2,3) b=(Red, Green, Blue) c=(a.b,c) d=(1,red,a 2,green,b 3,blue,c) Something like that. Those are tuples, not lists. Are you trying to create a list of tuples from a, b, and c? If so, then zip does what you want: d = list(zip(a, b, c)) d [(1, 'Red', 'a'), (2, 'Green', 'b'), (3, 'Blue', 'c')] Or do you want all those elements merged into a single list? d = list(sum(zip(a, b, c), ())) d [1, 'Red', 'a', 2, 'Green', 'b', 3, 'Blue', 'c'] -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
In obja4a5ij57rtuaivtvcuqsufhuhk3a...@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 gor...@panix.comwatch 'House', or a real serial killer to watch 'Dexter'. -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On 20/10/2014 21:23, Seymore4Head wrote: On Mon, 20 Oct 2014 13:58:46 -0600, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head Seymore4Head@hotmail.invalid wrote: For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? You mean like this? the_list = ['first_item'] the_list.append('second_item') the_list ['first_item', 'second_item'] a=(1,2,3) b=(Red, Green, Blue) c=(a.b,c) d=(1,red,a 2,green,b 3,blue,c) Something like that. https://docs.python.org/3/library/functions.html#zip -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 14:45:19 -0600, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Oct 20, 2014 at 2:23 PM, Seymore4Head Seymore4Head@hotmail.invalid wrote: On Mon, 20 Oct 2014 13:58:46 -0600, Ian Kelly ian.g.ke...@gmail.com wrote: On Mon, Oct 20, 2014 at 1:49 PM, Seymore4Head Seymore4Head@hotmail.invalid wrote: For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? You mean like this? the_list = ['first_item'] the_list.append('second_item') the_list ['first_item', 'second_item'] a=(1,2,3) b=(Red, Green, Blue) c=(a.b,c) d=(1,red,a 2,green,b 3,blue,c) Something like that. Those are tuples, not lists. Are you trying to create a list of tuples from a, b, and c? If so, then zip does what you want: d = list(zip(a, b, c)) d [(1, 'Red', 'a'), (2, 'Green', 'b'), (3, 'Blue', 'c')] Or do you want all those elements merged into a single list? d = list(sum(zip(a, b, c), ())) d [1, 'Red', 'a', 2, 'Green', 'b', 3, 'Blue', 'c'] I am not sure really. I know I need more practice, but I haven't found a reliable way to find practice problems. Thank you -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 20:48:54 + (UTC), John Gordon gor...@panix.com wrote: In obja4a5ij57rtuaivtvcuqsufhuhk3a...@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. :) -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
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 -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Tue, 21 Oct 2014 10:54:55 +1300, Gregory Ewing greg.ew...@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. -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
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, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 15:49:15 -0400, Seymore4Head wrote: For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? Yes, it's called assignment. You can for example change a member of a list from an string to a tuple such as ( string, number ): x = [ fred, jim, susan ] x[x.index(jim)] = ( jim, 11, ) print x ['fred', ('jim', 11), 'susan'] Do you have to know the number of items the list will have before making it? No. See the append() method of the list object. -- Denis McMahon, denismfmcma...@gmail.com -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Mon, 20 Oct 2014 22:40:50 + (UTC), Denis McMahon denismfmcma...@gmail.com wrote: On Mon, 20 Oct 2014 15:49:15 -0400, Seymore4Head wrote: For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? Yes, it's called assignment. You can for example change a member of a list from an string to a tuple such as ( string, number ): x = [ fred, jim, susan ] x[x.index(jim)] = ( jim, 11, ) print x ['fred', ('jim', 11), 'susan'] Do you have to know the number of items the list will have before making it? No. See the append() method of the list object. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
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! :-) -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
Seymore4Head wrote: For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? Do you have to know the number of items the list will have before making it? Now these are the right sort of questions that you should be asking! They are concrete and tightly focused, not vague and so broad that they could mean anything, and they invite a concrete answer rather than hints. First question: can you take a single list item and turn it into a 2-item list? Yes, you can, you can turn a single list item into *anything*, including deleting it. Start with a list, and remember that lists are indexed starting with 0: py alist = [1, 2, 4, 8, 16, 32, 64] py alist[0] = hello! py alist[1] = [a, b, c] py del alist[4] py print(alist) ['hello!', ['a', 'b', 'c'], 4, 8, 32, 64] Second question: do you need to know the number of items in a list in advance? No. You can add additional items to an existing list with the append() method: py import random py alist = [] py while random.random() 0.5: ... alist.append(spam) ... py print(alist) ['spam', 'spam'] Because this is random, if you try it you will get an unpredictable number of spam words in the list. About half the time it will contain no words at all, about a quarter of the time it will contain a single word, an eighth of the time it will contain two words, and so forth. -- Steven -- https://mail.python.org/mailman/listinfo/python-list
Re: Building lists
On Tue, 21 Oct 2014 10:55:08 +1100, Steven D'Aprano steve+comp.lang.pyt...@pearwood.info wrote: Seymore4Head wrote: For starters I would like to know if you can make a single item list and then turn it into a 2 item list. Is there a command for that? Do you have to know the number of items the list will have before making it? Now these are the right sort of questions that you should be asking! They are concrete and tightly focused, not vague and so broad that they could mean anything, and they invite a concrete answer rather than hints. First question: can you take a single list item and turn it into a 2-item list? Yes, you can, you can turn a single list item into *anything*, including deleting it. Start with a list, and remember that lists are indexed starting with 0: py alist = [1, 2, 4, 8, 16, 32, 64] py alist[0] = hello! py alist[1] = [a, b, c] py del alist[4] py print(alist) ['hello!', ['a', 'b', 'c'], 4, 8, 32, 64] Second question: do you need to know the number of items in a list in advance? No. You can add additional items to an existing list with the append() method: py import random py alist = [] py while random.random() 0.5: ... alist.append(spam) ... py print(alist) ['spam', 'spam'] Because this is random, if you try it you will get an unpredictable number of spam words in the list. About half the time it will contain no words at all, about a quarter of the time it will contain a single word, an eighth of the time it will contain two words, and so forth. Thanks -- https://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 With this, you are creating a list with 6 references to the same list. Note that the left operand of '*' is evaluated only once before multiplying it six times. Regards, Tito -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
Jean_Francois Moulin wrote in news:[EMAIL PROTECTED] in comp.lang.python: Hi all, I tried this piece of code (FWIW, it was taken as is from a help section of mpfit, a mathematical routine for least square fitting): parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 parinfo[0]['fixed'] = 1 parinfo[4]['limited'][0] = 1 parinfo[4]['limits'][0] = 50. The first line builds a list of six dictionaries with initialised keys. I expected that the last three lines would only affect the corresponding keys of the corresponding dictionnary and that I would end up with a fully initialised list where only the 'fixed' key of the first dict would be 1, and the first values of limited and limits for dict number 4 would be 1 and 50. respectively This is not so! I end up with all dictionaries being identical and having their 'fixed' key set to 1, and limited[0]==1 and limits[0]==50. I do not understand this behaviour... This should help: url:http://www.python.org/doc/faq/programming/#how-do-i-create-a- multidimensional-list As a TinyUrl: http://tinyurl.com/s8akj Rob. -- http://www.victim-prime.dsl.pipex.com/ -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 parinfo[0]['fixed'] = 1 parinfo[4]['limited'][0] = 1 parinfo[4]['limits'][0] = 50. The first line builds a list of six dictionaries with initialised keys. I expected that the last three lines would only affect the corresponding keys of the corresponding dictionnary and that I would end up with a fully initialised list where only the 'fixed' key of the first dict would be 1, and the first values of limited and limits for dict number 4 would be 1 and 50. respectively This is not so! I end up with all dictionaries being identical and having their 'fixed' key set to 1, and limited[0]==1 and limits[0]==50. I do not understand this behaviour... The *6 creates multiple references to the same dictionary. Thus, when you update the dictionary through one reference/name (parinfo[0]), the things that the other entries (parinfo[1:5]) reference that changed dictionary. You're likely looking for something like parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}] for i in xrange(1,6): parinfo.append(parinfo[0].copy()) or something like parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}.copy() for i in xrange(0,6)] However, this will still reference internal lists that have been referenced multiple times, such that parinfo[5]['limited'] [0, 0] parinfo[4]['limited'][0] = 2 parinfo[5]['limited'] [2, 0] Thus, you'd also want to change it to be something like parinfo = [ {'value':0., 'fixed':0, 'limited':[0, 0][:], 'limits':[0., 0.][:] }.copy() for i in xrange(0, 6)] where the slice operator is used to build a copy of the list for each element as well (rather than keeping a reference to the same list for each dictionary). Hopefully this makes some sense, and helps get you on your way. -tkc -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
Jean_Francois Moulin wrote: Hi all, I tried this piece of code (FWIW, it was taken as is from a help section of mpfit, a mathematical routine for least square fitting): parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}]*6 The first line builds a list of six dictionaries with initialised keys. This is not so! I end up with all dictionaries being identical and having their 'fixed' key set to 1, and limited[0]==1 and limits[0]==50. I do not understand this behaviour... Thanks for helping a newbie. JF xvec = [{'value':0}]*6 xids = [id(x) for x in xvec] print xids Should print a list of six integers, all equal. So all elements in your list are the same. Another way to construct the desired list: yvec = [{'value':0} for i in range(6)] yids = [id(y) for y in yvec] print yids The elements in this list should be all different. -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
Tim Chase wrote: parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}.copy() for i in xrange(0,6)] However, this will still reference internal lists that have been referenced multiple times, such that parinfo[5]['limited'] [0, 0] parinfo[4]['limited'][0] = 2 parinfo[5]['limited'] [2, 0] Interesting. Cut-and-paste to my python prompt and I get parinfo[5]['limited'] [0, 0] Tried both Python 2.4.1 and 2.5 beta, Linux, GCC 4.0.2 -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}.copy() for i in xrange(0,6)] However, this will still reference internal lists that have been referenced multiple times, such that parinfo[5]['limited'] [0, 0] parinfo[4]['limited'][0] = 2 parinfo[5]['limited'] [2, 0] Interesting. Cut-and-paste to my python prompt and I get parinfo[5]['limited'] [0, 0] Tried both Python 2.4.1 and 2.5 beta, Linux, GCC 4.0.2 Of course. The expression within the list comprehension is evaluated for each iteration, so that the objects are recreated each time. The copy() for the dictionary is also not needed: parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]} for i in xrange(0,6)] parinfo [{'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}] parinfo[0]['limited'] [0, 0] parinfo[0]['limited'][0]=1 parinfo [{'limited': [1, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}, {'limited': [0, 0], 'fixed': 0, 'limits': [0.0, 0.0], 'value': 0.0}] Ernesto -- http://mail.python.org/mailman/listinfo/python-list
Re: building lists of dictionaries
parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}.copy() for i in xrange(0,6)] However, this will still reference internal lists that have been referenced multiple times, such that parinfo[5]['limited'] [0, 0] parinfo[4]['limited'][0] = 2 parinfo[5]['limited'] [2, 0] Interesting. Cut-and-paste to my python prompt and I get parinfo[5]['limited'] [0, 0] Hmm...same behavior on my 2.3.5 here. Looks like the problem lay within the for-loop version I tried first: parinfo = [{'value':0., 'fixed':0, 'limited':[0,0], 'limits':[0.,0.]}] for i in xrange(1,6): parinfo.append(parinfo[0].copy()) which copied the elements, but when the elements were references to other lists, only the references were copied. I just tried it with the second method I suggested (the list-comprehension one you reference here) and it doesn't have the same problem as the crazy for-loop, and as shown by others, doesn't need the superflous copy() call either. That'll teach me to test both proposed ideas, rather than making assumptions. -tkc -- http://mail.python.org/mailman/listinfo/python-list