The main problem is efficiency and creating dupes of dupes.
example inputs: 2 lists:
names = ['brian', 'adam', 'mickey', 'brian', 'mouse', 'barbara', 'mouse']
files = ['brian the wise', 'adam not interesting', 'mickey the fatty', 'brian intelligent', 'mouse notmikey', 'barbara smartie', 'mouse clicknotclik']
I tried method:
for name in names:
for j in range(len(names)):
if not names.index(name)==j:
if name == names[j]:
number = names.index(name)
a = open(title + ".txt", 'a')
a.write('\n' + str(files[number]) + str(files[j])+'\n')
a.close()
And it worked but not really efficient . I got output with duplicates.
The names and files types are lists.
Names contains one word and files contains this name and some data .
I need to find the same names (that's why I created another list called Names) in Names, when it finds it, it should create a txt with whole data from list files .
I know it's pretty complicated but I really tried to make my target clear.
Edit:
Alright, the desired output is:
brian the wise
brian the intelligent
mouse notmikey
mouse clicknotclik
When the real output is:
brian the wise
brian intelligent
brian the wise
brian intelligent
mouse notmikey
mouse clicknotclik
mouse notmikey
mouse clicknotclik
I know it's because it's checking the second Brian but i want this to write it just once if it found it.
You can do so with comprehension lists
This will try to see for each element of files
if it contains at least one element from names
.
The list(set())
clears duplicates
with open(title + '.txt', 'a') as f:
[f.write("\n{}\n".format(entry)) for entry in files for name in list(set(names)) if name in entry]
Without comprehension lists:
with open(title + '.txt', 'a') as f:
for name in list(set(names)):
for entry in files:
f.write("\n{}\n".format(entry))
You can store all indexes of name into a dict and write to file only "information" duplicate entries:
names = ["brian", "adam", "mickey", "brian", "mickey", "mouse", "brian"]
files = ["brian the wise", "adam not interesting", "mickey the fatty", "brian intelligent",
"mickey the creepy", "mouse notmikey", "brian the mad"]
title = "omg_what_a_weird_task"
with open(title + ".txt", "a") as f:
tmp = {}
for i, name in enumerate(names):
if name not in tmp:
tmp[name] = [i]
else:
tmp[name].append(i)
for name, indexes in tmp.items():
if len(indexes) > 1:
f.write("\n".join(files[i] for i in indexes))
f.write("\n")
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.