[英]Use Data index to print all items in a row in a Python List
I have a Python List and I have implemented a click listener for the items in, if i have the index of an item in that list,我有一个 Python 列表,并且我已经为其中的项目实现了一个点击侦听器,如果我在该列表中有一个项目的索引,
Eg, I can get the text ( 'text': '0000' ), where index = 3 or the text ('text': '100') where index = 24, how can use an index of the item to print all the other data in row in which that index lies?例如,我可以获得文本 ('text': '0000'),其中 index = 3 或文本 ('text': '100') where index = 24,如何使用项目的索引来打印所有该索引所在的行中的其他数据?
data = [{'text': '400'}, {'text': 'Dairy'}, {'text': '0000'}, {'text': 'Milkshake'}, {'text': '500'}, {'text': 'Available'}, {'text': '0.0'}, {'text': '1'}, {'text': 'Kisumu'},
{'text': '500'}, {'text': 'Electronics'}, {'text': '1111'}, {'text': 'Flower'}, {'text': '700'}, {'text': 'Available'}, {'text': '50'}, {'text': '2'}, {'text': 'Kisumu'},
{'text': '500'}, {'text': 'Electronics'}, {'text': '8888'}, {'text': 'Eggs'}, {'text': '900'}, {'text': 'Unavailable'}, {'text': '100'}, {'text': '5'}, {'text': 'Kisumu'}]
Another way...其它的办法...
data = [{'text': '400'}, {'text': 'Dairy'}, {'text': '0000'}, {'text': 'Milkshake'}, {'text': '500'}, {'text': 'Available'}, {'text': '0.0'}, {'text': '1'}, {'text': 'Kisumu'},
{'text': '500'}, {'text': 'Electronics'}, {'text': '1111'}, {'text': 'Flower'}, {'text': '700'}, {'text': 'Available'}, {'text': '50'}, {'text': '2'}, {'text': 'Kisumu'},
{'text': '500'}, {'text': 'Electronics'}, {'text': '8888'}, {'text': 'Eggs'}, {'text': '900'}, {'text': 'Unavailable'}, {'text': '100'}, {'text': '5'}, {'text': 'Kisumu'}]
rows = [data[x:x+9] for x in range(0,len(data),9)] # split to rows
idx = 24
print(rows[idx//9])
Output输出
[{'text': '500'}, {'text': 'Electronics'}, {'text': '8888'}, {'text': 'Eggs'}, {'text': '900'}, {'text': 'Unavailable'}, {'text': '100'}, {'text': '5'}, {'text': 'Kisumu'}]
def get_row_range(index:int, num_cols:int) -> range:
# index - index, which you want the row of
# num_cols - number of columns in your table
row = int(index/num_cols) # the row you want
return range(row * num_cols, (row+1)*num_cols) # the range which that index lies
# Example usage of querying the index '10'
clicked_index = 10 # in the event handler get which index was clicked
num_cols = 9 # your example has 9 columns
for i in get_row_range(clicked_index, num_cols):
print(data[i]["text"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.