我有一个包含子组的列表。我根据基金名称的相似性进行了分组。如何选择/识别列表中的组?
mylist = [
{'American Funds Cnsrv Gr & Inc A', 'American Funds Cnsrv Gr & Inc R-5E', 'American Funds Cnsrv Gr & Inc F-1', 'American Funds Cnsrv Gr & Inc R-6', 'American Funds Cnsrv Gr & Inc R-5', 'American Funds Cnsrv Gr & Inc F-3', 'American Funds Cnsrv Gr & Inc R-4', 'American Funds Cnsrv Gr & Inc F-2'}
{'MFS Conservative Allocation R3', 'MFS Conservative Allocation R2', 'MFS Conservative Allocation R6', 'MFS Conservative Allocation I', 'MFS Conservative Allocation A', 'MFS Conservative Allocation R4'},
{'American Funds Cnsrv Gr & Inc 529-A', 'American Funds Cnsrv Gr & Inc 529-F-3', 'American Funds Cnsrv Gr & Inc 529-F-2', 'American Funds Cnsrv Gr & Inc 529-F-1'}]例如,如何调用/选择第二组:{'MFS Conservative Allocation R3', 'MFS Conservative Allocation R2', 'MFS Conservative Allocation R6', 'MFS Conservative Allocation I', 'MFS Conservative Allocation A', 'MFS Conservative Allocation R4'}
我的最终目标是在每一组中只保留一只基金,这是最好的股票类别,但首先,我需要一种方法来识别每个分组。
谢谢
发布于 2022-07-27 16:19:21
您可以对列表索引进行切片或插入:
mylist = [{'American Funds Cnsrv Gr & Inc A', 'American Funds Cnsrv Gr & Inc R-5E', 'American Funds Cnsrv Gr & Inc F-1', 'American Funds Cnsrv Gr & Inc R-6', 'American Funds Cnsrv Gr & Inc R-5', 'American Funds Cnsrv Gr & Inc F-3', 'American Funds Cnsrv Gr & Inc R-4', 'American Funds Cnsrv Gr & Inc F-2'}, {'MFS Conservative Allocation R3', 'MFS Conservative Allocation R2', 'MFS Conservative Allocation R6', 'MFS Conservative Allocation I', 'MFS Conservative Allocation A', 'MFS Conservative Allocation R4'}, {'American Funds Cnsrv Gr & Inc 529-A', 'American Funds Cnsrv Gr & Inc 529-F-3', 'American Funds Cnsrv Gr & Inc 529-F-2', 'American Funds Cnsrv Gr & Inc 529-F-1'}]
MFSC = mylist[1]
print(MFSC)输出:
{'MFS Conservative Allocation R2', 'MFS Conservative Allocation I', 'MFS Conservative Allocation A', 'MFS Conservative Allocation R3', 'MFS Conservative Allocation R4', 'MFS Conservative Allocation R6'}但是list的元素是一个集合,所以set元素可以在每次使用它们时以不同的顺序出现,并且不能被索引或键引用。
MFSC = list(mylist[1])
print(MFSC)输出:
['MFS Conservative Allocation R4', 'MFS Conservative Allocation R6', 'MFS Conservative Allocation I', 'MFS Conservative Allocation A', 'MFS Conservative Allocation R3', 'MFS Conservative Allocation R2']https://stackoverflow.com/questions/73141067
复制相似问题