I was trying the following code snippet for a project of mine:
scrap=['Theodore von Kármán Lecture', 'Science Journey: "The Origin of Photosynthesis: A Window into the Intricate Relationship Between Life and the World Around It"', 'Science Journey: "Addressing Energy Storage Challenges: Making Electrolytes That Can Help Batteries Store More Energy"', 'Science Journey: "Defects in Crystals: The Tiniest Engineering Tool"']
for i in scrap:
words_in_scrap = i.split()
# print("words_in_scrap=",words_in_scrap)
for j in words_in_scrap:
words = j.lower()
# print(words)
for k in words:
# print(k)
if k in punc:
words = words.replace(k,"")
# print(words)
# print(k)
clean = words #corrected lot
# print(clean)
c_list = []
c_list.append(clean)
print(c_list)
I got this output:
['theodore']
['von']
['kármán']
['lecture']
['science']
['journey']
['the']
['origin']
['of']
['photosynthesis']
['a']
['window']
['into']
['the']
['intricate']
['relationship']
['between']
['life']
['and']
['the']
['world']
['around']
['it']
['science']
['journey']
['addressing']
['energy']
['storage']
['challenges']
['making']
['electrolytes']
['that']
['can']
['help']
['batteries']
['store']
['more']
['energy']
['science']
['journey']
['defects']
['in']
['crystals']
['the']
['tiniest']
['engineering']
['tool']
I want all of these output lists to be put into one list, like:
[‘theodore’,’von’,’karman’,’science’,………….]
I understand that these individual outputs were produced due to loops, I’ve tried to do .append() for doing this and ended up with the c_list and they turned out to be independent lists. Any solution to this will be highly valued and appreciated. Thanks!
Advertisement
Answer
scrap=['Theodore von Kármán Lecture', 'Science Journey: "The Origin of Photosynthesis: A Window into the Intricate Relationship Between Life and the World Around It"', 'Science Journey: "Addressing Energy Storage Challenges: Making Electrolytes That Can Help Batteries Store More Energy"', 'Science Journey: "Defects in Crystals: The Tiniest Engineering Tool"']
punc = [".","/",":"]
c_list = []
for i in scrap:
words_in_scrap = i.split()
# print("words_in_scrap=",words_in_scrap)
for j in words_in_scrap:
words = j.lower()
# print(words)
for k in words:
# print(k)
if k in punc:
words = words.replace(k,"")
# print(words)
# print(k)
clean = words #corrected lot
# print(clean)
c_list.append(clean)
print(c_list)
I assumes punc
is tab list of punctuation.
Is this working for you ?
output :
[‘theodore’, ‘von’, ‘kármán’, ‘lecture’, ‘science’, ‘journey:’, ‘”the’, ‘origin’, ‘of’, ‘photosynthesis:’, ‘a’, ‘window’, ‘into’, ‘the’, ‘intricate’, ‘relationship’, ‘between’, ‘life’, ‘and’, ‘the’, ‘world’, ‘around’, ‘it”‘, ‘science’, ‘journey:’, ‘”addressing’, ‘energy’, ‘storage’, ‘challenges:’, ‘making’, ‘electrolytes’, ‘that’, ‘can’, ‘help’, ‘batteries’, ‘store’, ‘more’, ‘energy”‘, ‘science’, ‘journey:’, ‘”defects’, ‘in’, ‘crystals:’, ‘the’, ‘tiniest’, ‘engineering’, ‘tool”‘]