Login    Forum    Register    Search    FAQ

Board index » HELP AND ADVICE » GAME/SOFTWARE/HARDWARE PROBLEMS




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: More Python
 Post Posted: Sat Jan 12, 2008 2:10 pm 
Offline
"Eric ya Fecker!"
User avatar

Joined: Sat May 28, 2005 1:02 pm
Posts: 4223
Whats the easiest way of doubling the numerical contents of a list?
I've got this:

Code:
def intDouble():
    answer = 1
    int1 = input("Please input your integers ")
    listA = [int1]
    while answer == 1:
        print "Do you wish to enter another number?"
        answer = input("1. Yes" "\n" "2. No" "\n")
        if answer == 1:
            number = input("Please input a number ")
            listA.append(number)
            print listA
    if answer == 2:
        listB = listA * 2
        print listB
    else:
        print "Invalid entry"


But obviously list * 2 just prints it twice.

Any ideas?

_________________
Image
Image


Top 
 Profile  
 
 Post subject: Re: More Python
 Post Posted: Sat Jan 12, 2008 2:17 pm 
Offline
Ostracised!
User avatar

Joined: Wed Jun 01, 2005 3:41 pm
Posts: 9042
Location: cooking nades in the backyard
for i = 1 to listA.NumberOfItemsInListCodeThingy
listB.[i] = listA.[i] * 2
loop :?


or something like that at least? :)

maybe listB.append(listA.[i] * 2) instead though :)



syntax is probably horribly wrong aswell :lol: :oops:

_________________
The banhammer thirsts for tards | There is no 'overkill'. There is only 'open fire' and 'I need to reload'.


Top 
 Profile  
 
 Post subject: Re: More Python
 Post Posted: Sat Jan 12, 2008 2:43 pm 
Offline
"Eric ya Fecker!"
User avatar

Joined: Sat May 28, 2005 1:02 pm
Posts: 4223
erm... lolwut?

_________________
Image
Image


Top 
 Profile  
 
 Post subject: Re: More Python
 Post Posted: Sat Jan 12, 2008 3:36 pm 
Offline
I'm ghey 4 teh Hoff!
User avatar

Joined: Wed Sep 21, 2005 5:18 pm
Posts: 4142
You need to iterate over the list, which means you go through every element in the list.
Decide whether you want to have a new list of results, or the same list being used, but all the values altered.

http://effbot.org/zone/python-list.htm

Tells you how to access, to loop (iterate) and modify lists.

You can loop through, getting the item in the list and doubling it... or create a new list, and get the index of every element in listA... and assinging the same index location in list b to have the value of 2 * listA's indexed item value.

If you get stuck, shout out :D Still haven't installed python, but it's a programming language - so they're all roughly the same :P


Top 
 Profile  
 
 Post subject: Re: More Python
 Post Posted: Sat Jan 12, 2008 5:37 pm 
Offline
Ostracised!
User avatar

Joined: Wed Jun 01, 2005 3:41 pm
Posts: 9042
Location: cooking nades in the backyard
Chips=GCHQ= wrote:
You can loop through, create a new list, and get the index of every element in listA... and assinging the same index location in list b to have the value of 2 * listA's indexed item value.


that's the basic jest of what i failed to explain :oops:

syntaxes from different coding languages mushed together should work right? :lol: :oops:

_________________
The banhammer thirsts for tards | There is no 'overkill'. There is only 'open fire' and 'I need to reload'.


Top 
 Profile  
 
 Post subject: Re: More Python
 Post Posted: Sun Jan 13, 2008 12:52 am 
Offline
"Eric ya Fecker!"
User avatar

Joined: Sat May 28, 2005 1:02 pm
Posts: 4223
I'm still not sure whats going on, I've tried the iter function, but I think I'm getting the syntax wrong or something...

would I have to do something like

i = iter(ListA)
item = i.next()
item = i.next()

but I have no idea what that would do - do I need a different place to put each itterated item?

If so, how do I know how many items the list has?

_________________
Image
Image


Top 
 Profile  
 
 Post subject: Re: More Python
 Post Posted: Sun Jan 13, 2008 1:11 am 
Offline
I'm ghey 4 teh Hoff!
User avatar

Joined: Wed Sep 21, 2005 5:18 pm
Posts: 4142
To get the number of items in a list, it's len(List)

That'll return an integer value ;)
To access an item in the list, you can List[index]

Now when you do the length function, if the list has 4 items, they "occupy" the index locations 0, 1, 2, 3.

So you can iterate (or loop) over the items in the list 0 -> 3.

Personally from there I'd use this method:
Quote:
for index, item in enumerate(L):
print index, item


It's saying (essentially) for every item inside the list, get the index position of the item in the list... as well as the item itself.

So what you could do is:

Code:
for index, item in enumerate(listA):
      listA[index] = 2 * item


There's a variety of ways to loop, so you could just do:
Code:
for item in listA:
        item = item * 2



Course, not overly familiar with p*thon, but if it's returning a reference to the item, this should be fine.

You can use the iterator, but I am not sure how the iterator works within python, calling the next method is fine whilst something is there to call it upon... but I don't know how you'd check.

In java the iterator has a method called "hasNext()" which returns a boolean value if there are more objects to iterate over.
So get the iterator, then enter a loop with the condition: (remember, this is java - not p*thon. Tis an example).
Code:
while(iterator.hasNext()){
 
}


inside the loop you'd know there are more items to iterate over (hasNext returned true), and therefore can call the iterators next method to return a reference to the item. Calling the next method not only returns the object, or reference to, it also moves the iterator forward, so the next call to "hasNext()" will ascertain if there's any more.

In this instance I have no idea what p*thon does with its iterator to check there's anything left to iterate over, so therefore it's easier to use a more conventional method - just a basic loop through the indexes of your list, and access the elements via their index to manipulate them. I think every language has this feature, so it's pretty universal.


Top 
 Profile  
 
 Post subject: Re: More Python
 Post Posted: Sun Jan 13, 2008 3:01 am 
Offline
"Eric ya Fecker!"
User avatar

Joined: Sat May 28, 2005 1:02 pm
Posts: 4223
Solved it this way.

Code:
def intDouble():
    answer = 1
    int1 = input("Please input your integers ")
    listA = [int1]
    while answer == 1:
        print "Do you wish to enter another number?"
        answer = input("1. Yes" "\n" "2. No" "\n")
        if answer == 1:
            number = input("Please input a number ")
            listA.append(number)
            print listA
    if answer == 2:
        listB = 0
        listB = list(e*2 for e in listA)
        print listB
    else:
        print "Invalid entry"

_________________
Image
Image


Top 
 Profile  
 
Display posts from previous:  Sort by  
 
Post new topic Reply to topic  [ 8 posts ] 

Board index » HELP AND ADVICE » GAME/SOFTWARE/HARDWARE PROBLEMS


Who is online

Users browsing this forum: No registered users and 27 guests

 
 

 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  
  • Shoutbox
  • Shout Message


test
cron