在 Python 中提取列表元素 [英] Extract list element in Python

查看:142
本文介绍了在 Python 中提取列表元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个 python 程序.我使用以下代码为给定范围生成组合.

for k in range(0, items+1):对于范围内的 r(0, items+1):如果 (r-k) >0:res = [x for x in itertools.combinations(range(k, r), r-k)]打印资源

假设 items=4,代码产生 10 个组合

<代码> ## [(0,)]# [(0, 1)]# [(0, 1, 2)]# [(0, 1, 2, 3)]# [(1,)]# [(1, 2)]# [(1, 2, 3)]# [(2,)]# [(2, 3)]# [(3,)]#

我的问题是

  • (a) 如何检索每个组合中的每个元素,比如说,在 [(1, 2, 3)] 中,如何检索偏移量 0(即 1)处的值?

  • (b) 如何将 itertools.combinations 的返回值存储到res"中的列表数组中(例如, res[0] = [(0,)] , res[1] = [(0, 1)] ?

  • (c) 假设我想使用 map(),我怎样才能将值例如 [(0, 1)] 作为键,并为该键分配一个随机值?

解决方案

  • a) 使用索引:

    <预><代码>>>>[(1, 2, 3)][0][0]1

  • b) 我不是 100% 理解这个问题,但你可以使用 list(itertools.combinations(...))

  • c) 我认为您误解了 map() 的作用.map(str, [1, 2, 3]) 等价于:

    [str(i) for i in [1, 2, 3]]

如果你想给 [(0, 1)] 一个值,你可以使用字典,但你必须使用 (0, 1) 而不是[(0, 1)] 因为否则你会得到 TypeError: unhashable type: 'list'.如果你想要一个随机值",我想你可以使用 random 模块:

随机导入{(0, 1) : random.randint(1,10)} # 这只是一个例子,当然

<小时>

要将所有输出存储在一个列表中,您可以使用大量列表推导式:

<预><代码>>>>[list(itertools.combinations(range(x, i), i-x)) for x in range(0, items+1) for i in range(0, items+1) if (i-x) >0][[(0,)], [(0, 1)], [(0, 1, 2)], [(0, 1, 2, 3)], [(1,)], [(1, 2))], [(1, 2, 3)], [(2,)], [(2, 3)], [(3,)]]

This is my first python program. I use the below code to generate combinations for a given range.

for k in range(0, items+1):        
    for r in range(0, items+1):        
        if (r-k) > 0:
            res = [x for x in itertools.combinations(range(k, r), r-k)]            
            print res

Let say items=4, the code produce 10 combinations

            #     
            # [(0,)]
            # [(0, 1)]
            # [(0, 1, 2)]
            # [(0, 1, 2, 3)]
            # [(1,)]
            # [(1, 2)]
            # [(1, 2, 3)]
            # [(2,)]
            # [(2, 3)]
            # [(3,)]
            #

My questions are

  • (a) How can I retrieve each element in each combinations, let say, in [(1, 2, 3)], how can I retrieve value at offset 0 (i.e. 1) ?

  • (b) How can I store return value from itertools.combinations into a list array in "res" (eg, res[0] = [(0,)] , res[1] = [(0, 1)] ?

  • (c) Let say I want to use map(), How can I make the value eg [(0, 1)] as key, and assign a random value to this key?

解决方案

  • a) Use indexes:

    >>> [(1, 2, 3)][0][0]
    1
    

  • b) I don't 100% understand this question, but instead of using a list comprehension as you have done, you can use list(itertools.combinations(...))

  • c) I think you are misunderstanding what map() does. map(str, [1, 2, 3]) is equivalent to:

    [str(i) for i in [1, 2, 3]]
    

If you want to give [(0, 1)] a value, you can use a dictionary, but you have to use (0, 1) instead of [(0, 1)] because you would otherwise get TypeError: unhashable type: 'list'. If you want a "random value", I guess you can use the random module:

import random
{(0, 1) : random.randint(1,10)} # This is just an example, of course


To store all the outputs in one list, you can use a massive list comprehension:

>>> [list(itertools.combinations(range(x, i), i-x)) for x in range(0, items+1) for i in range(0, items+1) if (i-x) > 0]
[[(0,)], [(0, 1)], [(0, 1, 2)], [(0, 1, 2, 3)], [(1,)], [(1, 2)], [(1, 2, 3)], [(2,)], [(2, 3)], [(3,)]]

这篇关于在 Python 中提取列表元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆