A
A
alex_1242020-05-18 10:32:08
Python
alex_124, 2020-05-18 10:32:08

What's the easiest way to write code to compare lists?

Hello! I embarked on the path of learning Python and decided to write a program for review.

There is a list of T from multi-letters (each letter is a separate element). It is necessary to go through this entire list, which is not yet correctly divided (the line break character sometimes breaks sentences) and output the resulting list into separate variables to simplify further work. Now my logic is as follows:
1. A list is selected from a huge list ( eh :( ) according to the rule for finding the division symbol;
2. Since the list is sometimes not correctly divided, you need to find where the text is broken and combine the lists

That is, the output should be that each variable has an unterminated list (text)

Tried to find the index of the beginning (end of another) text like this:

test1 = ['К', 'a', 'р', 'т', 'о', 'ч', 'к', 'и', ' ', 'т', 'о', 'в', 'а', 'р', 'о', 'в', ' ', 'К', 'а', 'р', 'о']
test2 = ['К', 'а', 'р', 'о']

with open('C:/Test_python/file_' + str(random.randrange(1,100,1)) + '.txt', 'w', encoding='utf-8') as f:
    for i in range(len(test1)):
        if test1[i] == test2[0]:
            if test1[i+1] == test2[1]:
                if test1[i+2] == test2[2]:
                    if test1[i+3] == test2[3]:
                        k = test2[0:]
                        index = test1.index(k)
                        f.write(''.join(test1) + ' new' + str(index) + str(i) + '\n\n')
                    else: print('Sorry')

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question