标签: nested-lists

在表中设置计数器

我有两个问题,

Q1.代码如下:

orgtable = Table[{i, node2 = i + 1, node3 = node2 + 6, node4 = node3 - 1, 
                     node5 = i + 18, node6 = node5 + 1, node7 = node6 + 6, 
                     node8 = node7 - 1}, {i, 1, 36}
           ];
modtable = Drop[orgtable, {6, 36, 6}];
finaltable = With[{n = 5, m = 10},Flatten[Partition[modtable, n, n + m, 1, {}], 1]]
Run Code Online (Sandbox Code Playgroud)

第一段代码给了我一个原始表,第二段给了我一个修改过的表,第三段给出了最终表.最终表的输出如下所示:

{{1, 2, 8, 7, 19, 20, 26, 25},    {2, 3, 9, 8, 20, 21, …
Run Code Online (Sandbox Code Playgroud)

counter wolfram-mathematica list nested-lists

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

在django的列表中创建一种特殊的合并列表

我确信这很容易做到,但我不知道该怎么做.

我有三个列表:list1=[a1, a2, a3...],list2=[b1, b2, b3...],和list3=[c1, c2, c3...]

我想传递一个列表,这是这三个项目的合并:

finalList = [[a1, b1, c1,], [a2, b2, c2], [a3, b3, c3]...]

我该怎么做呢?

python nested-lists

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

如何从python中的两个列表生成嵌套列表

我是python的新手,我有两个列表:

l1 = ['a','b','c','d']
l2 = ['new']
Run Code Online (Sandbox Code Playgroud)

我想得到这样的新名单

l3 = [('a','new'),('b','new'),('c','new'),('d','new')]
Run Code Online (Sandbox Code Playgroud)

将两个列表组合在一起的最佳方法是什么?

python tuples list nested-lists

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

如何比较列表列表的元素(Python)

identical = 0
while x < len(BigList):
    while y < len(BigList):
        if BigList[x][0] == BigList[y][0] and y != x:
            identical += 1
        y += 1
    x += 1
Run Code Online (Sandbox Code Playgroud)

如何正确查看0th大列表中每个列表的元素是否等于Big List中另一个列表中的另一个第0个元素?

即,我需要[[1,2],[2,3],[1,4],[2,5]]制作,identical=2因为2个列表的第0个元素等于另一个列表的0th元素

TIA

python list nested-lists

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

C# - 列出内部列表

我需要一些帮助.

我正在尝试将信息存储在列表中.

例如,用户输入数字,年龄,地址和头发颜色......

我想在列表中创建列表来存储信息,如下所示:

[0] Number

----[0] Age

----------[0] Address

----------[1] Adrress

--------------[0] Hair Color
Run Code Online (Sandbox Code Playgroud)

如果有更好的方法,请帮助我!
列表中的列表我会很困惑,但如果它是更好的解决方案,我没有问题.

c# list nested-lists

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

根据每个子列表中的第三项删除列表中的重复项

我有一个列表列表,看起来像:

c = [['470', '4189.0', 'asdfgw', 'fds'],
     ['470', '4189.0', 'qwer', 'fds'],
     ['470', '4189.0', 'qwer', 'dsfs fdv'] 
      ...]
Run Code Online (Sandbox Code Playgroud)

c有大约30,000个内部清单。我想做的是根据每个内部列表的第4个项目消除重复项。因此,上面的列表列表如下所示:

c = [['470', '4189.0', 'asdfgw', 'fds'],['470', '4189.0', 'qwer', 'dsfs fdv'] ...]
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止的内容:

d = [] #list that will contain condensed c
d.append(c[0]) #append first element, so I can compare lists
for bact in c: #c is my list of lists with 30,000 interior list
    for items in d:
        if bact[3] != items[3]:
            d.append(bact)  
Run Code Online (Sandbox Code Playgroud)

我认为这应该可行,但它会不断运行。我让它运行30分钟,然后将其杀死。我认为程序不会花那么长时间,所以我猜我的逻辑有问题。

我觉得创建一个全新的列表列表非常愚蠢。任何帮助将不胜感激,请随时随地学习。如果不正确,请更正我的词汇。

python duplicate-removal nested-lists

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

如何将.txt文件转换为列表python?

在.txt文件中:

[['Courtney fan', 'https://www.aaa.com', 'he is a good guy'], ['Courtney fan', 'https://www.bbb.com', 'Dave Butner', 'https://www.ccc.com', 'Austin']]
Run Code Online (Sandbox Code Playgroud)

我尝试了此方法,但无法正确拆分:

with open("/Users/jj/Desktop/Courtney_fan.txt","r") as f:
       sd = f.read().split() 
Run Code Online (Sandbox Code Playgroud)

如何将其写入python的嵌套列表中?

python list nested-lists

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

使用嵌套的元组列表创建Python defaultdict

场景是我有一个2-D列表.内部列表的每个项目都是元组(键,值对).密钥可能会在列表中重复出现.我想动态创建一个默认字典,最后,字典存储密钥,以及二维列表中该密钥的所有值的累积和.

把代码放到:

listOfItems = [[('a', 1), ('b', 3)], [('a', 6)], [('c', 0), ('d', 5), ('b', 2)]]
finalDict = defaultdict(int)
for eachItem in listOfItems:
    for key, val in eachItem:
        finalDict[key] += val
print(finalDict)
Run Code Online (Sandbox Code Playgroud)

这给了我想要的东西:defaultdict(<class 'int'>, {'a': 7, 'b': 5, 'c': 0, 'd': 5})但我正在寻找一种更加"Pythonic"的方式来使用理解.所以我尝试了以下内容:

finalDict = defaultdict(int)
finalDict = {key : finalDict[key]+val for eachItem in listOfItems for key, val in eachItem}
print(finalDict)
Run Code Online (Sandbox Code Playgroud)

但输出结果是:{'a': 6, 'b': 2, 'c': 0, 'd': 5}我做错了什么?或者是在使用理解时,不会动态创建和修改词典?

python nested-lists python-3.x defaultdict dictionary-comprehension

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

从Python中的列表创建元组

我有这个数组:

lst = ['A', 'B', 'C']
Run Code Online (Sandbox Code Playgroud)

如何将字符串“ D”附加到每个元素并将每个集合转换为元组:

lst2=  [('A', 'D'),
          ('B', 'D'),
          ('C', 'D')]
Run Code Online (Sandbox Code Playgroud)

python nested-lists python-3.x

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

根据另一个嵌套列表对嵌套列表进行排序

我有两个嵌套列表,x1和x2。我需要根据x1 [0] [0]的顺序求助于x2。

我试过使用sort()和一些lambda x,但不完全确定如何处理此问题。

x1 = [["d", 0.4, 1],
     ["c", 0.5, 2],
     ["b", 0.3, 3],
     ["a", 0.5, 4]]

x2 = [["c", 4, 1],
     ["d", 2, 2],
     ["a", 6, 3],
     ["b", 6, 4]]
Run Code Online (Sandbox Code Playgroud)

我希望为重新排序的x2得到以下输出

x2 = [["d", 2, 2],
     ["c", 4, 1],
     ["b", 6, 4],
     ["a", 6, 3]]
Run Code Online (Sandbox Code Playgroud)

我不能依靠基于字母顺序的重新排序,因为我使用的数据集与此不同,并且我可能会遇到其他问题。理想情况下,我需要编写一些内容,使其仅将每个嵌套列表与x2在x1中出现的顺序匹配,仅基于每个嵌套列表的第一个元素(始终为字符串)。

我只需要使用pythons标准库即可。

python sorting nested-lists string-matching

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