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
How to get expected answer [0, 1, 2, 3] using the range function?
I tried this Python code:
print(range(4)),
result:
range(0,4)
The result is different from online playgrounds:
result 01: range(0,4)
result 02: [0, 1, 2, 3]
The general syntax of range function:
range(start, stop, step)
start : Numerical index, at which position to start(optional)
stop : Index you go upto but not include the number(position to end)(required)
step: Incremental step size(optional)
Examples
x = range(0, 6)
for n in x:
print(n) //output: 0 1 2 3 4 5
In your case you need a list containing the integers so as #Green Cloak Guy said you should use list(range(4))
do list(range(4)).
The range() function returns a generator (only produces one value at a time). You have to convert that generator to a list.
Related
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 6 months ago.
Improve this question
I'm making a program that gets new numerical values to put into a list in numerical order and I need to know what element is before it in the list or the index before it.
How do I find the index of a newly added element in a list that is ordered numerically?
If you have a list that is sorted, you can use the list.index function to find the index of an element in the list. You then just need to get the element at that index minus one. Assuming the index isn't zero.
>>> lst = [0, 2, 5, 9]
>>> x = 3
>>> lst.append(x)
>>> lst.sort()
>>> n = lst.index(x)
>>> (n-1, lst[n-1])
(1, 2)
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
How to select the previous number in list comprehension that starts from the second one?
The formula is [w(j-1)* 5 +w for t in y]?
w(j-1) is the first number in list.
Not exactly sure what you are trying to achieve. Therefore, my own interpretation of the problem. Assume you have a list of numbers:
my_list = range(1, 10)
Now we want to iterate over this list starting from the second entry and also access the previous entry:
new_list = [my_list[i - 1]*5 + my_list[i] for i, n in enumerate(my_list) if i > 0]
print(new_list)
This way you can get access to a list element and its predecessor.
Is this what you are trying to achieve?
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 3 years ago.
Improve this question
I’ve tried sample module to generate lists but only get 5 lists thought I need much more...what should I do?
There shall be no repeats in Each list
But the repetitive lists are allowed
When asking a question, make sure to provide examples of what you've tried and what output you're expecting.
If the output your expecting is 100 lists of [1, 2, 3, 4, 5] see below:
import random
listAmount = 100
lists = []
for l in range(listAmount):
lists.append([1, 2, 3, 4, 5])
#Shuffle the list:
random.shuffle(lists[l])
print(lists[l])
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 years ago.
Improve this question
For example, if I have the number 14731, I found how to convert it to a list of integers and how to find the highest integer. From here, how would I split the list [1,4,7,3,1] to be [1,4,7] and [3,1]?
x = 14731
ls = [int(i) for i in str(x)]
maxIndex = ls.index(max(ls))
ls1, ls2 = ls[:maxIndex+1], ls[maxIndex+1:]
print(ls1, ls2)
# [1, 4, 7] [3, 1]
This example uses slice notation to cut the list into two halves, based on the index of the maximum value.
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 7 years ago.
Improve this question
If I have 6 or 7 element in a python list, how do I count the number of elements in the list?
Use len(), e.g.
>>> l = [1,2,3,4,5,6,7]
>>> print len(l)
7
Given the basic nature of your question, I recommend that you read the Python Tutorial.
Use the len function :
len([1,2,3,4,5,6,7])
If you want to learn more about the len function :
http://www.tutorialspoint.com/python/list_len.htm
You can do as follows using lists', count:
In [5]: a=[1,3,4,6,6,7,6]
In [6]: a.count(6)
Out[6]: 3
In [7]: a.count(7)
Out[7]: 1
Or if you mean actual length of an array, rather than the number of occurrences of an element in a list, just use len:
In [8]: len(a)
Out[8]: 7
You can also do this:
a=[1,2,3,4,5,6]
print len(a) # will equal 6