0
for n in range(1,(len(randnum))/3):
    X.append(randnum(n))

for i in range((len(randnum))/3 , (2/3)*len(randnum)):
    Y.append(randnum(i))

for r in range ((2/3)*len(randnum) , len(randnum)):
    Z.append(randnum(r))    

I have been trying to form a list based on this criteria and I keep getting this error message for specifically this line below:

for n in range(1,(len(randnum))/3):

TypeError: 'float' object cannot be interpreted as an integer

The part of the program that is causing the problems is that part above and if I can fix it I can take the error and apply it to the rest.

Here is an example list that is used to fill the other three it has 20 elements and I want each list that I form to take from this list about 1/3 of its elements from different positions:

[ 59.18013391 12159.7881626  26308.21887981  8357.05103068
 20718.85232457 16333.1546026   9828.75690047 10273.65018539
  5949.58907673  8767.68292925 31826.29595355 13749.12915211
 25423.61181129 28799.50849876  9517.54482827 27275.19296144
 12460.2541769  25883.7888204  10393.9452616  26008.572598  ]

And I want this code to form 3 new lists containing in for example

X = [59.18013391 12159.7881626  26308.21887981  8357.05103068
 20718.85232457 16333.1546026]

Y = [9828.75690047 10273.65018539
  5949.58907673  8767.68292925 31826.29595355 13749.12915211 ]

Z = [ 25423.61181129 28799.50849876  9517.54482827 27275.19296144
 12460.2541769  25883.7888204  10393.9452616  26008.572598]
vash_the_stampede
  • 4,274
  • 1
  • 5
  • 20
Carmine
  • 29
  • 6
  • 1
    Can we see the lists, and a desired output – vash_the_stampede Sep 23 '18 at 23:00
  • 2
    Possible duplicate of [I keep getting this error for my simple python program: "TypeError: 'float' object cannot be interpreted as an integer"](https://stackoverflow.com/questions/19824721/i-keep-getting-this-error-for-my-simple-python-program-typeerror-float-obje) – wwii Sep 23 '18 at 23:04
  • Convert your range paramters to int: `int((len(randnum))/3)` – panktijk Sep 23 '18 at 23:05
  • Based on your question you want a list, A, to have the same length and elements of a list B. Why not just write A=B then? – Nasrin Sep 23 '18 at 23:05

3 Answers3

2

You cannot use float for the range() function.

See: https://docs.python.org/3/library/stdtypes.html#range

(len(randnum))/3

that's float!

possible fix :

int((len(randnum))/3)

OKAY, perhaps your should try random

With repetition

import random

original_list =[ i for i in range(20)]

X = random.sample(original_list, int(len(original_list)/3))

Y = random.sample(original_list, int(len(original_list)/3))

Z = random.sample(original_list, int(len(original_list)/3))

Sample Output

X: [7, 3, 18, 15, 19, 1]

Y : [6, 13, 17, 4, 14, 5]

Z: [19, 2, 8, 18, 13, 17]

Without repetition

from random import shuffle

shuffle(original_list)

list(zip(*[iter(original_list)]*int(len(original_list)/3)))

Sample Output

[(17, 13, 15, 5, 16, 12), (14, 4, 18, 2, 19, 6), (10, 11, 7, 3, 1, 0)]

pajamas
  • 1,032
  • 1
  • 11
  • 23
  • What would be the solution then? – wwii Sep 23 '18 at 23:05
  • If you sample from the original list three separate times, though, you'll sometimes get the same value in multiple outputs (as you do for 18, for example). The OP's example doesn't have any, so it's not clear that it's allowed. – DSM Sep 23 '18 at 23:56
1

If I follow your goal, a simple approach would be to shuffle a copy of the list and then take every 3rd element starting at 0, then 1, then 2:

tmp_data = data.copy()
random.shuffle(tmp_data)
new_lists = [tmp_data[i::3] for i in range(3)]

which gives me, e.g.

In [361]: new_lists
Out[361]: 
[[13749.12915211,
  26008.572598,
  25423.61181129,
  8767.68292925,
  12460.2541769,
  26308.21887981,
  59.18013391],
 [9828.75690047,
  20718.85232457,
  10273.65018539,
  9517.54482827,
  27275.19296144,
  8357.05103068,
  5949.58907673],
 [28799.50849876,
  12159.7881626,
  25883.7888204,
  16333.1546026,
  10393.9452616,
  31826.29595355]]

and you could then do

X, Y, Z = new_lists

if you insisted on separate named variables.

(You could also simply do tmp_data = random.sample(data, len(data)) to get a random permutation of the list instead, but for some reason I find this less clear than shuffling. Not sure why.)

DSM
  • 291,791
  • 56
  • 521
  • 443
0

You could use random here but also you are going to need to make sure you don't use that random int again, my approach is append it to a used list and check that list to see if it can be used again, as far as splitting them you have 20 so you can use // floor division and % to help you create two lists of 7 items and one of 6 items.

import random

data = [
    59.18013391,12159.7881626,26308.21887981, 8357.05103068,
    20718.85232457,16333.1546026,9828.75690047, 10273.65018539,
    5949.58907673, 8767.68292925, 31826.29595355, 13749.12915211,
    25423.61181129, 28799.50849876, 9517.54482827, 27275.19296144,
    12460.2541769, 25883.7888204, 10393.9452616, 26008.572598
]
y = len(data)//3
z = int((len(data) % 3)/2)
used = []
l1 = []
l2 = []
l3 = []

for i in range(y):
    x = random.randint(0, len(data)-1)
    while x in used:
        x = random.randint(0, len(data)-1)
    used.append(x)
    l1.append(data[x])

for i in range(y+z):
    x = random.randint(0, len(data)-1)
    while x in used:
        x = random.randint(0, len(data)-1)
    used.append(x)
    l2.append(data[x])

for i in range(y+z):
    x = random.randint(0, len(data)-1)
    while x in used:
        x = random.randint(0, len(data)-1)
    used.append(x)
    l3.append(data[x])
chrx@chrx:~/python/stackoverflow/9.23$ python3.7 loop.py 
l1:  [8357.05103068, 10273.65018539, 26008.572598, 5949.58907673, 28799.50849876, 8767.68292925]
l2:  [25423.61181129, 13749.12915211, 26308.21887981, 9828.75690047, 59.18013391, 16333.1546026, 27275.19296144]
l3:  [12460.2541769, 12159.7881626, 9517.54482827, 10393.9452616, 25883.7888204, 31826.29595355, 20718.85232457]
vash_the_stampede
  • 4,274
  • 1
  • 5
  • 20