I am writing a program in python. In it, I want to generate a list. This list will start at one and increment by one [1, 2, 3, 4, 5, etc.].
However, I want the length of the list to be random, between 3 numbers and 8 numbers long. For example, on one run the list might generate [1, 2, 3, 4], on another it might generate 1, 2, 3, 4, 5, 6], another run might generate [1, 2, 3], and so on.
I know how to make a list generate random numbers but not so that it increments numbers at a random length. Thank you for your time. I am using Python 2.7 by the way.
Just
l1 = [ i + 1 for i in range(randint(...) ]
import random
start = 1
end = random.randint(3, 8)
l = range(start, end + 1)
An itertools approach - which also means if you don't need to materialise the list, then you don't have to:
from itertools import count, islice
from random import randint
mylist = list(islice(count(1), randint(3, 8)))
Since count is a generator incrementing by 1 each time, we use islice to "limit" the total number of items we take from it, and then build a list.
you can do it using range() where the first argument is the start and the second argument is the random length
import random
range(1, random.randint(3, 8))
If you specify 4 at the bottom end and 9 as the top of randint you'll get up to 8 numbers like you ask for. You need to do this because you start the range at 1.
>>> import random
>>> range(1, random.randint(4,4))
[1, 2, 3]
>>> range(1, random.randint(9,9))
[1, 2, 3, 4, 5, 6, 7, 8]
>>> rand_list = range(1, random.randint(4,9))
>>> rand_list
[1, 2, 3, 4, 5, 6, 7]
Range() first argument is the starting point. The 2nd argument is the stop. Many of the other answers will sometimes return only two numbers, in the cases where stop == 3.
>>> range(1,3)
[1, 2]
>>> range(1,4)
[1, 2, 3]
>>> range(1,8)
[1, 2, 3, 4, 5, 6, 7]
>>> range(1,9)
[1, 2, 3, 4, 5, 6, 7, 8]
from random import randint
l = range(1,randint(3,8)+1 )
print l
Related
I have a list and I want to iterate through it from element 1 to the end, and then finish up with element 0. Ie, essentially it's basic list iteration, except the very first element should be at the end.
I could do this with for i in range(len(myList)): and then adding 1 to i and checking for the edge case (that 's how I'd do in it C++), but was wondering if Python had some syntax which would make this concept easier to express?
I don't believe there is a convenient syntax for this, but you could easily define your own: I've chosen the name irotate by analogy to itertools.islice, and written it in a way such that it can be provided a single-use iterable such as a generator expression.
def irotate(iterable, k=1):
iterator = iter(iterable)
initial_elements = [next(iterator) for _ in range(k)]
yield from iterator
yield from initial_elements
Example usage:
>>> list(irotate(range(5)))
[1, 2, 3, 4, 0]
>>> for x in irotate(y**2 for y in range(5)): print(x)
...
1
4
9
16
0
The most simple way to achieve this, without the need for error handling/checks is to just use slicing.
_list = [1, 2, 3, 4]
for i in _list[1:] + _list[:1]:
print(i)
#2
#3
#4
#1
One could use modulo to get something like this -
mylist = [i for i in range(8)]
for i in range(5):
diff = i
rotated = [mylist[i % len(mylist)] for i in range(diff, len(mylist)+diff)]
print(rotated)
Output is -
[0, 1, 2, 3, 4, 5, 6, 7]
[1, 2, 3, 4, 5, 6, 7, 0]
[2, 3, 4, 5, 6, 7, 0, 1]
[3, 4, 5, 6, 7, 0, 1, 2]
[4, 5, 6, 7, 0, 1, 2, 3]
It is creating another list. So, may wanna keep that in mind.
... and then adding 1 to i and checking for the edge case ...
If you want only get rid of the checking for the edge case, use cycle() from the itertools standard module to create an infinite iterator:
import itertools
my_list = [2, 10, "a"]
my_iter = itertools.cycle(my_list) # 2, 10, "a", 2, 10, "a", 2, 10, "a", ...
next(my_iter) # drop the first element
for __ in range(len(my_list)):
curr_elem = next(my_iter)
print(curr_elem) # or do other activity with the current element
The output:
10
a
2
This question already has an answer here:
Create index list for np.split from the list that already has number for each section
(1 answer)
Closed 3 years ago.
Let's say I've got an array [0, 1, 2, 3, 4, 5, 6, 7] and a tuple: (3, 3, 2).
I'm looking for a way to split my array to 3 array based on my tuple data:
[0, 1, 2]
[3, 4, 5]
[6, 7]
I can write a simple code like this to get what I want, however I'm looking for a correct and pythonic way to do this:
I used lists for simplicity.
a = [0, 1, 2, 3, 4, 5, 6, 7]
b = (3, 3, 2)
pointer = 0
for i in b:
lst = []
for j in range(i):
lst.append(a[pointer])
pointer += 1
print(lst)
Or this one:
a = [0, 1, 2, 3, 4, 5, 6, 7]
b = (3, 3, 2)
pointer = 0
for i in b:
lst = a[pointer:pointer+i]
pointer += i
print(lst)
Results:
[0, 1, 2]
[3, 4, 5]
[6, 7]
you can use the split method of numpy
import numpy as np
a = [0, 1, 2, 3, 4, 5, 6, 7]
b = (3, 3, 2)
c = np.split(a, np.cumsum(b)[:-1])
for r in c:
print(r)
np.split(a, b) splits a by the indices in b along a given axis(0 by default).
If you don't want to modify your input list, you can use an iterator and the itertools module.
>>> from itertools import islice
>>> a = [0, 1, 2, 3, 4, 5, 6, 7]
>>> b = (3, 3, 2)
>>> i = iter(a)
>>> [list(islice(i, x)) for x in b]
[[0, 1, 2], [3, 4, 5], [6, 7]]
In the first step you create an iterator, which starts at the first element of a. Then you iterate in a list comprehension over your numbers in b and in each step you pull accordingly many elements from the iterator and store them in your result list.
One simpler way is this:
a = [0, 1, 2, 3, 4, 5, 6, 7]
b = (3, 3, 2)
for ind in b:
print(a[:ind])
a = a[ind:]
It loops through slice sizes in b while shortening the original array every time. You can easily append the resulting slices as sublists if you need them for something else. It's almost like one of your solutions except it doesn't use any extra variables and iterates directly through elements of b.
Also, I wouldn't call variables a and b - surely not in this case where variables have clear meanings that you can express through their names. More meaningful names lessen bugs number and make code more clear, becomes a real difference with larger/more complex code. I'd call a at least in_list and b slices, but with more context this could be better.
The most "concise" syntax would be :
ex_array = [0, 1, 2, 3, 4, 5, 6, 7]
extuple = (3, 3, 2)
result = [ex_array[sum(extuple[:iii]):sum(extuple[:iii])+extuple[iii]] for iii in range(len(extuple))]
result would be a list of the expected sub-lists
Re-using the pairwise function from Compare two adjacent elements in same list, you could also:
from itertools import accumulate
from more_itertools import pairwise
a = [0, 1, 2, 3, 4, 5, 6, 7]
b = (3, 3, 2)
[a[slice(*s)] for s in pairwise(accumulate((0,)+b))]
That begin said, the np.split answer is probably faster (and easier to read).
This question already has answers here:
How do I split a list into equally-sized chunks?
(66 answers)
Closed 6 years ago.
I need to add the first three elements of a list then add the next three elements of a list and so forth. This is the code I have got so far:
def get_triple_sums_list(a_list):
new_list = []
for numbers in range(0,len(a_list)):
numbers = sum(a_list[:3])
new_list.append(numbers)
return new_list
if a_list == []:
return []
For the list:
[1, 5, 3, 4, 5, 2]
This in turn gives me the result:
[9]
I need to get
[9, 11]
If the remaining numbers is less than 3, it gives me the remainder of the sum ie,
[1, 6, 2, 4, 3]
Gives me
[9, 7]
And
[1, 6, 2, 4]
Give me
[9, 4]
Let's analyze your code!
def get_triple_sums_list(a_list):
new_list = []
for numbers in range(0,len(a_list)):
numbers = sum(a_list[:3]) #You should be using the variable
#numbers here somehow.
#^^^^^^^ - You are overwriting the for-loop index.
new_list.append(numbers)
return new_list #Why are you returning here? You should be
#appending to `new_list`.
if a_list == []:
return []
Here is the fixed code:
def get_triple_sums_list(a_list):
new_list = []
for index in range(0,len(a_list), 3): #Range takes a 3rd param!
total = sum(a_list[index:index+3])#Get all the elements from the
#index to index+3
new_list.append(total)
return new_list
UPDATE: It seems there's a shortening contest going on -- and I do not want to be left behind. Here's an ugly version I'd like to add to the list.
>>> a = [1,2,3,4,5,6,7,8]
>>> a += [0]*(len(a)%3) #For people who are too lazy to import izip_longest
>>> map(sum,zip(a[::3], a[1::3], a[2::3]))
[6, 15, 15]
I like SuperSaiyan's approach of explaining things, I'll be the one who shortens it a bit. You can get the same result with a single comprehension:
l = [1, 5, 3, 4, 5, 2]
n = 3
r = [sum(l[i:i+n]) for i in range(0, len(l), n)]
print(r)
[9, 11]
l[i:i+n] splits the list in even chunks of length 3 and sum takes care of adding these together. Using the for i in range(0, len(l), n) we dictate that this operation is to happen for ceil(len(l) / 3) times.
Just cuz I like to be different.
l = [1, 5, 3, 4, 5, 3, 42]
g = lambda l,s: [sum(l[i:i+s]) for i in range(0,len(l),s)]
print g(l,3)
#>> [9,12,42]
The other answer mentions the fault with your code. However do note that it's always easier to use a list comprehension in these cases.
>>> l = [1, 5, 3, 4, 5, 2]
>>> [sum(l[i:i+3]) for i in range(0,len(l),3)]
[9, 11]
It also works for un-mod-3 lists
>>> l = [1, 5, 3, 4, 5]
>>> [sum(l[i:i+3]) for i in range(0,len(l),3)]
[9, 9]
See What does "list comprehension" mean? How does it work and how can I use it? for more details about a list comprehension.
Here is a slightly different way of doing it using zip_longest from itertools (izip_longest in python2), it splits the list in three lists then zip them to get packs of three elements and finally sums the packs:
from itertools import zip_longest
a=[1, 6, 2, 4, 3]
b=zip_longest(a[0::3],a[1::3],a[2::3],fillvalue=0)
result=[sum(x) for x in b]
>>>[9, 7]
Alternatively, you may achieve it by using map() with lambda function as:
>>> my_list = [1, 5, 3, 4, 5, 2]
>>> list(map(lambda x: sum(my_list[x:x+3]), range(0, len(my_list), 3)))
[9, 11]
In Python we can make a random array from 0 to 10 by doing:
randarray = random.sample(range(0, 10), 10)
My problem is that I want to create an array from 0 to 10, BUT in the first position I want to add a randomly generated number, and then append the rest of the numbers without repetition.
This means I first make:
first_value = random.randint(0,10)
and for example, if first_value is 4, I want to create a random array from 0 to 10 (without repetition) but ignoring number 4. For example, [0, 9, 5, 6, 1, 8, 7, 3, 2] . This is what I dont know how to do, so any help would be much appreciated.
Then I just want to append the first_value to randarray.
randarray.insert(0, first value)
So the desired output would be: [4, 0, 9, 5, 6, 1, 8, 7, 3, 2])
It sounds like you are really looking for a shuffled list:
>>> from random import shuffle
>>> xs = range(0, 10)
>>> shuffle(xs)
>>> xs
[2, 5, 4, 7, 1, 9, 3, 0, 6, 8]
For a more general way, use sample:
>>> from random import sample
>>> xs = sample(range(0, 10), 10)
If you actually do want to generate the head separately:
>>> from random import randint, sample
>>> x = randint(0, 10)
>>> p = range(0, 10)
>>> p.remove(x)
>>> xs = [x] + sample(p, 9)
Hope this helps -
first_value = random.randint(0, 10)
randarray = [i for i in random.sample(range(0, 10), 10) if i != first_value]
randarray.insert(0, first_value)
The way is:
you have a list
you have first number
remove number from list
shuffle list
This question already has answers here:
Repeating elements of a list n times
(14 answers)
Closed 5 months ago.
I want to write a function that reads a list [1,5,3,6,...]
and gives [1,1,5,5,3,3,6,6,...].
Any idea how to do it?
>>> a = range(10)
>>> [val for val in a for _ in (0, 1)]
[0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9]
N.B. _ is traditionally used as a placeholder variable name where you do not want to do anything with the contents of the variable. In this case it is just used to generate two values for every time round the outer loop.
To turn this from a list into a generator replace the square brackets with round brackets.
>>> a = [1, 2, 3]
>>> b = []
>>> for i in a:
b.extend([i, i])
>>> b
[1, 1, 2, 2, 3, 3]
or
>>> [a[i//2] for i in range(len(a)*2)]
[1, 1, 2, 2, 3, 3]
numpy.repeat does what you want:
import numpy as np
yourList = [1,5,3,6]
n = 2
list(np.repeat(yourList, n))
result:
[1, 1, 5, 5, 3, 3, 6, 6]
If you don't mind using numpy arrays you can also omit the list() call in the last line.
If you already have the roundrobin recipe described in the documentation for itertools—and it is quite handy—then you can just use
roundrobin(my_list, my_list)
I would use zip and itertools.chain.
>>> import itertools
>>> l = [1,5,3,6,16]
>>> list(itertools.chain(*zip(l,l)))
[1, 1, 5, 5, 3, 3, 6, 6, 16, 16]
Note: I only used list to consume the generator to make it fit for printing. You probably don't need the list call in your code...
It is possible use list multiplication. Case you need each list member together just use sorted method.
>>> lst = [1,2,3,4]
>>> sorted(lst*2)
[1,1,2,2,3,3,4,4]
With a little slicing...
>>> a = [3, 1, 4, 1, 5]
>>> a[:0] = a[::2] = a[1::2] = a[:]
>>> a
[3, 3, 1, 1, 4, 4, 1, 1, 5, 5]
I would use
import itertools
foo = [1, 5, 3, 6]
new = itertools.chain.from_iterable([item, item] for item in foo)
new will be an iterator that lazily iterates over the duplicated items. If you need the actual list computed, you can do list(new) or use one of the other solutions.
One can use zip and flat the list
a = [3, 1, 4, 1, 5]
sum(zip(a,a), ()) # (3, 3, 1, 1, 4, 4, 1, 1, 5, 5)
The output is a tuple, but conversion to a list is easy.
Regarding flatting a tuple with sum see https://stackoverflow.com/a/952946/11769765 and python: flat zip.
For as much as Guido dislikes the functional operators, they can be pretty darned handy:
>>> from operator import add
>>> a = range(10)
>>> b = reduce(add, [(x,x) for x in a])
For a more general approach you could go with a list comprehension and a factor term.
Example
sample_list = [1,2,3,4,5]
factor = 2
new_list = [entry for entry in sample_list for _ in range(factor)]
Out:
>>> new_list
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
Changing the factor variable will change how many entry of each item in the list you will have in the new list.
You could also wrap it up in a function:
def multiply_list_entries(list_, factor = 1):
list_multiplied = [entry for entry in list_ for _ in range(factor)]
return list_multiplied
>>> multiply_list_entries(sample_list, factor = 3)
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4, 5, 5, 5]
ls1=[1,2,3]
ls2=[]
for i in ls1:
ls2.append(i)
ls2.append(i)
This code duplicates each elements in ls1
the result ls2 --> [1,1,2,2,3,3]