简体   繁体   中英

Python, Adding all lines with a certain string from a file to list then randomly choosing which string to print?

import random
com=input("")
if com.startswith("/tip"):
    numlines=sum(1 for line in open("C:\\Users\\Jace\\Desktop\\Python Programs\\Quote\\tip.txt"))-1
    randomint=random.randint(0, numlines)
    with open("C:\\Users\\Jace\\Desktop\\Python Programs\\Quote\\tip.txt", "r") as f:
        i=1
        for line in f:
            if i==randomint:
                break
            i+=1
    print(line.strip("\n"))

This is the part of the code for my random tips from a file so far. I wish to add another part of code where it adds all strings with any occurrence of the input placed after "/tip ", for example, if I were to type "/tip Hello", it would compile all lines in the text file with "Hello" in the string and do a random.choice() from the list, printing the one chosen. I don't really know where to start with this, any help would be appreciated. Thanks in advance!

You don't have to store all of the lines in a list. You can read the lines, selecting one at random and discarding the rest. This is called "resevoir sampling".

Your code might look like this:

import random

def random_line(iterator):
    result = None
    for n, item in enumerate(iterator):
        if random.randint(0,n) == 0:
            result = item
    return result

# A random line
with open('tip.txt') as f:
    print random_line(f) or "No tip for you!"

# A random line that has 'Hello'
with open('tip.txt') as f:
    print random_line(line for line in f if 'Hello' in line) or "nothin!"

As a more special case, this code randomly chooses a matching line from the tips file, but falls back to a random non-matching line if no match exists. It has the advantages of reading the input file exactly once, and not having to store the entire tips file in memory.

import random

def random_line_with_fallback(iterator, match = lambda x: True):
    result_match = None
    result_all = None
    n_match = n_all = 0
    for item in iterator:
        if match(item):
            if random.randint(0, n_match) == 0:
                result_match = item
            n_match += 1
        if random.randint(0, n_all) == 0:
            result_all = item
        n_all += 1
    return (result_match or result_all).strip()

# A random line
with open('tip.txt') as f:
    print random_line_with_fallback(f)

# Another way to do a random line. This depends upon
# the Python feature that  "'' in line" will always be True. 
com = ''
with open('tip.txt') as f:
    print random_line_with_fallback(f, lambda line: com in line)

# A random line that has 'Hello', if possible
com = 'Hello'
with open('tip.txt') as f:
    print random_line_with_fallback(f, lambda line: com in line)

References:

I think this is what you want, process each line of a text file, checking if the line has the word you're looking for. If so, add it to a list, and the randomly select one "line" for all possible "lines".

lines = []
with open("tip.txt", "r") as f:
    for line in f:
        if com in line:
            lines.append(line)
print(random.choice(lines))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM