Home > Enterprise >  how to make list comparable with other lists
how to make list comparable with other lists

Time:11-11

i have this code:

import csv
from collections import Counter

with open('C:\\Users\\ntonto\\Documents\\Currencies\\BTC-USD.csv', encoding='utf-8=sig') as csvfile:
    r = csv.DictReader(csvfile)
    count = 0
    fsa = []
    for row in r:
        count = count   1
        fsa.append(row['Open'])
        if count > 10:
            continue

with open('C:\\Users\\ntonto\\Documents\\Currencies\\BTC-USD.csv', encoding='utf-8=sig') as csvfile:
    r = csv.DictReader(csvfile)
    count = 0
    fsb = []
    for row in r:
        count = count   1
        fsb.append(row['Close'])
        if count > 10:
            continue


kol = []
for i in range(len(fsa)):
    if fsa[i] < fsb[i]:
        kol.append('1')
    else:
        kol.append('0')

start = 0
end = len(fsa)
j = [ ]
for x in range(start, end, 6):
    m = fsb[x:x 6]
    t = kol[x:x 6]
    if m[0] < m[-1]:
        t.append('up')
    else:
        t.append('down')
    j.append(t)

jol = []
counter = Counter(tuple(j) for j in j)
for members, count, in counter.items():
    print(list(members), count)

which presents this output:

['0', '0', '0', '1', '0', '0', 'up'] 2
['0', '0', '1', '0', '1', '0', 'up'] 1
['0', '0', '1', '0', '0', '1', 'down'] 2
['0', '1', '1', '0', '1', '0', 'up'] 3
['0', '1', '0', '1', '0', '0', 'up'] 2
['1', '1', '0', '1', '0', '1', 'down'] 2
['0', '0', '1', '1', '1', '1', 'up'] 2
['1', '0', '0', '1', '0', '1', 'up'] 1
['0', '0', '0', '1', '1', '0', 'up'] 2
['1', '1', '0', '0', '0', '0', 'up'] 1
['0', '0', '1', '1', '0', '1', 'down'] 1
['1', '0', '0', '0', '0', '1', 'down'] 1
['0', '0', '0', '0', '1', '1', 'up'] 1

i am trying to get the output to be presented as a list in this manner:

[['0', '0', '0', '1', '0', '0', 'up', 2],
['0', '0', '1', '0', '1', '0', 'up', 1],
['0', '0', '1', '0', '0', '1', 'down', 2],
['0', '1', '1', '0', '1', '0', 'up', 3],
['0', '1', '0', '1', '0', '0', 'up', 2],
['1', '1', '0', '1', '0', '1', 'down', 2],
['0', '0', '1', '1', '1', '1', 'up', 2],
['1', '0', '0', '1', '0', '1', 'up', 1],
['0', '0', '0', '1', '1', '0', 'up', 2],
['1', '1', '0', '0', '0', '0', 'up', 1],
['0', '0', '1', '1', '0', '1', 'down', 1],
['1', '0', '0', '0', '0', '1', 'down', 1],
['0', '0', '0', '0', '1', '1', 'up', 1]]

the reason is because i want to be able to use this code:

def find_list(list1, list2):
    index = -1
    occ = 0
    for i, l in enumerate(list2):
        if l[:len(list1)] == list1:
            if l[-1] > occ:
                index = i
                occ = l[-1]
    if index == -1:
        return "The 1st list doesn't appear in the 2nd one."
    else:
        return f"The 1st list appears in the 2nd one at index {index} with a number of occurences equal to {occ}."


print(find_list(listB, listA))

I to be able to compare the two lists and find a similar list in the list of lists. i have been trying various methods but non of the ones I tried work and now I am stuck here. I truly hope i was clear enough on what it is i want but in case i was not, what i want to do is to be able to use a list made up of multiple lists to find a specific list for example:

to find this list:

['1', '1', '0', '0', '0', '0']

in this list of lists:

 [['0', '0', '0', '1', '0', '0', 'up', 2],
    ['0', '0', '1', '0', '1', '0', 'up', 1],
    ['0', '0', '1', '0', '0', '1', 'down', 2],
    ['0', '1', '1', '0', '1', '0', 'up', 3],
    ['0', '1', '0', '1', '0', '0', 'up', 2],
    ['1', '1', '0', '1', '0', '1', 'down', 2],
    ['0', '0', '1', '1', '1', '1', 'up', 2],
    ['1', '0', '0', '1', '0', '1', 'up', 1],
    ['0', '0', '0', '1', '1', '0', 'up', 2],
    ['1', '1', '0', '0', '0', '0', 'up', 1],
    ['0', '0', '1', '1', '0', '1', 'down', 1],
    ['1', '0', '0', '0', '0', '1', 'down', 1],
    ['0', '0', '0', '0', '1', '1', 'up', 1]]

CodePudding user response:

Change

for members, count, in counter.items():
    print(list(members), count)

to

for members, count, in counter.items():
    print([*members, count])

This will spread the tuple into the first list elements, and then use count as the next element.

  • Related