如何将一个列表中的元素添加到另一个列表?

Air Stalk3r:

SO社区可能并没有很好地接受这个问题。我是12岁的Python初学者,我正在从事这个项目,以收集多个网站和GitHub帖子上的Corona统计信息。然后,我将其变成图表。已创建的一些列表将由一个国家(但在不同省份)生成。所以我需要做的是将列表中的每个元素添加到列表中的另一个元素,这样我就只能拥有一个国家的列表。我已经将两个或两个以上的列表附加到一个称为完整的列表中,并对其进行了格式化,看起来有点像这样:

['China', '  8', '  8', '  8', '  8', '  8', '  8', '  8', 
 'China', '  2', '  2', '  2', '  2', '  2', '  2', '  2', 
 'China', '  6', '  6', '  6', '  6', '  6', '  6', '  6', 
 'China', '  22', '  22', '  22', '  22', '  22', '  22', '  22']

我的目标


因此,我需要例如将8加2,并将其放入列表中:

插图图片

现在很简单:

list1 = full[1]+full[3]

但是,如果我需要在一个列表中相互添加多个元素,以及每次都不同的完整列表(我的列表)的数量(会发生这种情况,因为中国可能有8个省,所以我创建了8个列表,而印度有50个省因此我创建了50个列表)。

结果


所以这就是我想要得到但无法弄清楚的东西。
['China', '8','7','2', 'China','2','34','18']
['India', '8','7','2', 'India','2','34','8','India','2','231','44']

China = ['10','41','20']
India = ['12','271','55']

这是我的代码:

file = open('similar.txt','r')
L = []
full = []
final = []
countryName = ''
numfinal = 0
for row in file:
    count = 1
    row = row[:-2]
    row = row[1:]
    L.append(row)
    for i in L:
        L = i.split(',')
    L = [i.replace('\n','')for i in L]
    L = [i.replace('\'','') for i in L]  

    if L[0] == countryName:
        for i in L:
            full.append(i)
        print(full)
    else:
        countryName = L[0]
        full.clear()

(还有更多文件,但这些文件对我的问题无关紧要。它还会从包含省份多的所有国家/地区的文件中读取文件。由于粘贴太多,因此下面有卷曲)curl-O 'https://raw.githubusercontent.com/BuddyBob/Py_Programs/master/Hackathon/Deaths/like.txt'或仅使用链接。任何帮助将非常感激

这是来自like.txt的几行代码:

['Australia', ' 0', ' 0', ' 0', ' 0', ' 0', ' 0', ' 0']
['Australia', ' 4', ' 4', ' 4', ' 4', ' 4', ' 4', ' 4']
['Australia', ' 83', ' 92', ' 105', ' 112', ' 116', ' 123', ' 123']
['Canada', ' 7', ' 8', ' 8', ' 8', ' 8', ' 8', ' 8']
['Canada', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3']
['Canada', ' 2807', ' 2812', ' 2813', ' 2816', ' 2819', ' 2821', ' 2822']
['Canada', ' 5667', ' 5670', ' 5670', ' 5673', ' 5674', ' 5678', ' 5681']
['China', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1']
['China', ' 8', ' 8', ' 8', ' 8', ' 8', ' 8', ' 8']
FMc:

这是对数据进行分组和求和的一种方式的说明。肯定有更短,更奇妙的方法来执行此类操作,但是此示例将步骤分解了。

import json

# Your lists.
raw_lists = [
    ['Australia', ' 0', ' 0', ' 0', ' 0', ' 0', ' 0', ' 0'],
    ['Australia', ' 4', ' 4', ' 4', ' 4', ' 4', ' 4', ' 4'],
    ['Australia', ' 83', ' 92', ' 105', ' 112', ' 116', ' 123', ' 123'],
    ['Canada', ' 7', ' 8', ' 8', ' 8', ' 8', ' 8', ' 8'],
    ['Canada', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3'],
    ['Canada', ' 2807', ' 2812', ' 2813', ' 2816', ' 2819', ' 2821', ' 2822'],
    ['Canada', ' 5667', ' 5670', ' 5670', ' 5673', ' 5674', ' 5678', ' 5681'],
    ['China', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1'],
    ['China', ' 8', ' 8', ' 8', ' 8', ' 8', ' 8', ' 8'],
    ['China', ' 2', ' 2', ' 2', ' 2', ' 2', ' 2', ' 2'],
    ['China', ' 6', ' 6', ' 6', ' 6', ' 6', ' 6', ' 6'],
    ['China', ' 22', ' 22', ' 22', ' 22', ' 22', ' 22', ' 22'],
    ['China', ' 4512', ' 4512', ' 4512', ' 4512', ' 4512', ' 4512', ' 4512'],
    ['China', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1'],
    ['China', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1'],
    ['China', ' 2', ' 2', ' 2', ' 2', ' 2', ' 2', ' 2'],
    ['China', ' 0', ' 0', ' 0', ' 0', ' 0', ' 0', ' 0'],
    ['China', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3'],
    ['China', ' 7', ' 7', ' 7', ' 7', ' 7', ' 7', ' 7'],
    ['China', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3'],
    ['China', ' 0', ' 0', ' 0', ' 0', ' 0', ' 0', ' 0'],
    ['China', ' 2', ' 2', ' 2', ' 2', ' 2', ' 2', ' 2'],
    ['Denmark', ' 613', ' 613', ' 614', ' 615', ' 615', ' 615', ' 615'],
    ['France', ' 38', ' 38', ' 39', ' 39', ' 39', ' 39', ' 39'],
    ['France', ' 4', ' 4', ' 4', ' 4', ' 4', ' 4', ' 4'],
    ['France', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3', ' 3'],
    ['France', ' 30096', ' 30109', ' 30108', ' 30123', ' 30150', ' 30150', ' 30150'],
    ['Netherlands', ' 15', ' 15', ' 15', ' 15', ' 15', ' 15', ' 16'],
    ['United Kingdom', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1', ' 1'],
]

# Group the lists based on country. This will give us a dict-of-lists-of-lists.
# Like this: grouped_lists{COUNTRY} = [ [...], [...], etc ]
grouped_lists = {}
for xs in raw_lists:
    # Grab country name and the rest of the values.
    c = xs[0]
    vals = xs[1:]
    assert len(vals) == 7
    # Convert the values to integers using a list comprehension.
    nums = [int(v.strip()) for v in vals]
    # Add the list of numbers to the country's list-of-lists.
    grouped_lists.setdefault(c, []).append(nums)

# Sum up the separate lists for each country.
country_totals = {}
for c, xss in grouped_lists.items():
    # Initialize the key for the country.
    country_totals[c] = []
    # Since xss is a list-of-lists for the current country, we can use zip() to
    # weave together the values based on their positions (or indexes). For
    # example, on the first iteration tup will be a tuple holding the first
    # elements from the separate lists. On second iteration, second elements. Etc.
    for tup in zip(*xss):
        country_totals[c].append(sum(tup))

# Take a look.
print(json.dumps(country_totals))

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何将一个列表中的元素添加到另一个列表中?

如何将元素从另一个列表添加到列表?

如何将列表中的列表添加到另一个列表

如何将一个列表添加到另一个列表

如何将列表添加到C#中的另一个列表?

Java-如何将一个对象添加到另一个对象的列表中

如何将列表添加到另一个列表

根据R中的另一个列表将元素添加到列表

如何将一个列表元素分别添加到 2 个列表中?

将一个列表添加到Java中的另一个列表?

如何将一个列表的项目一个接一个地添加到另一个列表?

python 3如何将项目添加到另一个列表中的列表

如何将字符串列表添加到特定位置的另一个列表中?

如何将列表中的匹配元素附加到另一个列表?

如何在Python3中将一个列表中的元素添加到另一个列表中?

R:如何将一个列表中的向量并行添加到另一个列表中(带有可重现的示例)

如何将一个列表框中的项目添加到另一个列表框中?

如何将某些值从一个列表添加到.NET中的另一个列表

如何仅将另一个列表中的值添加到字典中

如何将另一个Ubuntu版本添加到Launchpad错误的“影响”列表中?

如何将项目添加到 vb.net 中的另一个列表框

如何将表列数据添加到mysql中另一个类似的列表

如何将数据类字段的所有值添加到另一个列表中

如何将前四位数字添加到另一个列表中

Groovy:将列表的某些元素添加到另一个列表

将临时列表添加到另一个列表中,丢失列表数据

将记录从一个列表添加到另一个列表

输入一个列表中的元素以添加到另一个列表中

如何将一个对象添加到另一个对象的列表中并将其保存在 JSF 中?