[英]trying to find out places located within list
For example I am looking for a way to search every spot in a list to see where an object is located. 例如,我正在寻找一种方法来搜索列表中的每个点,以查看对象的位置。 Some example psuedocode could go:
一些示例的伪代码可以去:
for every column in current row:
if column is the first one:
do this
if column in the last one:
do that
else:
find the previous row and columns place
Basically I am at a standstill so any insight would be helpful thanks 基本上我处于停滞状态,所以任何见解都会有所帮助,谢谢
EDIT Sample Code: 编辑示例代码:
for row in range(0,h+1):
newrow=[]
if row==0:
newrow=[1]
elif row==1:
newrow=[1,1]
else:
for column,x in enumerate(row):
if column==0:
newrow.append(1)
elif column==len(row)-1:
newrow.append(1)
else:
newrow.append(2)
Are you looking for list.index
? 你在寻找
list.index
吗?
l = ['foo', 'bar', 'baz']
i = l.index('bar')
# i is 1, because l[1] == 'bar'
If you need special handling based on whether it's the first or last item: 如果您需要根据它是第一个还是最后一个项目进行特殊处理:
# using i from above
if i == 0:
# it's the first item
elif i == len(l) - 1:
# it's the last item
else:
# it's neither first nor last
Or if you need to process all items anyway, consider using enumerate
to keep track of indices throughout the loop: 或者,如果您仍需要处理所有项目,请考虑使用
enumerate
来跟踪整个循环中的索引:
for i, x in enumerate(mylist):
if i == 0:
# x is the first item
elif i == len(mylist)-1:
# x is the last item
else:
# x is in the middle
bardockyo: bardockyo:
The problem appears to be that you don't have a list in row... when you run the code 'for row in range(0,h+1):', row will always be an integer with a value between greater than or equal to 0, and less than or equal to h. 问题似乎是你没有行中的列表...当你为范围内的行(0,h + 1)运行代码时:',行总是一个值大于的整数或等于0,小于或等于h。
Are you trying to read a file a row at a time, and keep track of the row numbers? 您是否尝试一次读取一个文件,并跟踪行号? If so, you should use a separate counter to track the row number...
如果是这样,您应该使用单独的计数器来跟踪行号...
I can't quite follow what you're trying to accomplish, so I can't even generate code to help you... 我不能完全遵循你想要完成的任务,所以我甚至无法生成代码来帮助你......
Added in response to bardockyo's comment: 添加以回应bardockyo的评论:
I believe this accomplishes your goal: 我相信这可以实现你的目标:
# Setting h to a value so that I can use your 'range' line.
h = 5
# Create a blank dictionary object to store the row data.
rows = {}
for row_number in range(0,h+1):
row_data = []
# range isn't inclusive of the end value by default, to 'make it work', we
# must add 1 to the row_number.
for val in range(0,row_number+1):
if val == 0 or val == row_number:
# Determine if the value in 'val' is either the first number or
# last number in this row, and if so append '1' to the list.
row_data.append(1)
else:
# Determine if the value in 'val' is not the first number or last
# number in this row, and if so append '2' to the list.
row_data.append(2)
# Store the row data in the row dictionary, with the key of 'row_number'.
rows[row_number] = row_data
# Iterate through the dictionary. There's no guarantee as to the order
# returned by the 'keys()' function, so I use sorted() to ensure it's in
# numerical order.
for row_num in sorted(rows.keys()):
print 'Row Number %d contains the list:' % row_num,
for val in rows[row_num]:
print '%d' % val,
print ''
# There are better (read: cleaner) ways to format the output for printing,
# but they can be hard to read.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.