I want to choose randomly 3 elements from each line of my text file:
6717108 8373270 8670842 8671024 8671040
8671069 8672185
8672302 8672317 8672363 8672481 8672533 8672550 8672587
8672610
8672611 8672640 8672661 8672684 8688747 8688760 8688777 8688792 8688827
8688836 8688884 8689003 8689037
8672233 8688891 8688908
8688971 8689078
However, I don't have always 3 elements in each line, in this case it should take all of them. So the output will be like, taken randomly:
6717108 8670842 8671040
8671069 8672185
8672317 8672481 8672533
8672610
8672611 8688747 8688760
8688836 8689003 8689037
8672233 8688891 8688908
8688971 8689078
My attempt is the following:
random_list = []
with open('my_inputFile', "r") as myFile:
for line in myFile.readlines():
myparts = line.split(' ')
random_list.append(np.random.choice(myparts, 3))
the output format will be in list form:
6717108
8670842
8671040
8671069
8672185
8672317
8672481
8672533
8672610
8672611
8688747
8688760
8688836
8689003
8689037
8672233
8688891
8688908
8688971
8689078
Question is:
my code doesn't fulfill the condition when it is less than 3 elements in each line, and it is not apparently in list format.
Aucun commentaire:
Enregistrer un commentaire