I have my txt file something like this.
[0, "we break dance not hearts by Short Stack is my ringtone.... i LOVE that !!!.....\n"]
[1, "I want to write a . I think I will.\n"]
[2, "@va_stress broke my twitter..\n"]
[3, "\" "Y must people insist on talking about stupid politics on the comments of a bubblegum pop . Sorry\n"]
[4, "aww great "Picture to burn"\n"]
I have a some code which want to access the 2nd element of each array. When I use the code from Get the nth element from the inner list of a list of lists in Python It is giving each characters but not the entire string.
What could be the best way to make a loop for getting second element?
My code is something like this.
ALl the tweets are in the tweets[] list.
cluster = []
for idx, cls in enumerate(km.labels_):
if cls == 1:
# printing cluster 2 data.
# print tweets from the tweets array. like the entire line. But I
# want to get the String here not the entire line.
print tweets[idx]
cluster.append(tweets[idx])
Here, idx element is used to get specific queries. so tweets[idx] will print specific queries from the text file but It is printing the entire line like [2, "@va_stress broke my twitter..\\n"] and I want string element only.
I guess what you are wanting is the string from each list. I am assuming you already would have a list of list parameter with each list from your text file. hence you can apply this to get the values.
list_of_strings = filter(lambda x:x[1], list_of_lists)
You can try any of these method :
data=[[0, "we break dance not hearts by Short Stack is my ringtone.... i LOVE that !!!.....\n"],
[1, "I want to write a . I think I will.\n"],
[2, "@va_stress broke my twitter..\n"],
[3, "\" "Y must people insist on talking about stupid politics on the comments of a bubblegum pop . Sorry\n"],
[4, "aww great "Picture to burn"\n"]]
print(list(map(lambda x:x[1].strip(),data)))
or
print([i[1].strip() for i in data])
output:
['we break dance not hearts by Short Stack is my ringtone.... i LOVE that !!!.....', 'I want to write a . I think I will.', '@va_stress broke my twitter..', '" "Y must people insist on talking about stupid politics on the comments of a bubblegum pop . Sorry', 'aww great "Picture to burn"']
Try this:
data=eval('['+(open('file.txt').read().replace('\n', ', ')[:-2])+']')
result=[]
for i in data:
data.append(i[1])
The last 3 lines are pretty obvious, but here's what the first one does:
open('file.txt').read()
opens the file and gets the contents .replace('\\n', ', ')[:-2]
replaces the newlines with ,
, except the last, so that it's formatted like a list. Skip [:-2]
it if the last line doesn't end in a newline. '['+...+']'
adds [
and ]
for more formatting as a list. data=eval(...)
makes creates the list and assigns it to data
. Just in case here is what the final lines do:
result
i
assigned to each value in data
for the following line: data
to result
.
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.