Gamecommunity =GCHQ=
http://forum.gamecommunity.co.uk:8080/

More Python
http://forum.gamecommunity.co.uk:8080/viewtopic.php?f=6&t=48823
Page 1 of 1

Author:  Seric=GCHQ= [ Sat Jan 12, 2008 2:10 pm ]
Post subject:  More Python

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?

Author:  Tjolbi=GCHQ= [ Sat Jan 12, 2008 2:17 pm ]
Post subject:  Re: More Python

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:

Author:  Seric=GCHQ= [ Sat Jan 12, 2008 2:43 pm ]
Post subject:  Re: More Python

erm... lolwut?

Author:  Chips=GCHQ= [ Sat Jan 12, 2008 3:36 pm ]
Post subject:  Re: More Python

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

Author:  Tjolbi=GCHQ= [ Sat Jan 12, 2008 5:37 pm ]
Post subject:  Re: More Python

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:

Author:  Seric=GCHQ= [ Sun Jan 13, 2008 12:52 am ]
Post subject:  Re: More Python

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?

Author:  Chips=GCHQ= [ Sun Jan 13, 2008 1:11 am ]
Post subject:  Re: More Python

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.

Author:  Seric=GCHQ= [ Sun Jan 13, 2008 3:01 am ]
Post subject:  Re: More Python

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"

Page 1 of 1 All times are UTC [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/