Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
For a class projet, i want to be able to predict the result of a random.shuffle, but what i learned about the random module is way more advance than everything i learned so far.
Do you have any idea of how it could be done ?
I found some code that allowed me to predict the result of randint, but nothing for shuffle
The following code should always give the same output:
lst = [0,1,2,3,4]
random.seed(5)
random.shuffle(lst)
print(lst)
My guess is that if you managed to make it work for other methods, you simply forgot to reinitialize the list each time
Related
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 months ago.
Improve this question
I need help with python. How can I separate combine word into two words. At the first we don't know what kind of words could be? so we must enter a string at the input (v="") and then that word must be separate. For example we have "AMnidcrheaal" and in output will be "Andrea" and "Michal".
According to the output you gave, you want to separate odd and even indices words which can be done as follows
v="AMnidcrheaal"
even=""
odd=""
for i in range(len(v)):
if i%2==0:
even+=v[i]
else:
odd+=v[i]
print(odd)
print(even)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Can someone give a suitable example of the XOR Operator in Python ? I understand its definition but couldn’t implement it. So, please explain with a suitable example.
Thanks in advance
This is a fair enough implementation:
def xor(a:bool, b:bool)->bool:
return (not a) == (b)
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I am a beginner to python and I'm trying to make a simple scrabble score calculation program. May I know how to get the letter score from the letterScore function and add it to the scrabbleScore function? Thanks a lot for your help! Please have a look on the screenshot for the program I have tried~
That looks good, however I would recommend using a dictionary to get the letter scores:
values = { ('a':1), ('b':3), ('c':3), ... , ('z':10) }
then you can find the score much faster by using values[letter] inside your letterScore function.
Now to get the score from your function, you need to call the following inside the bottom for loop, above the totalScore calculation
score = letterScore(letter)
Hope this helps
I think you just need to move score = letterScore(letter) to the inside of your loop in scrabbleScore
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
There are two major variables (calls and puts), and several sub-variables (e.g. bid, change, time etc.) For example, if there are total 5 data points. I know how to do separately:
data[u'options'][0]["calls"][0]["change"]['fmt'], data[u'options'][0]["calls"][1]["change"]['fmt'], data[u'options'][0]["calls"][2]["change"]['fmt'], data[u'options'][0]["calls"][3]["change"]['fmt'],data[u'options'][0]["calls"][4]["change"]['fmt']
but that spend too much time. I wonder how to choose multiple items in one code.
You can do this with a little bit of list comprehension if I understand your question properly.
For each value in data["options"][0]["calls"], it adds that value's ["change"]["fmt"] value to the list.
d = [call["change"]["fmt"] for call in data["options"][0]["calls"]]
If you want a list of EACH value from every set of options, you could do it like so:
d = [[call["change"]["fmt"] for call in option["calls"]] for option in data["options"]]
and now you can say
for option in d:
for call in option:
print(call)
[data[u'options'][0]["calls"][i]["change"]['fmt'] for i in range(5)]
I don't quite understand your problem, is this what you're after?
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I feel like there is definitely a quick way to check it, instead of having to loop through the entire list.
any(isinstance(x, int) for x in list_of_things_that_might_be_ints)
This still loops, but the loop is inside the generator expression argument to any. Also, any will short-circuit, so if you have a million-entry list, and the second item in the list is an int, then the remaining 999,998 won't be looked at.
Sorry, just found the answer through a similar question!
Use:
if any(isinstance(x, SubclassOne) for x in list_of_stuff):