Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

Python problems


-god-'s Avatar
Member
0 0

I just start learning python again, and I'm trying to write a program as I learn. But I'm having trouble finding specific information through google, so here I am.

On a basic level what I want to do is write a tuple[] and then select relative elements within it and write a new list() based on user input

so, if we have:

list = [] list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

x = input("Select: ") …

if x = 1 # I want elements 1, 3, 5, 6, 8 and 10. if x = 2 # –> 2, 4, 6, 7, 11[0]

So we can do this with a a series of if/while statements, but I feel that's not the most efficient way of doing it.

I also thought of trying something like this:

x = () x = (list[0:4:2] + list[5:9:2])

but I'd still have to make several conditionals based on the value of X, unless I can tell python to read from list[0] after it reads the last element.

where I'm at right now is something like this:

list = [] list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] y = input("select: ")

if (y == list[0]): x = () x = (list[0:5:2] + list[5:12:2]) elif (y == list[1]): x = () x = (list[1:6:2] + list[6:13:2] + list[0] #rinse and repeat for x in list[]

In my head I wanted to create a module that could select relative elements in a given list, starting at point x.

therefore x = list[x], list[x+2], list [x+4], list[x+5] etc. (the reason I'm not simply slicing it all is that the relative distances are not consistent, else i'd just have // x = list[0:12:2] or some such.

I don't know how to adjust x() to start from any interval and wrap around once it gets to the final element. maybe a for * in style would work, but I don't know how to make it ignore unwanted elements [and I still have the problem of what happens when it gets to the last element.]

Not asking for anyone to code it for me, just want someone to point me in the right direction so I know what I should be reading up on.


MrCyph3r's Avatar
npm ERR!
0 0

Ok, let's get into this…

First thing I would say is that if this is an exercise for learning purposes than good, go for it, but if it is a real world scenario and you are trying to implement a real program, I suggest you to think about a different implementation because this is a pretty uncommon thing to do and looks kinda messy, at least in my opinion.

That being said, yes, it is possible and this is a function already implemented by the numpy module (which is doing a really great job on that), so you don't have to create your own module.

Let's take your use case as an example:

items = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

y = raw_input('Select: ')

Now you want to select a subset of items based on the selection

A possible implementation is to convert (or switch, for that matter) to numpy arrays your tuple and define an array of indexes as per requirements.

so your code will be:

items = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

y = int(raw_input('Select: '))

The predefined array of indexes

choice1 = np.array([0,2,4,5,7,9]) # Note that indexes start from 0 choice2 = np.array([1,3,6,8])

Now select the subset of items

if y == items[0]: x = np.array(items[choice1]) elif y == items[1]: x = items[choice2]

Note, this is just a suggestion and can be further improved… the if statement for example, if you need more than two cases to check for, I would switch to another kind of statement.

In my opinion this is the best method to use for this particular requirement, however you can still implement your own, but it will be like reinventing the wheel.

You can find the manual reference here

Hope it helps :)


MrCyph3r's Avatar
npm ERR!
0 0

Haha lol :P