-1

how would I create an n vector(or array) on Python and specify the value for each entry? For instance, let's say I want the vector (a1,...,a10) thus n=10 and each ai can be a value from [1,20]. So a1 can be any value from 1 through 20, thus a1 is any element in [1,20] , moreover a2 can be any value from [1,20] and so on. Or if n =15, then for (a1,...,a15) each ai can be a value [1,20]. Basically I would like to generalize it to any n tuple (a1,...,an) where each ai is a value from [1,20] and run through different values for n

Example.)if I wanted all tuples for n=2 and where each element ai can be a number 1 through 4 or in other words (a1,a2) where a1 is in [1,4] and a2 is in [1,4] this would give me:(1,1) ,(1,2), (2,1), (2,2), (2,3),(3,2),(3,3),(4,3),(3,4) and (4,4)

I not only want all combinations but each element has to be a value in the range [1,20]

M3105
  • 519
  • 2
  • 7
  • 20
  • How about a tuple? Here's a tuple of 5 elements: `(7,13,11,2,8)` – zvone Oct 27 '16 at 16:57
  • yes, an n tuple works as well – M3105 Oct 27 '16 at 17:06
  • so if I wanted an n tuple where n=5, I'm trying to figure out how to code something like (a1=[1,20] , a2=[1,20], a3=[1,20], a4=[1,20], a5=[1,20]) via Numpy – M3105 Oct 27 '16 at 17:09
  • So, what exactly is your question? – zvone Oct 27 '16 at 17:09
  • You want random value for each element? – zvone Oct 27 '16 at 17:10
  • okay, for example, say if I wanted all 2-dimensional tuples where a1 and a2 can be any value from 1 through 4. Thus n=2 and each element a1 and a2 are in [1,4]. So I would get: (1,1) ,(1,2), (2,1), (2,2), (2,3),(3,2),(3,3),(4,3),(3,4) and (4,4) – M3105 Oct 27 '16 at 17:19
  • Ah, you want all combinations? http://stackoverflow.com/questions/1208118/using-numpy-to-build-an-array-of-all-combinations-of-two-arrays – zvone Oct 27 '16 at 17:23
  • yes I would like all possible n dimensional combinations where each element can be a value from 1 through 20 – M3105 Oct 27 '16 at 18:12

3 Answers3

1

According to your updated description if I understand correctly what you need is itertools.product (in general itertools module deals with permutations and combinations and the docs are excellent. For example:

s = list(range(1, 5))  # [a, b)
print(list(itertools.product(s, repeat=2)))
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]

Below some further code I've written a while ago making use itertools for my own needs :) (note if you are in python2 uncomment the first line from __future__ import print_function, division line.

# from __future__ import print_function, division
import itertools

def possible_pairs(s, mirrors=True):
    if len(s) != len(set(s)):
        raise ValueError('duplicates found, but not allowed')
    return list(itertools.permutations(s, 2)) if mirrors else list(itertools.combinations(s, 2))

def possible_pairs_between_two_lists(s1, s2, mirrors=True):
    if len(s1) != len(set(s1)) or len(s2) != len(set(s2)):
        raise ValueError('duplicates found, but not allowed')
    return list(itertools.product(s1, s2)) + list(itertools.product(s2, s1)) if mirrors else list(
        itertools.product(s1, s2))


s = list(range(1, 5))  # [a, b)
print(possible_pairs(s))
# [(1, 2), (1, 3), (1, 4), (2, 1), (2, 3), (2, 4), (3, 1), (3, 2), (3, 4), (4, 1), (4, 2), (4, 3)]
print(possible_pairs(s, False))
# (1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
print(possible_pairs_between_two_lists(s, s))
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4), (1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
print(possible_pairs_between_two_lists(s, s, False))
# [(1, 1), (1, 2), (1, 3), (1, 4), (2, 1), (2, 2), (2, 3), (2, 4), (3, 1), (3, 2), (3, 3), (3, 4), (4, 1), (4, 2), (4, 3), (4, 4)]
John Smith
  • 1,077
  • 6
  • 21
  • Thank you for your response. I've updated my question to be more specific. Essentially I want the list of the n tuples where each element is a value from 1 through 20. So for example, if I wanted all tuples for n=2 and where each element ai can be a number 1 through 4 or (a1,a2) where a1 is in [1,4] and a2 is in [1,4] this would give me:1,1) ,(1,2), (2,1), (2,2), (2,3),(3,2),(3,3),(4,3),(3,4) and (4,4) – M3105 Oct 27 '16 at 17:26
  • I noticed after running the code above for n=2, randint(1,4) and print(int_list) I got [1, 4] which I'm assuming it's returning random intergers from 1 through 4. However what I'm actually trying to figure out is how to obtain all of the n tuples given each element in that tuple is a value from [1,20]. So for n=2 and each element a_i is from [1,4] this would generate (1,1) ,(1,2), (2,1), (2,2), (2,3),(3,2),(3,3),(4,3),(3,4) and (4,4) – M3105 Oct 27 '16 at 18:11
0

Python arrays default to lists (unless you're using Numpy). See this link for more.

You can actually build a list relatively easily.

import random
int_list = [randint(1,20) for i in range(n)]        # n is the list length

Hope this helps.

  • Thank you for your response. I've updated my question to be more specific. Essentially I want the list of the n tuples where each element is a value from 1 through 20. So for example, if I wanted all tuples for n=2 and where each element ai can be a number 1 through 4 or (a1,a2) where a1 is in [1,4] and a2 is in [1,4] this would give me:1,1) ,(1,2), (2,1), (2,2), (2,3),(3,2),(3,3),(4,3),(3,4) and (4,4) – M3105 Oct 27 '16 at 18:13
  • If that's the case, a list comprehension with two for-loops should do. `int_list = [(i,k) for i in range(1,21) for k in range(1,21)]` should give you a list of all combinations (as tuples) of value pairs in [1,20] inclusive. If you want to make larger tuples (containing more than 2 values), you can add another loop in the list comp. Just be wary that runtime increases by factor of n for each additional value in the tuple. As such, I'd recommend switching over to numpy if you want to handle larger tuples/values. – Sondering Narcissist Oct 28 '16 at 03:39
0

Use the array module if you want a very fast and efficient implementation of "true" arrays (as opposed to lists)

import random
import array

a = array.array('l', [random.randint(1,20) for i in range(20)])
print a[1], a[2]

More information on the array module is here: https://docs.python.org/2/library/array.html

Regards

Ukimiku
  • 608
  • 1
  • 6
  • 13