473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

List function

3
How to do the function for input list of stock in python
like [p(1), p(2), . . . , p(n)], for example [ 23,
53, 55, 42, 31, 40 ]
Mar 30 '10 #1

✓ answered by Glenton

Hi Adam4u

I'm afraid that code in your second post makes no sense. As I see it there are two things you could be trying to do.

option 1: user enters a number, and the computer assigns it an index number automatically

option 2: user enters a number and an index and the computer assigns that number to that index.

Both are possible, of course. Which do you want? Or is there something else that you want?

Here's an ugly implementation of option 1:
Expand|Select|Wrap|Line Numbers
  1. p=[]  #sets up p as a list
  2. while True:  #infinite loop for data input
  3.     i=input("Enter any number (0 to quit): ")
  4.     if i==0: break   #if user enters nothing break out of infinite loop
  5.     p.append(i)  #append i as the next item in the list
  6.  
  7. #Once we've broken out of the loop, print of the values you've entered:
  8. print p
  9. #or if you want to be a bit fancy
  10. for n,i in enumerate(p):
  11.     print "index ", n, "value ", i, " which is equal to ", p[n]
  12.  

5 1975
bvdet
2,851 Expert Mod 2GB
You will have to clarify your question. I don't understand what you are trying to do.

BV - Moderator
Mar 30 '10 #2
Adam4u
3
i'm trying to do a functio that consists two input as arrays or (pairs)
and here what i came up with
Expand|Select|Wrap|Line Numbers
  1. def main():
  2.     i = [n]
  3.     i, n = input("Enter any number: ")
  4.     print " The first input is", i, " and the index is", n

On other hand, I want to print out the number of any integer entered with its array index between bracket... Thanks in advance
Mar 30 '10 #3
bvdet
2,851 Expert Mod 2GB
Does this help any?
Expand|Select|Wrap|Line Numbers
  1. >>> numbers = [0,1,2,3,4,5,6,7,8,9]
  2. >>> for i, num in enumerate(numbers):
  3. ...     print "The number is %s and the index is %s" % (num, i)
  4. ...     
  5. The number is 0 and the index is 0
  6. The number is 1 and the index is 1
  7. The number is 2 and the index is 2
  8. The number is 3 and the index is 3
  9. The number is 4 and the index is 4
  10. The number is 5 and the index is 5
  11. The number is 6 and the index is 6
  12. The number is 7 and the index is 7
  13. The number is 8 and the index is 8
  14. The number is 9 and the index is 9
  15. >>> 
Mar 30 '10 #4
Glenton
391 Expert 256MB
Hi Adam4u

I'm afraid that code in your second post makes no sense. As I see it there are two things you could be trying to do.

option 1: user enters a number, and the computer assigns it an index number automatically

option 2: user enters a number and an index and the computer assigns that number to that index.

Both are possible, of course. Which do you want? Or is there something else that you want?

Here's an ugly implementation of option 1:
Expand|Select|Wrap|Line Numbers
  1. p=[]  #sets up p as a list
  2. while True:  #infinite loop for data input
  3.     i=input("Enter any number (0 to quit): ")
  4.     if i==0: break   #if user enters nothing break out of infinite loop
  5.     p.append(i)  #append i as the next item in the list
  6.  
  7. #Once we've broken out of the loop, print of the values you've entered:
  8. print p
  9. #or if you want to be a bit fancy
  10. for n,i in enumerate(p):
  11.     print "index ", n, "value ", i, " which is equal to ", p[n]
  12.  
Mar 30 '10 #5
Adam4u
3
bvdet and Glenton thanks for your help... i just figured it out from online search, Glenton you were closed to what i wanted.. but thanks
Mar 30 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jeffrey Silverman | last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree structure from that list. Basically, I want to turn this: $list = array( array( 'id' => 'A', 'parent_id' => null, 'value'...
3
by: George M Jempty | last post by:
Is there a good linked list implementation in the public domain? Or that someone on this group has already written that they'd like to put in the public domain, right here on this newsgroup? I've...
8
by: JustSomeGuy | last post by:
I need to write an new class derived from the list class. This class stores data in the list to the disk if an object that is added to the list is over 1K in size. What methods of the std stl...
7
by: Kieran Simkin | last post by:
Hi all, I'm having some trouble with a linked list function and was wondering if anyone could shed any light on it. Basically I have a singly-linked list which stores pid numbers of a process's...
4
by: JS | last post by:
I have a file called test.c. There I create a pointer to a pcb struct: struct pcb {   void *(*start_routine) (void *);   void *arg;   jmp_buf state;   int    stack; }; ...
4
by: bitshadow | last post by:
I've been working on a link list implementation and I'm driving myself crazy trying to understand something. assuming i have a list and i have the following quasi pseudocode: add(list *head):...
6
by: Fazana | last post by:
I was doing one of the question but my program was not working properly. Can anyone help me with it pliz........ Here is the question for the program Question. Part 1. Add the function...
2
by: Lysander | last post by:
I have not seen this feature documented before, so I thought I would share it with you, as I will be using it in a later article. For a combo or list box, the source data is normally a...
7
by: =?Utf-8?B?Sm9lbCBNZXJr?= | last post by:
I have created a custom class with both value type members and reference type members. I then have another custom class which inherits from a generic list of my first class. This custom listneeds...
0
by: Atos | last post by:
SINGLE-LINKED LIST Let's start with the simplest kind of linked list : the single-linked list which only has one link per node. That node except from the data it contains, which might be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.