Donate to us via Paypal!
I'd prefer to die standing, than to live on my knees - Che Guevara
Wednesday, March 03, 2021
Navigation
Home
 Find:
 Information:
Learn
Communicate
Submit
Shop
Challenges
 Exploit:
 Programming:
 Think:
 Track:
 Patch:
 Other:
 Need Help?
Other
Members Online
Total Online: 112
Guests Online: 110
Members Online: 2

Registered Members: 133807
Newest Member: dacomir635
Latest Articles

View Thread

HellBound Hackers | Computer General | Programming

Author

Python problems

-god-
Member

Your avatar

Posts: 97
Location:
Joined: 28.01.07
Rank:
Wiseman
Posted on 25-06-15 05:50
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.
Author

RE: Python problems

MrCyph3r
npm ERR!



Posts: 786
Location:
Joined: 09.08.14
Rank:
God
Posted on 30-06-15 23:18
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 :)
Author

RE: Python problems

Huitzilopochtli
Member



Posts: 1644
Location:
Joined: 19.02.13
Rank:
God
Posted on 01-07-15 23:12
All Hail the python king.
Author

RE: Python problems

MrCyph3r
npm ERR!



Posts: 786
Location:
Joined: 09.08.14
Rank:
God
Posted on 04-07-15 13:08
Haha lol Pfft