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

How to add elements in one list to another?

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

['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/similar.txt' 或者只使用 link。任何帮助将不胜感激

这里有几行来自 similar.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']

您可以使用 itertools.groupby 来获取分组数字,对于您的密钥,只需确定字符串是否为数字(我使用 str.isdigit)。

然后将它们转换成数字,

最后对结果执行 element-wise 求和(可以使用 mapoperator.add 完成)。

这一切都可以在一行中完成。

from itertools import groupby
from operator import add

data = ['China', '8','7','2', 'China','2','34','18']

china = list(map(add, *(tuple(map(int, g)) for k, g in groupby(data, str.isdigit) if k)))

结果:

[10, 41, 20]

下面是对数据进行分组和求和的一种方法的说明。肯定有更短、更奇特的方法来做这类事情,但这个例子分解了这些步骤。

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))