标签: sublist

子列表到字典

所以我有:

a = [["Hello", "Bye"], ["Morning", "Night"], ["Cat", "Dog"]]
Run Code Online (Sandbox Code Playgroud)

我想把它转换成字典.

我试过用:

i = iter(a)  
b = dict(zip(a[0::2], a[1::2]))
Run Code Online (Sandbox Code Playgroud)

但它给了我一个错误: TypeError: unhashable type: 'list'

python dictionary list sublist

3
推荐指数
1
解决办法
1164
查看次数

如何找到两个列表之间的公用子列表?

如果子列表也存在于另一个列表中,您如何查找或仅保留列表的子列表?

lsta = [['a','b','c'],['c','d','e'],['e','f','g']]
lstb = [['a','b','c'],['d','d','e'],['e','f','g']]
Run Code Online (Sandbox Code Playgroud)

我想做类似set(lsta)和set(lstb)的事情

Desired_List = [['a','b','c'],['e','f','g']]
Run Code Online (Sandbox Code Playgroud)

我之所以想执行set之类的原因是因为它的速度,因为我在非常重要的大型列表中执行此操作。

另外,稍微不相关,如果我想从lsta中减去lstb以得到

Desired_List2 = [['d','d','e']]
Run Code Online (Sandbox Code Playgroud)

python list set sublist

3
推荐指数
1
解决办法
663
查看次数

python-搜索字典子列表; 将字典键转换为值

说我有以下字典(我正在使用的字典更多,更大):

dict1={1:["item", "word", "thing"], 2:["word", "item"], 3:["thing", "item", "item"]}
Run Code Online (Sandbox Code Playgroud)

并将字典中使用的每个单词存储在列表中:

all_words=["item", "word", "thing"]
Run Code Online (Sandbox Code Playgroud)

我想通过字典子列表运行列表中的每个单词,并返回找到它们的所有子列表的键,将它们存储在元组中.所以我想得到:

dict2={"item":(1, 2, 3), "word":(1, 2), "thing":(1, 3)}
Run Code Online (Sandbox Code Playgroud)

继承人我所拥有的:

dict2={}    
for word in all_words:
    for key, sublist in dict2.items():
        for word in sublist:
            if word not in sublist:
                dict2[word]=dict2[word]+key
            else:
                dict2[word]=key
Run Code Online (Sandbox Code Playgroud)

python search dictionary sublist

3
推荐指数
1
解决办法
931
查看次数

程序的断言测试,用于检查列表是否是另一个列表中的子列表

我写了一个小程序,应检查给定列表是否是来自另一个列表的子列表,并返回TrueFalse:

def is_sublist_of(sublist, given):
    """ Returns whether the sublist is part of the given combination.
    The order of the sublist must also correspond to the order of the
    corresponding part in the given combination."""

    return sublist in [given[i:i+len(sublist)] for i in range(0,len(given)-len(sublist))]
Run Code Online (Sandbox Code Playgroud)

此代码是我必须执行的赋值的一部分,但给定的断言之一是:

simple_list = [1, 2, 3, 4]
for element in simple_list:
    assert is_sublist_of([element], simple_list)
assert not is_sublist_of([5], simple_list)
Run Code Online (Sandbox Code Playgroud)

我的程序未通过此测试.这是否意味着我的程序在某些特殊情况下不起作用?感谢您对此进行调查.

python assert list sublist

3
推荐指数
1
解决办法
59
查看次数

Lisp程序,用于创建每个增量的子列表

我目前正在开发一个程序,该程序通过列表递增,并在每次递增时在列表中创建子列表.例如,如果我有一个列表

(1 2 3 4 5 6)
Run Code Online (Sandbox Code Playgroud)

.然后我想通过它增加并创建

(1 (2 (3 (4 (5 (6))))))
Run Code Online (Sandbox Code Playgroud)

.到目前为止,我有:

(defun INCREMENT (L) (unless (endp L) (cons (list (first L)) (INC (rest L)))))
Run Code Online (Sandbox Code Playgroud)

但这一切都将归来

((1) (2) (3) (4))
Run Code Online (Sandbox Code Playgroud)

我知道它只是递增列表的使用次数以及遍历列表的元素,但我仍然在学习Lisp的语法.有人可以帮我解决这个问题吗?

lisp syntax list common-lisp sublist

3
推荐指数
1
解决办法
52
查看次数

如何将一个值附加到列表中的每个元组?

如何将1个值附加到列表中的每个元组?

 tuple_list = [('a','b'),('c','d'),('e','f')]
 value = 111


 Desired_List = [(111,'a','b'),(111,'c','d'),(111,'e','f')]
Run Code Online (Sandbox Code Playgroud)

我尝试过以下方法:

   for x in tuple_list:
        x.append(111)

   for x in tuple_list:
        x + '111'
Run Code Online (Sandbox Code Playgroud)

我更喜欢子列表而不是元组,所以无论如何也要将元组更改为子列表?

注意:111实际上无论是第一个索引还是元组的最后一个索引都无关紧要.

python tuples list append sublist

2
推荐指数
1
解决办法
3872
查看次数

python:按索引将列表拆分为n个子列表

我希望这个问题不是重复的;我找到了类似的,但不完全是我需要的。

我想要一种将列表拆分为n个子列表的有效方法,其中每个索引转到不同的列表,直到我们到达第n 个索引,然后接下来的n 个索引以相同的顺序转到我们已有的列表,依此类推。 .

例如,给定以下列表:

l = [1,1,1,2,2,2,3,3,3]
n = 3
Run Code Online (Sandbox Code Playgroud)

在这种情况下,我需要将列表拆分为具有所需输出的 ​​3 个列表:

[[1,2,3],[1,2,3],[1,2,3]]
Run Code Online (Sandbox Code Playgroud)

我可以创建n 个for 循环,每n步跳过一次,但我确信有更好的方法。

python split list sublist

2
推荐指数
1
解决办法
6179
查看次数

获取固定大小的子列表

标题不清楚,但这是我想要做的。

我有一个基因组链:

corpus_2 = ['TCAATCAC', 'GGGGGGGGGGG', 'AAAA']
Run Code Online (Sandbox Code Playgroud)

我想提取固定大小的所有子列表。假设我想要大小为 4 的子列表。

我寻找的结果示例: ['TCAA', 'CAAT', 'AATC', 'ATCA', 'TCAC'], ['GGGG', 'GGGG', 'GGGG', 'GGGG', 'GGGG', 'GGGG', 'GGGG', 'GGGG'], ['AAAA']]

我们采用索引 0 到索引 3 的子列表,然后添加一个新字符串等...

这是我的代码:

ngram_size=4
corpus=['TCAA', 'CAAT', 'AATC', 'ATCA', 'TCAC'], ['GGGG', 'GGGG', 'GGGG', 'GGGG', 'GGGG', 'GGGG', 'GGGG', 'GGGG'], ['AAAA']]
decoliste=[] #list output
        listemp=[] # I add one list by one list, each of these list corresponds to a list in input list.
        for element in self.corpus:
#             print(element)
            decoliste.append(listemp)
            listemp=[]

            for i in …
Run Code Online (Sandbox Code Playgroud)

python list sublist python-3.x

2
推荐指数
1
解决办法
47
查看次数

List<Integer> arr 和 var arr 之间的区别

下面的代码工作正常。

List<Integer> arr = new ArrayList<Integer>();
System.out.println(arr.getClass().getSimpleName()); // output: ArrayList
arr.add(0);
arr.add(1);
arr = arr.subList(0,1);
Run Code Online (Sandbox Code Playgroud)

但是如果你将第一行更改为

var arr = new ArrayList<Integer>();
Run Code Online (Sandbox Code Playgroud)

会出现错误:

java: 不兼容的类型: java.util.List<java.lang.Integer> 无法转换为 java.util.ArrayList<java.lang.Integer>

然而,即使arr是用第二种方式定义的,它的类型仍然是ArrayList,那么有什么区别呢?

java containers sublist

2
推荐指数
1
解决办法
170
查看次数

当 a, b 是不同长度的子数组时,为什么 a, b = b, a 不能按应有的方式工作?

作为Python的新手,我将Python理解为一个简单的交换,但是当和是两个具有不同长度的子数组a, b = b, a时,为什么它的行为不符合我的预期呢?ab

例如:

nums = [1, 2, 3, 4, 5, 6, 7]

nums[0:4], nums[4:] = nums[4:], nums[0:4]

print(nums)  #[5, 6, 7, 5, 1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)

为什么7后面有重复的5?为什么不是 [5, 6, 7, 1, 2, 3, 4] 因为我只是分成了nums2 个子数组并交换了它们的顺序?

谢谢!

python swap list sublist

2
推荐指数
1
解决办法
65
查看次数

标签 统计

sublist ×10

list ×8

python ×8

dictionary ×2

append ×1

assert ×1

common-lisp ×1

containers ×1

java ×1

lisp ×1

python-3.x ×1

search ×1

set ×1

split ×1

swap ×1

syntax ×1

tuples ×1