简体   繁体   English

试图找出列表中的位置

[英]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.

相关问题 试图找出一种在列表中查找连续元素的方法 - Trying to figure out a way to find consecutive elements in a list 我试图通过列表理解找出下面显示的结果: - I am trying to find out the result shown below with list comprehension: 意外错误:尝试访问 function 中的列表时出现“IndexError:列表索引超出范围” - Unexpected error: "IndexError: list index out of range" when trying to access a list within a function Python-尝试在循环中为列表项分配值时列表分配索引超出范围 - Python - list assignment index out of range when trying to assign value to list item within loop Matplotlib轴标签:如何找出它们的位置? - Matplotlib axis labels: how to find out where they will be located? 尝试在列表中创建列表 - Trying to create a list within a list 有关数字列表和间隔列表,请查找每个数字所在的时间间隔 - For a list of numbers and a list of intervals, find the interval in which each number is located 试图将除法方程的结果输出到小数点后两位? - Trying to get the results of division equation to two decimal places in out put? 如何阅读位于另一个班级内的列表? - How do I read a list that is located within another class? IndexError:与目标相比,尝试在列表中查找整数对的总和时列出索引超出范围 - IndexError: list index out of range while trying to find sum of the pair of integer in a list when compared to a target
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM