如何使列表与其他列表可比较



我有这样的代码:

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)

显示如下输出:

['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

我正在尝试以这种方式将输出显示为列表:

[['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]]

的原因是因为我想能够使用这段代码:

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))

我希望能够比较两个列表并在列表的列表中找到一个相似的列表。我一直在尝试各种方法,但没有一个我尝试过的工作,现在我被困在这里。我真的希望我足够清楚它是什么,我想要的,但万一我不是,我想做的是能够使用多个列表组成的列表来找到一个特定的列表,例如:

查找此列表:

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

:

[['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]]

变化

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

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

这将把元组扩展到第一个列表元素中,然后使用count作为下一个元素。

相关内容

  • 没有找到相关文章

最新更新