首页 > 解决方案 > 将元组列表中元组的单个条目旋转(如“顺时针”)作为焦点

问题描述

我必须在元组列表中旋转(如“顺时针”)一个元组的单个条目。我使用这个单一的元组作为焦点。并且当标志被放置在元组列表的末尾时,它下一个应该再次位于开头。

x = []
x.append(('search','https://www.search.com','1'))   # '1' is the focus
x.append(('financials','https://www.stock-exchange.com','0'))
x.append(('fastfood','https://www.burgers.com','0'))
x.append(('tv','https://www.tv.com','0'))

print x

[('search','https://www.search.com','1'),('financials','https://www.stock-exchange.com','0'),('fastfood','https://www.burgers.com','0'),('tv','https://www.tv.com','0')]

我需要的是,这是(显示的摘要)...

 - - *
 - - 
 - - 
 - -

然后我必须将焦点切换到“下一行”......

 - - 
 - - *
 - - 
 - -

...然后...

- - 
- - 
- - *
- -

...然后...

 - - 
 - - 
 - - 
 - - *

……然后又是这个……

 - - *
 - - 
 - - 
 - -

...等等。

有时我需要在我的代码中找到焦点,并从当前放置焦点(第三个条目)的第一个和第二个条目中提取数据。有了这个“单线”,我可以找到焦点,并且可以将所有条目存储到 a、b 和 c:

a,b,c =  list(data[[index for (index, a_tuple) in enumerate(x) if a_tuple[2]=='1'][0]])

我喜欢那些“单线”。但将焦点转移到“下一行”似乎并不那么容易。

newX = []                           # Create a new List for x
if '1' in x[len(x)-1][2]:           # Determine whether the focus is currently placed on the end, and if Yes, place it to the begin
    for b in range(0,len(x),1):     # Parse
        if b == 0:                  # Set the focus on the begin
            newX.append((x[b][0],x[b][1],'1'))  # Set new focus to new list
        else:                       # No focus for all other entries
            newX.append((x[b][0],x[b][1],"0"))  # Set "no-focus" to all other entries in new list
else:                               # The focus was not on the end.  Where is it?
    a = 0                           # A little helper
    for b in range(0,len(x),1):     # Parse again
        if '1' in x[b][2]:          # Focus found
            a = b                   # Set the current tuple-number to a
            break                   # Already found... don't go on with for/next
    for b in range(0,len(x),1):     # Parse again
        if b == a+1:                # Set the new focus on next entry as it was before
            newX.append((x[b][0],x[b][1],'1'))  # Set new focus to new list
        else:                       # No Focus for all other entries
            newX.append((x[b][0],x[b][1],"0"))  # Set "no-focus" to all other entries in new list
x = newX                            # Set x with the new list
del newX,a,b                        # Save some memory

我设法做到了,但我不喜欢我的代码。这个任务看起来很简单,我认为它还必须有一个“单行”,它是嵌入在 Python 内部的,并且正是为此而设计的。我有 v2.7。任何人的想法?

标签: rotationtuplesfocus

解决方案


我想我明白了

    lst = {
        0: ("0","search", "https://www.search.com"),
        1: ("0", "financials", "https://www.stock-exchange.com"),
        2: ("*", "fastfood", "https://www.burgers.com"),
        3: ("0", "tv", "https://www.tv.com"),
    }
    
    def next_selected(lst, cur_selected):
        return (cur_selected + 1) % len(lst)
    
    for index, key in enumerate(lst):               # parse
        if lst[index][0] == "*":                    # found focus
            break
    
    lst[index] = "0",lst[index][1],lst[index][2]    # delete old focus
    
    selected = index                                # use index from above parsing
    selected = next_selected(lst, selected)         # will wrap around
    
    lst[selected] = '*',lst[selected][1],lst[selected][2]   # store focus to selected
    
    a,b = lst[selected][1],lst[selected][2]         # get values of selected

推荐阅读