python choose randomly from each line of a file

passion

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.

Psidom

You need extend method if you want a flat list at the end and also specify replace=False if you don't want to choose one item more than once:

random_list = []
​
with open('pathToFile/inputFile', 'r') as f:
    for line in f.readlines():
        myparts = line.strip().split(' ')
        if len(myparts) <= 3:
            random_list.extend(myparts)
        else:
            random_list.extend(np.random.choice(myparts, 3, replace=False))

random_list
['8670842',
 '6717108',
 '8671024',
 '8671069',
 '8672185',
 '8672363',
 '8672317',
 '8672587',
 '8672610',
 '8688827',
 '8672661',
 '8688792',
 '8688884',
 '8689037',
 '8689003',
 '8672233',
 '8688891',
 '8688908',
 '8688971',
 '8689078']

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python: Choose random line from file, then delete that line

From Dev

printing the output for each line from a file in python

From Dev

Add each character from each line of a text file, to a list in Python

From Dev

Printing out each line (from file) with line number python

From Dev

Randomly generate a line from a .txt file

From Dev

Remove whitespaces from beginning and end of each line in a file in python

From Dev

Python - extracting particular numbers from each line in a file

From Dev

python - How to extract strings from each line in text file?

From Dev

How to split each line from file using python?

From Dev

How to randomly sample from 4 csv files so that no more than 2/3 rows appear in order from each csv file, in Python

From Dev

grep each line from a file in another file

From Dev

Simple way to randomly choose the name of a JavaScript file from a list of possible names

From Dev

How to randomly choose from a varying array of elements

From Dev

Is there a way to randomly choose value from hashmap by key?

From Dev

How to randomly choose an element from coredata swift

From Dev

Is there a way to randomly choose value from hashmap by key?

From Dev

Randomly Choose Music From a Folder on my Website

From Dev

Get each word from a line in a text file

From Dev

Bash reading from file and parsing each line

From Dev

Extract string from each line of a file

From Dev

Compare variable with each line from a txt file

From Dev

Cut a letter from each line of a file

From Dev

Removing the first line of each file from a wildcard?

From Dev

for each line in file write line to an individual file in python

From Dev

Python, concatenate each line of a file with one string

From Dev

Count values for each line in text file python

From Dev

How to take elements from each line of a file, and prepend them to each entry in each line of a different file?

From Dev

Taking a value from each line of a file to each their own var in php

From Dev

Print the line number of a text file for each word from a list in python 3 without importing anything

Related Related

  1. 1

    Python: Choose random line from file, then delete that line

  2. 2

    printing the output for each line from a file in python

  3. 3

    Add each character from each line of a text file, to a list in Python

  4. 4

    Printing out each line (from file) with line number python

  5. 5

    Randomly generate a line from a .txt file

  6. 6

    Remove whitespaces from beginning and end of each line in a file in python

  7. 7

    Python - extracting particular numbers from each line in a file

  8. 8

    python - How to extract strings from each line in text file?

  9. 9

    How to split each line from file using python?

  10. 10

    How to randomly sample from 4 csv files so that no more than 2/3 rows appear in order from each csv file, in Python

  11. 11

    grep each line from a file in another file

  12. 12

    Simple way to randomly choose the name of a JavaScript file from a list of possible names

  13. 13

    How to randomly choose from a varying array of elements

  14. 14

    Is there a way to randomly choose value from hashmap by key?

  15. 15

    How to randomly choose an element from coredata swift

  16. 16

    Is there a way to randomly choose value from hashmap by key?

  17. 17

    Randomly Choose Music From a Folder on my Website

  18. 18

    Get each word from a line in a text file

  19. 19

    Bash reading from file and parsing each line

  20. 20

    Extract string from each line of a file

  21. 21

    Compare variable with each line from a txt file

  22. 22

    Cut a letter from each line of a file

  23. 23

    Removing the first line of each file from a wildcard?

  24. 24

    for each line in file write line to an individual file in python

  25. 25

    Python, concatenate each line of a file with one string

  26. 26

    Count values for each line in text file python

  27. 27

    How to take elements from each line of a file, and prepend them to each entry in each line of a different file?

  28. 28

    Taking a value from each line of a file to each their own var in php

  29. 29

    Print the line number of a text file for each word from a list in python 3 without importing anything

HotTag

Archive