Problem With SPLIT FUNCTION And RANDOM Module - python-3.x

[](https://i.stack.imgur.com/AWJQV.png)
i get input from user and created the list using split function. and while using random.randint()
it should produce random intger from the list but it prints the entire list,
but it works properly when i created my own list . what is the problem here.
you can see my problem in the image

Related

How to initilise a list that contains custom functions without python running those functions during initialisation?

Short version:
How do you store functions in a list and only have them be executed when they are called using their index position in the list?
Long Version:
So I am writing a program that rolls a user-chosen number of six-sided dice, stores the results in a list and then organizes the results/ data in a dictionary.
After the data is gathered the program gives the user options from 0-2 to choose from and asks the user to type a number corresponding to the option they want.
After this input by the user, a variable, lets say TT, is assigned to it. I want the program to use TT to identify which function to run that is contained within a list called "Executable_options" by using TT as the index posistion of this function within the list.
The problem I am having is that I have to have the list that contains the functions on a line after the functions have been defined and when I initialize the list it goes through and executes all functions within it in order when I don't want it to. I just want them to be in the list for calling at a later date.
I tried to initialise the list without any functions in and then append the functions individually, but every time a function is appened to the list it is also executed.
def results():
def Rolling_thunder():
def roll_again():
The functions contains things, but is unnecessary to show for the question at hand
Executable_options = []
Executable_options.append(results())
Executable_options.append(Rolling_thunder())
Executable_options.append(roll_again)
options = len(Executable_options)
I am relatively new to Python so I am still getting my head around it. I have tried searching for the answer to this on existing posts, but couldn't find anything so I assume I am just using the wrong key words in my search.
Thank you very much for taking the time to read this and for the answers provided.
Edit: Code now works
The () on the end of the function name calls it - i.e. results() is the call to the results method.
Simply append to the list without the call - i.e:
Executable_options.append(results)
You can then call it by doing e.g.:
Executable_options[0]()
as per your given data the code will look like this:
def results():
def Rolling_thunder():
def roll_again():
Executable_options = []
Executable_options.append(results)
Executable_options.append(Rolling_thunder)
Executable_options.append(roll_again)
for i in range(0,len(Executable_options)):
Executable_options[i]()
this will work for you.

Program in to generate chained list in python

I am developing a program in python and as part of it I have to link all the lists that have an element in common in a certain position, that is, there is an input element and an output element and I want to gather all those that follow the chain. For example, we have as input a list :
list_array = [[n_element, l_input, l_ouput], .....]
A concrete example would be
list_array = [[1,a,b],[2,c,d],[3,e,f],[4,b,e],[5,d,f],[6,a,e],[7,b,c]
The result of the program should be a list where the elements are linked by input and output.
res_array = [[1,4,3],[1,7,2,5],[6,3]]
The result of the program should be a list where the elements are linked by input and output. If there is one element included in another, the element with greater length prevails. My first thought was to use a tree structure, a search in depth or length. I need ideas.
You could use a graph representation of this problem:
For each element [n_element, l_input, l_output], you add vertices l_input and l_output to your graph (if not already present) and add an edge labelled n_element (from l_output to l_input).
Then, you look for paths through that graph. The resulting list is then given by the concatenation of edge labels.

using list values in an if statement to compare with a string

I have this list and string
list_var=(['4 3/4','6','6 3/4'])
string_var='there are 6 inches of cement inside'
i want to check through and find if any of the elements of the list is in the string
if list_var in strin_var (wont work of course due to the fact its a list but this is what i want to achieve is to compare all of the values and if any match return true)
i tried using any function but this not seem to work i would also like to achieve this without using loops as the code i will be using this in contains extracting data from a lot of files so execution speed is an issue
you will have to iteratre over the list at least once as you are trying a search operation.
is_found = any(True for i in list_var if i in string_var)
works perfectly for this scenario.

How do I avoid "'zip' object is not reversible" errors when migrating to Python 3?

In my recently migrated from 2 to 3 Python code I have
list(reversed(zip(*positions)))
which generates the error
TypeError: 'zip' object is not reversible
I can fix this by changing the problematic code to
list(reversed(list(zip(*positions))))
but this seems like the wrong way to go about it.
What is the correct way to revers a zip in Python 3?
reversed is used to iterate on the list. It doesn't create a list on purpose, because it's often used just to iterate backwards on elements, not to create lists.
That's why you have to use list on it to create a list. And it needs a sequence to be able to get to the last element directly so you have to do list(zip()) in python 3.
Maybe you could shorten
list(reversed(list(zip(*positions))))
to
list(zip(*positions))[::-1]
it creates a list directly without the need for reverse so it's probably slightly faster too.

Zip Function without using built in

I'm trying to create a function (without using a built in function) that turns this
([f,u,n],[1,2,3,4])
into this [(f,1),(u,2),(n,3)]
Basically taking the first element of both lists and turning them into a tuple, then the second element of both lists, etc.
I have this so far:
>> def zipup(lista,listb):
>> for x in range(len(lista)):
>>for y in range(len(listb)):
>>return [(lista[x],listb[y])]
But I keep getting this: [(f,1)]
Help?!
The error message is saying that the variable i is not defined, it is not even in the code snippet that you showed ... that a look at the line that the error shows.
As for the zip, take a look at the docs for the standard library for the izip, there is a equivalent python implementation that you can use as a guide.

Resources