我想减少此词典列表以获取重复项的最新记录,其中重复项由相同的 project_name 和相同的 feature_group_name 确定。我该怎么做呢?
我现在的操作方式如下,但是我敢肯定,没有熊猫的需求还有更好的方法:
from stack import stack
def getAction():
'''
Write docstring to describe function
Inputs: no arguments taken by this function.
Returns: string containing users input if he has chosen one of the correct input options
'''
correct_input=False
while correct_input==False:
user_input=input("Enter = to enter a URL, < to go back, > to go forward, q to quit: ")
if user_input =='q' or user_input =='>' or user_input =='<' or user_input =='=':
correct_input=True
if correct_input==False:
print('Invalid entry.')
return user_input
def goToNewSite(current, pages):
'''
Write docstring to describe function
Inputs: index of the current website (int), reference to list containing the webpage addresses to go back and forth between
Returns: address inputted by user as a string
'''
new_web_address=input('Enter a new website address ')
for i in range(current+1,len(pages)):
pages.pop()
pages.append(new_web_address)
#pages[current+1]=new_web_address
#pages=pages[:current+1]
return current+1
def goBack(current, pages):
'''
Write docstring to describe function
Inputs: index of the current website (int),reference to list containing the webpage addresses to go back and forth between
Returns: index of the previous webpage (int)
'''
# alternatively this could be done by checking if current-1>=0
if current-1>=0:
return current-1
else:
print('Cannot go backward')
return current
def goForward(current, pages):
'''
Write docstring to describe function
Inputs: index of the current website (int),reference to list containing the webpage addresses to go back and forth between
Returns: index of the previous webpage (int)
'''
# alternatively this could be done by checking if current +1 in range(len(pages))
if current+1<len(pages):
return current+1
else:
print('Cannot go forward')
return current
def main():
'''
Controls main flow of web browser simulator
Inputs: N/A
Returns: None
'''
HOME = 'www.google.ca'
websites = [HOME]
currentIndex = 0
quit = False
while not quit:
print('\nCurrently viewing', websites[currentIndex])
print(websites)
action = getAction()
if action == '=':
currentIndex = goToNewSite(currentIndex, websites)
elif action == '<':
currentIndex = goBack(currentIndex, websites)
elif action == '>':
currentIndex = goForward(currentIndex, websites)
elif action == 'q':
quit = True
print('Browser closing...goodbye.')
if __name__ == "__main__":
main()
示例:
class Stack:
def __init__(self):
self.items = []
def push(self, item):
self.items.append(item)
# MODIFY: RAISE AN EXCEPTION IF THIS METHOD IS INVOKED ON AN EMPTY STACK
def pop(self):
if self.isEmpty()==True:
raise Exception('Stack is empty cannot pop')
if self.isEmpty()==False:
return self.items.pop()
# MODIFY: RAISE AN EXCEPTION IF THIS METHOD IS INVOKED ON AN EMPTY STACK
def peek(self):
if self.isEmpty()==True:
raise Exception('Stack is empty cannot peek')
if self.isEmpty()==False:
return self.items[len(self.items)-1]
def isEmpty(self):
return self.items == []
def size(self):
return len(self.items)
def show(self):
print(self.items)
def __str__(self):
stackAsString = ''
for item in self.items:
stackAsString += item + ' '
return stackAsString
所需结果:
d = pd.DataFrame(l)
sorted_df = d.sort_values('datetime', ascending=False).drop_duplicates(['project_name', 'feature_group_name'],
keep='first')
sorted_df.to_dict(orient='records')
答案 0 :(得分:0)
l = [i for n, i in enumerate(l) if i not in l[n + 1:]]