Home > database >  how i can update a string list with a feed of string every second?
how i can update a string list with a feed of string every second?

Time:10-29

For example, i have one method new_strings_feed() that return a list of strings every second, like:

1s:    ["this"]
2s:    ["this", "is"]
3s:    ["this", "is", "example"]
4s:    ["is", "example"] 
5s:    ["is", "an" "example", "that",] 
6s:    ["an", "example", "that", "return", "strings"] #

i would like get this information very second, but without loose past informations. example, if we are in sec 6 and I print my list:

while(True):
    refresh_my_list(new_strings_feed(), mylist)
    print(mylist)
    time.sleep(1)

So mylist outputs should be:

1s:    ["this"]
2s:    ["this", "is"]
3s:    ["this", "is", "example"]
4s:    ["this", "is", "example"]
5s:    ["this", "is", "an" "example", "that",] 
6s:    ["this", "is", "an" "example", "that", "return", "strings"] #

I've tried different types of comparisons, but I can't solve it.

Someone have a suggest how i can implement my "reflesh_my_list"?

CodePudding user response:

append method is what you are looking for, dont know what this "refresh_my_list" function does but if you have a list like this:

sample_array = ["this", "is", "an" "example", "that", "return", "strings"]
import time
output_array=[]

for item in sample_array:
    output_array.append(item)
    print(output_array)
    time.sleep(1)

this should do what you want :)

CodePudding user response:

You want to keep a history of your feeds. All you need to do is adding the current feed into a list of feeds. Hard to do exactly what you need without your current code, but here's a try.

code00.py:

#!/usr/bin/env python

import sys
import time


def new_strings_feed():
    l = ["this", "is", "an" "example", "that", "return", "strings"]
    for idx, _ in enumerate(l, start=1):
        yield l[:idx]


def main(*argv):
    feeds = []
    for idx, feed in enumerate(new_strings_feed()):
        feeds.append(feed)
        print("{:2d}s: {:}".format(idx, feed))
        time.sleep(1)
    print("\nHistorical feeds: {:}".format(feeds))


if __name__ == "__main__":
    print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
                                                   64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    rc = main(*sys.argv[1:])
    print("\nDone.")
    sys.exit(rc)

Output:

[cfati@cfati-5510-0:/mnt/e/Work/Dev/StackOverflow/q069759917]> python code00.py 
Python 3.8.10 (default, Sep 28 2021, 16:10:42) [GCC 9.3.0] 064bit on linux

 0s: ['this']
 1s: ['this', 'is']
 2s: ['this', 'is', 'anexample']
 3s: ['this', 'is', 'anexample', 'that']
 4s: ['this', 'is', 'anexample', 'that', 'return']
 5s: ['this', 'is', 'anexample', 'that', 'return', 'strings']

Historical feeds: [['this'], ['this', 'is'], ['this', 'is', 'anexample'], ['this', 'is', 'anexample', 'that'], ['this', 'is', 'anexample', 'that', 'return'], ['this', 'is', 'anexample', 'that', 'return', 'strings']]

Done.
  • Related