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

时间:2020-08-03 23:13:06

标签: python list add

这可能不是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,并将其放入列表中:

An illustrative picture

现在很简单:

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']

2 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

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

然后将它们转换为数字,

最后对结果执行逐元素求和(可以使用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]