Open Side Menu Go to the Top
Register
Search function with lists [ipython] Search function with lists [ipython]

10-30-2015 , 10:56 AM
Hey, maybe here is some1 who can help me.
Is their any function in ipython that compares 3char of my input with any object in my list and print me all objects with the same 3chars.
for example my list

A=['ZZo(0.25)','YQa(0.75)','ABz(R 0.75)',['ABz(L 0.25)']]
now when i press ABz i want that the searchfunction prints me
ABz(R 0.75) ABz(L 0.25)

maybe it will look like this(pseudocode)
if input == position(0,1,2)of any object in List A then print the objects

Hope some1 can help me
regards
Search function with lists [ipython] Quote
10-30-2015 , 11:30 AM
one way to do it

Code:
for item in A:
    if item[0:3]==input:
        print(item)
Search function with lists [ipython] Quote
10-30-2015 , 12:10 PM
Quote:
Originally Posted by econophile
one way to do it

Code:
for item in A:
    if item[0:3]==input:
        print(item)
Hey, thanks for your help
I tried to build the function but when i use it ,it prints only one object.
Where is my mistake?
regards

[PHP]def searchinlist(input):
for item in A:
if item[0:3]==input:
print(item)
[/PHP]

[PHP]searchinlist(ABz)

['ABz(L 0.25)']
[/PHP]
Search function with lists [ipython] Quote
10-30-2015 , 12:13 PM
The last element of your list above is a list rather than a string.

If you change A=['ZZo(0.25)','YQa(0.75)','ABz(R 0.75)',['ABz(L 0.25)']]

to A=['ZZo(0.25)','YQa(0.75)','ABz(R 0.75)','ABz(L 0.25)']

then it should work. Unless there is some reason that some of the elements in your list should be lists.
Search function with lists [ipython] Quote
10-30-2015 , 12:54 PM
Quote:
Originally Posted by econophile
The last element of your list above is a list rather than a string.

If you change A=['ZZo(0.25)','YQa(0.75)','ABz(R 0.75)',['ABz(L 0.25)']]

to A=['ZZo(0.25)','YQa(0.75)','ABz(R 0.75)','ABz(L 0.25)']

then it should work. Unless there is some reason that some of the elements in your list should be lists.
I actually thought if i have a mainlist for xxx(R x.xx)elements then in this list another list for xxx(L x.xx) elements makes it a little bit clear for me.
Im not so familiar with python so i thought i can make a lists with alot of lists inside just with adding [x,y,[z,a],[ab]] so now ive got 3lists.
But it isnt necessary. So im very happy with this function it works fine for my purposes. Thank you alot

Is it possible to give this function and the input a very short inputlook?
I tried this
[PHP]b = searchinlist[/PHP]
[PHP]b('ABz')[/PHP]
this works well.Is it possible to reduce this even more.
For example just press b space bar and then ABz without ''
So something like bABz or b ABz
regards
Search function with lists [ipython] Quote
10-30-2015 , 01:18 PM
This may be closer to what you are looking for:

Code:
def searchinlist():
    input = raw_input('Enter search string: ')
    for item in A:
        if item[0:len(input)]==input:
            print(item)
Code:
>>> searchinlist()
Enter search string: ABz
ABz(R 0.75)
ABz(L 0.25)
Search function with lists [ipython] Quote
10-30-2015 , 01:49 PM
Quote:
Originally Posted by econophile
This may be closer to what you are looking for:

Code:
def searchinlist():
    input = raw_input('Enter search string: ')
    for item in A:
        if item[0:len(input)]==input:
            print(item)
Code:
>>> searchinlist()
Enter search string: ABz
ABz(R 0.75)
ABz(L 0.25)
That will help me. Thanks

Is it also possible to include a randomnize function to the searchfunction that will give me a random ouput .
So ABz(R 0.75) means 75% and ABz(L 0.25) means 25%.
I can also change the name of the objects when it makes it easier to implement. Maybe the form ABz(R 75.0) or anyother form is better for this task.

So when i search my list and typ ABz it will give me ABz(R0.75) with a 75% chance and ABz(L 0.25) with a 25% chance.
Maybe something with

[PHP]import random[/PHP]
[PHP]print(random.randint(1,100))[/PHP]

and then compare the 5&6char of ABz(R75.0) which is 75 with the random number. if random number is between 1 and 75 print ABz(R75.0)
if random number is between 76 and 100 print ABz(L 0.25)
their is a maximum of 3 objects that the searchfunktion give out
some examples
so maybe ABz(R50.0) ABz(L25.0) ABz(S25.0)
or ABz(R1.0)
but another question is how to separe stuff like 50,25,25
so that the computer now 50 is 1-50 25 is 51-75 and the other 25 is 76-100

maybe this randominclude is way to difficult i dont know. but perhaps someone has a idea.

regards

Last edited by AsUs91; 10-30-2015 at 02:14 PM.
Search function with lists [ipython] Quote
10-31-2015 , 01:09 AM
I'm not a python guy, why did they add parenthesis to print in python3.
Seems better to not have them like in python2 and also why the need of : at the end of each statement. I mean, it doesn't seem like it would be impossible to get rid of the : by checking the beginning of the line and checking eof but there is probably something basic that i'm not getting.

Like the language seems good for kids to learn and move into a more structured programming language, after they've learned the basic fundamentals but there are these inconsistencies that annoy me, when looking at the language.

This post is ugly, I'm really tired and not really caring to write it out clean.
Search function with lists [ipython] Quote
10-31-2015 , 06:49 AM
Works only with flattened lists.

Code:
import re
from numpy.random import choice


def find_probability(element):
    return float(re.findall(r'[+-]?\d+(?:\.\d+)?', element)[0])


def random_choice(term, input_list):
    filtered = [el for el in input_list if term in el]
    weights = [find_probability(el) for el in filtered]
    print(choice(filtered, p=weights))


if __name__ == '__main__':
    a = ['ZZo(0.25)', 'YQa(0.75)', 'ABz(R 0.75)', 'ABz(L 0.25)']
    random_choice('ABz', a)
Search function with lists [ipython] Quote
10-31-2015 , 02:50 PM
Quote:
Originally Posted by iosys
I'm not a python guy, why did they add parenthesis to print in python3.
print is now a function as opposed to a statement.

Some of the rationale is outlined in the original PEP but I understand it basically to be much improved flexibility. https://www.python.org/dev/peps/pep-3105/
Search function with lists [ipython] Quote
11-01-2015 , 01:20 AM
Smiley what about the required colon, it seems like there could be workarounds to get rid of it and or do you know the reason for it.

I'm just thinking that the goal of the language was typically to make the simplest to write language; when I look at python, I get that vibe but maybe i'm wrong on what python is trying to be.

Anyway thanks for the reply, will take a look at the link and get some grip on the rational behind the flexibility.
Search function with lists [ipython] Quote
11-01-2015 , 08:09 AM
Quote:
Originally Posted by Pahvak
Works only with flattened lists.

Code:
import re
from numpy.random import choice


def find_probability(element):
    return float(re.findall(r'[+-]?\d+(?:\.\d+)?', element)[0])


def random_choice(term, input_list):
    filtered = [el for el in input_list if term in el]
    weights = [find_probability(el) for el in filtered]
    print(choice(filtered, p=weights))


if __name__ == '__main__':
    a = ['ZZo(0.25)', 'YQa(0.75)', 'ABz(R 0.75)', 'ABz(L 0.25)']
    random_choice('ABz', a)
This looks very good. Works only with flattened lists means only one big list,and no lists in lists?
Is their also a option to shortcut the random_choice('ABz', a)
like only typ in ABz a or a ABz something like this
regards
Search function with lists [ipython] Quote
11-01-2015 , 11:03 AM
Quote:
Originally Posted by iosys
Smiley what about the required colon, it seems like there could be workarounds to get rid of it and or do you know the reason for it.

I'm just thinking that the goal of the language was typically to make the simplest to write language; when I look at python, I get that vibe but maybe i'm wrong on what python is trying to be.

Anyway thanks for the reply, will take a look at the link and get some grip on the rational behind the flexibility.
My understanding of the ethos of the python community is the same. Simplicity in the syntax with maximum effect.
Search function with lists [ipython] Quote
11-02-2015 , 08:36 AM
Quote:
Originally Posted by AsUs91
Works only with flattened lists means only one big list,and no lists in lists?
Exactly.

Quote:
Originally Posted by AsUs91
Is their also a option to shortcut the random_choice('ABz', a)
like only typ in ABz a or a ABz something like this
regards
Well you can always write a new function, which uses the previous ones. Not sure if you want to be able to choose from multiple lists. For example currently there are two lists - a & b to choose from. New code:

Code:
import re
from numpy.random import choice

a = ['ZZo(0.25)', 'YQa(0.75)', 'ABz(R 0.75)', 'ABz(L 0.25)']
b = ['CCo(0.25)', 'DFa(0.75)', 'HNz(R 0.75)', 'HNz(L 0.25)']


def find_probability(element):
    return float(re.findall(r'[+-]?\d+(?:\.\d+)?', element)[0])


def random_choice(term, input_list):
    filtered = [el for el in input_list if term in el]
    weights = [find_probability(el) for el in filtered]
    print(choice(filtered, p=weights))


def random_choice_from_string(string):
    """ :param string: for example 'ABz a' """
    term, input_list_name = [s.strip() for s in string.split(" ") if s.strip()]
    input_list = globals()[input_list_name]
    random_choice(term, input_list)


if __name__ == '__main__':
    while True:
        ip = input("Insert (for example: 'ABz a' or 'HNz b'): ")
        random_choice_from_string(ip)
NB! If you use python2.7, then use raw_input() instead of input().
Search function with lists [ipython] Quote
11-02-2015 , 05:26 PM
Works well. Thank u alot
regards
Search function with lists [ipython] Quote
11-03-2015 , 02:11 PM
Oh i tried to use some stuff like B8z or 88z and get the error message probabilities do not sum to 1. I think this is because we use numbers. But i thought numbers are valid in strings, so why is this not working? Is it possible and how to make the function working with numbers too? So 88z, B9z, 87z like this.
regards
Search function with lists [ipython] Quote
11-04-2015 , 04:29 AM
Well I had no idea what your other inputs look like. Are they poker hands? Anyway the code breaks because the first occurence of a number is treated as a probability. If the input string is '88z(L 0.25)', then it uses 88, which obviously is not a probability. Just change this function to

Code:
def find_probability(element):
    return float(re.findall(r'[+-]?\d+(?:\.\d+)?', element)[-1])
Now the last number in a string is used as a probability.
Search function with lists [ipython] Quote
11-08-2015 , 12:57 PM
Thanks now it works rlly well. Is their a easy function in python
for this programm that if the user make a wrong input for example k8sa
then ask for new input? now if i make a wrong input i get a error message.
like a do while loop in c++.
regards
Search function with lists [ipython] Quote
11-09-2015 , 07:14 AM
Just use try/catch.

Code:
while True:
    try:
        ip = input("Insert (for example: 'ABz a' or 'HNz b'): ")
        random_choice_from_string(ip)
    except:
        print("Try again.")
Search function with lists [ipython] Quote

      
m