旋转(如 "clockwise")元组列表中元组的单个条目作为焦点
Rotate (like "clockwise") a singe entry of a tuple in a list of tuples as a Focus
我必须旋转(如“顺时针”)元组列表中元组的单个条目。我使用这个单个元组作为焦点。
当标志被放置在元组列表的末尾时,它接下来应该再次出现在开始处。
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。有人有想法吗?
如果我没理解错的话,你需要简单的模 (%
) 运算符。
从元组中删除第三项,创建名为 selected
的新变量,它将保存所选项目的值:
lst = [
("search", "https://www.search.com"),
("financials", "https://www.stock-exchange.com"),
("fastfood", "https://www.burgers.com"),
("tv", "https://www.tv.com"),
]
selected = 0
def next_selected(lst, cur_selected):
return (cur_selected + 1) % len(lst)
selected = next_selected(lst, selected) # will wrap around
那么获取选中项就是:
a, b = lst[selected]
我想我明白了
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
我必须旋转(如“顺时针”)元组列表中元组的单个条目。我使用这个单个元组作为焦点。 当标志被放置在元组列表的末尾时,它接下来应该再次出现在开始处。
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。有人有想法吗?
如果我没理解错的话,你需要简单的模 (%
) 运算符。
从元组中删除第三项,创建名为 selected
的新变量,它将保存所选项目的值:
lst = [
("search", "https://www.search.com"),
("financials", "https://www.stock-exchange.com"),
("fastfood", "https://www.burgers.com"),
("tv", "https://www.tv.com"),
]
selected = 0
def next_selected(lst, cur_selected):
return (cur_selected + 1) % len(lst)
selected = next_selected(lst, selected) # will wrap around
那么获取选中项就是:
a, b = lst[selected]
我想我明白了
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