标签: nested-lists

Javascript:从点击目标的同级和子级中删除类

我正在尝试构建类似于 OS X Finder 的东西。从顶级列表中选择一个元素,并向其添加类以设置样式并显示子列表,然后选择子列表的一个元素。我有这个工作。问题是当单击同级项时,从顶级项目中删除“活动”类。此外,当单击顶级项目时,从任何子列表项目中删除“活动”。

本质上:如何选择单击事件目标的所有同级,以及如何选择单击事件目标的所有子级。

我知道这对于 jquery 来说很容易,但我无法在这个项目上使用 jquery。

这是我到目前为止所拥有的:

var allThings = document.querySelectorAll('.thing'); //all tabs on page

function clickHandler(e) {
	e.preventDefault();
	//var siblings = getSiblings(e.target);
  //console.log("siblings = " + siblings);
  //for (var i = 0; i < siblings.length; i++) {
    //siblings[i].classList.remove("active");
  //}
  //e.target.nextSibling.classList.remove("active");
  //e.target.previousSibling.classList.remove("active");
	e.target.classList.toggle("active");
};

function getSiblings(el, filter) {
  var siblings = [];
  el = el.parentNode.firstChild;
  do { if (!filter || filter(el)) siblings.push(el); } 						while (el = el.nextSibling);
  return siblings;
}

for (var i = 0; i …
Run Code Online (Sandbox Code Playgroud)

javascript nested-lists

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

遍历嵌套列表并计算元素的平均值

我正在使用 Riot 的 API 开发一个应用程序,用于分析玩家的英雄联盟比赛历史数据。


我有一个包含商品名称购买时间(以秒为单位)的列表

item_list =
[['Boots of Speed', 50], 
['Health Potion', 60], 
['Health Potion', 80],
['Dorans Blade', 120],  
['Dorans Ring', 180], 
['Dorans Blade', 200], 
['Dorans Ring', 210]]
Run Code Online (Sandbox Code Playgroud)

我正在尝试将其转换为包含商品名称及其平均购买时间的唯一商品列表。

对于此示例,我希望将列表转换为:

['Boots of Speed', 50]
['Health Potion', 70]
['Dorans Blade', 160]
['Dorans Ring', 195]
Run Code Online (Sandbox Code Playgroud)

我尝试的解决方案是创建一个空字典,迭代列表,将字典键设置为项目名称,将平均时间设置为键值。

dict = {}
for item in item_list:
    item_name = item[0]
    time_of_purchase = item[1]
    dict[item_name] = (dict[item_name] + time_of_purchase) / 2 # Would cast this as …
Run Code Online (Sandbox Code Playgroud)

python iteration nested-lists

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

如何替换Python中嵌套列表中的所有值(通过函数)?

有很多建议,但我无法坚持下去。我下面有最接近的解决方案。数据是这样的:

my_list = ([1, [2, 3], 4, [5, [6 , [], [8, 9]], 10]], 8)
Run Code Online (Sandbox Code Playgroud)

和一个函数

def convert(G, l, d):
    z = []
    if isinstance(x, list):
        print(x)
        z.append(convert(G, x, d))
    else:
        print(x)
        z.append([v[d] for n,v in G.nodes(data=True) if n == l])
    return z
Run Code Online (Sandbox Code Playgroud)

执行:

print(convert(G, my_list, "name"))
Run Code Online (Sandbox Code Playgroud)

它给出空数组,但 print(x) 按原样获取源。我想我已经很接近了。问题是我不知道如何作为整数而不是列表l传递。if n == l

编辑

Outpit: (['a', ['b', 'c'], 'd', ['e', ['f' , [], ['g', 'h']], 'j']], 'g) 采用相同的格式(嵌套)。自定义函数仅获取每个元素(int)并从 dict(字母)返回其属性。

另一种尝试:

my_list = ([1, [2, 3], 4, [5, …
Run Code Online (Sandbox Code Playgroud)

python nested-lists

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

Python:如何用我可以附加的空值初始化嵌套列表

我正在学习Python并且碰壁了。我正在尝试定义一个二维列表,稍后我可以用它来附加值。这对应于宽度*高度的网格

我尝试使用 [] 初始化空列表,但随后 wid 被忽略。我尝试使用 None 作为占位符,但后来我无法附加

wid = 3
hgt = 3
l1 = [[]*wid ] * hgt
l = [[None]*wid ] * hgt
l[1][1].append("something")
Run Code Online (Sandbox Code Playgroud)

结果

l1: [[], [], []]

l: [[None, None, None], [None, None, None], [None, None, None]]
Run Code Online (Sandbox Code Playgroud)

错误:

append: AttributeError: 'NoneType' object has no attribute 'append'
Run Code Online (Sandbox Code Playgroud)

期望的结果:[[[], [], []], [[], [], []], [[], [], []]]

python nested-lists

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

计算列表列表中字符串的出现次数

我想计算一个字符串在另一个列表中出现的次数,并将其存储在字典列表中,其中每个字典都有一个列表的计数。前任,

list = [['Sam','John','Alex','Sam','Alex'],['Max','Sam','Max']...]
Run Code Online (Sandbox Code Playgroud)

我希望我的字典列表如下:

count_list = [{'Sam':2,'Alex':2,'John':1}, {'Max':2, 'Sam':1}..] 
Run Code Online (Sandbox Code Playgroud)

我正在迭代每个列表以计算每个字符串出现的次数并将每个结果添加到字典中。但我最终每次都会得到不同的结果,而不是正确的值。

count_list = [{'Sam':2,'Alex':2,'John':1}, {'Max':2, 'Sam':1}..] 
Run Code Online (Sandbox Code Playgroud)

任何帮助都会有用。谢谢。

python dictionary nested-lists

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

如何检查列表是否包含 Dart 中的特定值?

如果列表包含该值,我想得到 true。目前,我粘贴在那里的这段代码返回 false。如果值存在,我希望该值应该为真。

   void main() {
  var demo = [
    {123, 1},
    {234, 1}
  ];

  print(demo.contains(123));

}
Run Code Online (Sandbox Code Playgroud)

dictionary list nested-lists dart

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

在python中检查嵌套列表中的所有元素是否为正数或负数

我试图使用列表理解来查找元素均为正数的嵌套列表,但我不确定如何表达条件来检查列表理解的嵌套列表中的所有值。

records = [[-167.57, 4.0, 61.875, -100.425],
 [-1.75, 3.75, 4.0],
 [7612.875, 10100.0, 74.25, 1.75, 61.875],
 [-2333.37, -5404.547500000001, -5178.645833333333, 97.0, 167.57],
 [-96.99999999999997, -5277.999999999999, -4998.5, 74.25, 3.75]]

answer = [i for i in records if (n < 0 for n in i)]
answer
Run Code Online (Sandbox Code Playgroud)

我认为这就是我试图将其转换为代码的语句:“如果 i 中的 n 的所有 n > 0,则返回索引 i”

编辑:输出将返回所有正数的相应行的索引

python nested-lists

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

嵌套列表的元素总和

我有一个名为 list_6 的嵌套列表:

[[-1, 1, -1, 1, -1, 1, 1, 0, -1, 0, -1, -1, 1, 1, 1, 0, 1, -1, -1, -1 , 1, 1, 1, 0, 0, -1, 0, 0, 0, 1, 0, -1, 1, 1, -1, 0, 0, 1, 1, -1, 0, -1, 1, 1, -1, 1, -1, -1, -1, 1, -1],...]]

列表的每个元素包含-1,1中的整数,并且元素长度不等,列表中有20个元素

我想创建一个名为 list_7 的新列表,如下所示:

[[13],[4],[5],...]],以便对嵌套列表中的每个元素求和,并打印结果。我尝试使用 iter.zip_longest:

[sum(i) for i in itertools.zip_longest(*list_6, fillvalue=0)]

但我得到一个错误函数:

“str”对象不可调用

python sum nested-lists strerror elementwise-operations

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

List comprehension combine 2 elements in nested list

Is it possible to use list comprehension to combine 2 elements in a nested list based on the occurrence of a character, eg: if you encounter '+' combine with the next element? I have some code that does this using nested loops, but trying to achieve using list comprehension.

Input: l = [['A-2', 'A-3', 'A-4', '+', '100', 'A-5'],['B-2', 'B-3', 'B-4', '+', '500', 'B-5']]

Output: l = [['A-2', 'A-3', 'A-4', '+100', 'A-5'],['B-2', 'B-3', 'B-4', '+500', 'B-5']]

Code:

for nested in l: …
Run Code Online (Sandbox Code Playgroud)

python loops list-comprehension list nested-lists

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

如何将嵌套列表转换为字典?

我想采用嵌套列表,例如:

list = [[Name, Height, Weight],[Dan, 175, 75],[Mark, 165, 64], [Sam, 183, 83]]
Run Code Online (Sandbox Code Playgroud)

并将其转换为字典,如:

dict = {Name: [Dan,Mark, Sam], Height: [175, 165, 183], Weight: [75, 64, 83]}
Run Code Online (Sandbox Code Playgroud)

不幸的是,我当前的代码并没有真正给我我正在寻找的字典格式。

list = [[Name, Height, Weight],[Dan, 175, 75],[Mark, 165, 64], [Sam, 183, 83]]
Run Code Online (Sandbox Code Playgroud)

有人可以帮助我并找出我哪里出错了吗?

python dictionary nested-lists

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