Reputation: 159
I have a list where each item is a list containing a sentence and a score for each sentence.
my_list=[[0,
'ALL TEXT IS RELEVANT\r \r Good day, everyone, and welcome to this Apple Incorporated first quarter fiscal year 2017 earnings release conference call.'],
[-1, "Today's call is being recorded."],
[0,
'At this time for opening remarks and introductions, I would like to turn the call over to Nancy Paxton, Senior Director of Investor Relations.'],
[-1, "Please go ahead, ma'am."],
[-1, 'Thank you.'],
[0, 'Good afternoon and thanks to everyone for joining us today.'],
[1,
"Speaking first is Apple CEO Tim Cook, and he'll be followed by CFO Luca Maestri."],
[0, 'And after that, we will open the call to questions from analysts.'],
etc...
I want to print a sentence only if it has a specific score. At the same time, I also want to print the sentence before and after it.
Something of the nature of:
for line in my_list: if line[0]==1: print(the line before, line, the line after)
output: 'Good afternoon and thanks to everyone for joining us today.' Speaking first is Apple CEO Tim Cook, and he'll be followed by CFO Luca Maestri. And after that, we will open the call to questions from analysts.
How can I do this?
Upvotes: 0
Views: 119
Reputation: 1624
my_list=[['nice day', '100'],
['I think this is a good idea', '90'],
['very nice', '85'],
['very well done', '100'],
["I don't understand", '85'],
['very nice job', '93']
]
#getting the only strings
only_str=list(map(lambda x:x[0],my_list))
print(only_str)
for idx,item in enumerate(my_list):
if int(item[1])>=90:
if not idx:
#for 0 only
print(only_str[idx],only_str[idx+1],sep=' , ')
elif idx==len(my_list)-1:
#for last item only
print(only_str[idx-1],only_str[idx],sep=' , ')
else:
#for other index
print(only_str[idx-1],only_str[idx],only_str[idx+1],sep=' , ')
Upvotes: 0
Reputation: 96
you can use this code first you need to sort it by the score then you can run for loop on list's index.
my_list = [['nice day', '100'], ['I think this is a good idea', '90'], ['very nice', 85], ['very well done', '100'],
['I don\'t understand', '85'], ['very nice job', '93']]
my_list.sort(key=lambda a:-int(a[1]))
for i in range(0, len(my_list)):
if int(my_list[i][1]) == 90:
if i - 1 >= 0 and i+1 < len(my_list):
print(my_list[i-1][0], ' , ', my_list[i][0], ' , ', my_list[i+1][0])
elif i - 1 >= 0 and not (i+1 < len(my_list)):
print(my_list[i - 1][0], ' , ', my_list[i][0])
elif not (i - 1 >= 0) and (i+1 < len(my_list)):
print(my_list[i][0], ' , ', my_list[i + 1][0])
Upvotes: 0
Reputation: 49893
Something a bit more compact:
for idx, line in enumerate(my_list):
if int(line[-1])==90:
print(", ".join([x[0] for x in my_list[max(idx-1,0):min(idx+1,len(line)+1)]]))
Upvotes: 1
Reputation: 24787
Here is one approach.
>>> my_list = [
... ["nice day", "90"],
... ["I think this is a good idea", "90"],
... ["very nice", 85],
... ["very well done", "90"],
... ["I don't understand", "85"],
... ["very nice job", "90"],
... ]
>>>
>>> prev = [None]
>>>
>>> for cur, nex in zip(my_list, my_list[1:] + [[None]]):
... if int(cur[1]) == 90:
... print(
... f"Previous Line:- {prev[0]!r}, Current Line:- {cur[0]!r}, Next Line:- {nex[0]!r}"
... )
... prev = cur
...
Previous Line:- None, Current Line:- 'nice day', Next Line:- 'I think this is a good idea'
Previous Line:- 'nice day', Current Line:- 'I think this is a good idea', Next Line:- 'very nice'
Previous Line:- 'very nice', Current Line:- 'very well done', Next Line:- "I don't understand"
Previous Line:- "I don't understand", Current Line:- 'very nice job', Next Line:- None
Upvotes: 0
Reputation: 12927
for i in range(len(my_list)):
if my_list[i][1] == '90':
lowerbound = max(i-1, 0)
print(', '.join(sentence for (sentence, score) in my_list[lowerbound:i+2]))
Upvotes: 1
Reputation: 71687
Try this:
i = 0
while i < len(my_list):
sentences = []
if my_list[i][1] == "90":
if i - 1 >= 0:
sentences.append(my_list[i - 1][0])
sentences.append(my_list[i][0])
if i + 1 < len(my_list):
sentences.append(my_list[i + 1][0])
print(", ".join(sentences))
i += 1
Output:
nice day, I think this is a good idea, very nice
Upvotes: 1