【问题标题】:separate list into smaller list and go through each smaller list python将列表分成较小的列表并遍历每个较小的列表 python
【发布时间】:2015-11-30 13:04:06
【问题描述】:

我有一个这样的列表

results = [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0]

我想把这个列表分开,把项目分成4个元素:

size = 4
group_list_4 = [results[i:i+size] for i  in range(0, len(results), size)]
print "List:" , group_list_4

这个命令的结果是这样的:

List: [[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1], [1, 0, 0, 0], [1, 0, 0, 0]]

在每个 4 组中,我必须检查 1 个元素在哪里,因此如果 4 组中的第一个元素是 1,则 return "first" 如果第二个 return "second" 直到 4 并且该值放入 json_obj['value_1_in_list'] 中。

lista = []
for record in records:
            json_obj = {}
            json_obj['filename'] = filename
            json_obj['value_1_in_list'] =  put element 1 on list
            lista.append(json_obj)

在上面的代码中,我有一个名为 lista 的列表,我在其中创建 JSON obj,条件 for record in records: 将执行 17 次,我还有 17 个包含 4 个元素的小列表。每次执行 for 循环都会创建一个 JSON。 现在我想在这个for loop 中包含 1(first,second,third,fourth) 的值在一个 4elements 列表中,下一次执行 for 循环以包含 results list 中的另一个小列表包含 4elements 我该怎么做,有什么帮助吗?

Lists with four elements always contain only one 1.

【问题讨论】:

标签: python json list if-statement for-loop


【解决方案1】:

使用字典和列表的索引方法

代码:

test=[[1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1],
    [0, 0, 1, 0], [1, 0, 0, 0], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1],
    [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 0, 1], [0, 0, 0, 1], [0, 0, 0, 1],
    [1, 0, 0, 0], [1, 0, 0, 0],[0,0,0,0]]
indexer={0:"first : ",1:"two : ",2:"three : ",3:"four : "}
for val in test:
    try:
        print indexer[val.index(1)],val
    except ValueError:
        print "No 1",val

输出:

first :  [1, 0, 0, 0]
first :  [1, 0, 0, 0]
four :  [0, 0, 0, 1]
first :  [1, 0, 0, 0]
four :  [0, 0, 0, 1]
three :  [0, 0, 1, 0]
first :  [1, 0, 0, 0]
first :  [1, 0, 0, 0]
four :  [0, 0, 0, 1]
four :  [0, 0, 0, 1]
four :  [0, 0, 0, 1]
first :  [1, 0, 0, 0]
four :  [0, 0, 0, 1]
four :  [0, 0, 0, 1]
four :  [0, 0, 0, 1]
first :  [1, 0, 0, 0]
first :  [1, 0, 0, 0]
No 1 [0, 0, 0, 0]

Warning this may not work as expected for cases where there are more then one 1 in the list and OP has not told there will be situation like that

【讨论】:

  • 你的 else 永远不会到达,如果没有 1 你会得到一个错误
  • @Vignesh Kalai 谢谢你的回答!现在这段代码如何包含第一个结果在 json_obj['value_1_in_list'] 等直到条件被执行?
  • @PadraicCunningham 为什么会这样?
  • @PadraicCunningham 我在这里查看if 1 in val: 并查看我的输出No 1 [0, 0, 0, 0]
  • @PadraicCunningham 是的,我们可以这样做 :)
【解决方案2】:

这可以通过非常简单的方式实现。您有明确定义的标准(根据输入返回值)。这只是一个函数的规范。编写一个函数 (one_location) 并将其映射到您的数据集。

results = [1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0]

size = 4
group_list_4 = [results[i:i+size] for i  in range(0, len(results), size)]

def one_location(l):
    if l[0] == 1: return "first"
    elif l[1] == 1: return "second"
    elif l[2] == 1: return "third"
    elif l[3] == 1: return "fourth"

result_list = map(one_location, group_list_4)

list(result_list)

# ['first',
#  'first',
#  'fourth',
#  'first',
#  'fourth',
#  'third',
#  'first',
#  'first',
#  'fourth',
#  'fourth',
#  'fourth',
#  'first',
#  'fourth',
#  'fourth',
#  'fourth',
#  'first',
#  'first',
#  'first',
#  'first']

【讨论】:

  • 感谢您的回答!现在这段代码如何包含第一个结果在 json_obj['value_1_in_list'] 等直到条件被执行?
  • @EstinaEsitna 我不明白你的问题。
【解决方案3】:
digit_word_map = {1: 'one', 2: 'two', 3: 'three', 4: 'four'}
chunks =  [results[i:i + 4] for i in range(len(results)) if i % 4 == 0]
for chunk in chunks:
    print digit_word_map[chunk.index(1) + 1]

【讨论】:

    【解决方案4】:

    使用itertools.compress: 适用于子列表中的多个1s

    >> q = ['1st','2nd', '3rd', '4th']
    >> for i in range(0,len(results),4):
           f.append(compress(q,results[i:i+4]) or ["null"])
    
    # [['1st', '4th'], ['1st'], ['4th'], ['1st'], ['4th'], ['3rd'], ['1st'], ['1st'], ['4th'], ['4th'], ['4th'], ['1st'], ['4th'], ['4th'], ['4th'], ['1st'], ['1st'], ['1st'], ['1st']]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-19
      • 2012-07-12
      • 2012-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多