This question already has answers here:
Selecting random elements in a list conditional on attribute
(4 answers)
How to find all occurrences of an element in a list
(18 answers)
Get random element on condition
(3 answers)
How can I randomly select an item from a list?
(17 answers)
Closed 6 days ago.
Let's say I have a list: a = [1,1].
If I call a.index(1) it will always return 0.
Is there any pythonic way to return 0 or 1 in equal probabilities?
As stated here:
The syntax of the index method is: list.index(x[, start[, end]]).
It also says:
The optional arguments start and end are interpreted as in the slice notation and are used to limit the search to a particular subsequence of the list. The returned index is computed relative to the beginning of the full sequence rather than the start argument.
So a possible way could be the use of random as the start argument:
import random
a = [1, 1, 1, 1, 1, 1]
index_method = a.index(1)
index_method_with_start = a.index(1, random.randint(0, len(a) -1))
print(index_method, index_method_with_start)
Related
This question already has answers here:
How do I create a list with numbers between two values?
(12 answers)
Closed 17 days ago.
So I am defining a function that takes in one variable R. Then I need to create a list of all integers from 0 to R (in the context of the problem R will always be positive).
EX) When I do
R=5
print(list(0,R))
I just get a list with 2 elements: 0 and 5, but I want 0,1,2,3,4,5
The range() function returns a sequence of numbers, starting from 0 by default, and increments by 1 (by default), and stops before a specified number.
range(6) would return [0,1,2,3,4,5]
This question already has answers here:
Pythonic way to find maximum value and its index in a list?
(11 answers)
Getting the index of the returned max or min item using max()/min() on a list
(23 answers)
Closed 1 year ago.
list = [3,5,1,8,9]
I want to find positions of the maximum value in the list
This is pretty simple, but it will give you the index of the first occurrence:
>>> l = [3,5,1,8,9]
>>> l.index(max(l))
4
I strongly suggest you not use the name of built-in functions as list for variables.
This question already has answers here:
How do I reverse a string in Python?
(19 answers)
Closed 1 year ago.
I have a code in which I am attempting to reverse a certain string. I have received instructions to only use index to do this, so I used index, and came to a halt when I found this. Here is my code first, for context.
emptyList = []
Len = len(var)
for i in range(Len, 0, -1):
emptyList.append(i)
print(emptyList)
My problem is that whenever I print the variable emptyList, I only get a list of numbers, like such:
[3, 2, 1]
I want the numbers to represent their character, like 3 = G, 2 = O. How do I do that?
Use emptyList.append(var[i-1]) not just i.
This question already has answers here:
Find the index of the second occurrence of a string inside a list
(3 answers)
Find the index of the n'th item in a list
(11 answers)
Closed 7 years ago.
If I'm working with a list containing duplicates and I want to know the index of a given occurrence of an element but I don't know how many occurrences of that element are in the list, how do I avoid calling the wrong occurrence?
Thanks
I don't know that a single builtin does this thing alone, but you could fairly easily write it, for instance:
def index_second_occurence(alist, athing):
if alist.count(athing) > 1:
first = alist.index(athing)
second = alist[first + 1::].index(athing)
return second + first + 1
else:
return - 1
This question already has answers here:
How to get last items of a list in Python?
(5 answers)
Python - How to extract the last x elements from a list [duplicate]
(4 answers)
Closed 9 years ago.
e.g., for a sequence of unknown length, what is the most "Pythonic" way of getting the last n elements?
Obviously I could calculate the starting and ending indices. Is there anything slicker?
Yes, by using negative indices:
last_five = somesequence[-5:]
Negative indices in a slice are relative to the sequence length.
Use negative indexing.
seq[-1] is the last element of a sequence. seq[-3:] gives you the last three.
Try:
sequence[-n:]
(text to make Stack Overflow happy)