英文:
Multiply list in list string items with list in list integers
问题
我有2个列表:list1
和 list2
。
list1
是一个包含各种字符串的列表,其中每个元素也是一个列表。
list2
也是一个包含不同整数值的列表,同样每个元素也是一个列表。
这两个列表的维度是相同的。
如何将 list1
中的每个字符串与 list2
中的每个整数相乘,以实现类似以下的结果:
list1 = [['ABC','DEF'],['GHI','JKL'],['MNO','PQR']]
list2 = [[2, 2], [3, 3], [4, 4]]
result = [['ABC', 'ABC', 'DEF', 'DEF'], ['GHI', 'GHI', 'GHI', 'JKL', 'JKL', 'JKL'], ...]
非常感谢!
我已经尝试过使用 zip()
函数和 *
运算符,但都没有成功。
英文:
I have 2 lists: list1
and list2
list1
is a list in list and contains various strings
list2
is also a list in list and consists of different integer values
the dimensions of both lists is the same
how do I multiply each string in list1
with each integer in list2
in order to achieve something like that:
list1 = [['ABC','DEF'],['GHI','JKL'],['MNO','PQR']]
list2 = [[2, 2], [3, 3], [4, 4]]
result = [['ABC', 'ABC', 'DEF', 'DEF'], ['GHI', 'GHI', 'GHI', 'JKL', 'JKL', 'JKL'], ...]
Thank you very much
I already tried the zip() function and worked with the * operator but nothing worked
答案1
得分: 1
这是您的代码的翻译:
期望`list2`中的子列表可以具有不同的值。我编写了以下代码:
list1 = [['ABC', 'DEF'], ['GHI', 'JKL'], ['MNO', 'PQR']]
list2 = [[2, 2], [3, 3], [4, 4], [1, 1]]
combined = []
for sublist1, sublist2 in zip(list1, list2):
sublist = []
for elem1, elem2 in zip(sublist1, sublist2):
sublist.extend([elem1] * elem2)
combined.append(sublist)
print(combined)
结果:
[['ABC', 'ABC', 'DEF', 'DEF'], ['GHI', 'GHI', 'GHI', 'JKL', 'JKL', 'JKL'], ['MNO', 'MNO', 'MNO', 'MNO', 'PQR', 'PQR', 'PQR', 'PQR']]
希望这对您有所帮助。
英文:
Expecting that sublists in list2
can have varying values. I coded this:
list1 = [['ABC','DEF'],['GHI','JKL'],['MNO','PQR']]
list2 = [[2, 2], [3, 3], [4, 4], [1, 1]]
combined = []
for sublist1, sublist2 in zip(list1, list2):
sublist = []
for elem1, elem2 in zip(sublist1, sublist2):
sublist.extend([elem1]* elem2)
combined.append(sublist)
print(combined)
Result:
[['ABC', 'ABC', 'DEF', 'DEF'], ['GHI', 'GHI', 'GHI', 'JKL', 'JKL', 'JKL'], ['MNO', 'MNO', 'MNO', 'MNO', 'PQR', 'PQR', 'PQR', 'PQR']]
答案2
得分: 0
这是一个简单的一行代码:
[展开收缩 for l1, l2 in zip(list1, list2)]
英文:
Here's a simple 1-liner:
[展开收缩 for l1,l2 in zip(list1, list2)]
答案3
得分: 0
@Zero的代码可以简化为以下的列表推导式:
list1 = [['ABC','DEF'],['GHI','JKL'],['MNO','PQR']]
list2 = [[2, 2], [3, 3], [4, 4], [1, 1]]
[[i for i, j in zip(a, b) for _ in range(j)] for a, b in zip(list1, list2)]
性能:
In [153]: %timeit [[i for i, j in zip(a, b) for _ in range(j)] for a, b in zip(list1, list2)]
3.86 微秒 ± 234 纳秒每循环一次(7 次,每次 100,000 次循环)
In [154]: def func(list1, list2):
...: combined = []
...: for sublist1, sublist2 in zip(list1, list2):
...: sublist = []
...: for elem1, elem2 in zip(sublist1, sublist2):
...: sublist.extend([elem1]* elem2)
...:
...: combined.append(sublist)
...: return combined
In [155]: %timeit func(list1, list2)
2.76 微秒 ± 135 纳秒每循环一次(7 次,每次 100,000 次循环)
我对自己的方法比较慢感到惊讶。
我想出了另一种方法,但它似乎更慢:
from itertools import chain
[list(chain(*([i]*j for i, j in zip(a, b)))) for a, b in zip(list1, list2)]
In [158]: %timeit [list(chain(*([i]*j for i, j in zip(a, b)))) for a, b in zip(list1, list2)]
4.76 微秒 ± 78.9 纳秒每循环一次(7 次,每次 100,000 次循环)
所以我想出了我能想到的最聪明的方法,但它似乎仍然很慢:
from functools import reduce
from operator import iconcat
def repeat_elements(a, b):
return reduce(iconcat,([i]*j for i, j in zip(a, b)),[])
list(map(repeat_elements, list1, list2))
In [169]: %timeit list(map(repeat_elements, list1, list2))
3.86 微秒 ± 90.1 纳秒每循环一次(7 次,每次 100,000 次循环)
不知何故,我的聪明方法都不如直接的方法性能好。这让我感到谦卑。但我喜欢这个小练习。
英文:
@Zero's code can be simplified to this list comprehension oneliner:
list1 = [['ABC','DEF'],['GHI','JKL'],['MNO','PQR']]
list2 = [[2, 2], [3, 3], [4, 4], [1, 1]]
[[i for i, j in zip(a, b) for _ in range(j)] for a, b in zip(list1, list2)]
Performance:
In [153]: %timeit [[i for i, j in zip(a, b) for _ in range(j)] for a, b in zip(list1, list2)]
3.86 µs ± 234 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
In [154]: def func(list1, list2):
...: combined = []
...: for sublist1, sublist2 in zip(list1, list2):
...: sublist = []
...: for elem1, elem2 in zip(sublist1, sublist2):
...: sublist.extend([elem1]* elem2)
...:
...: combined.append(sublist)
...: return combined
In [155]: %timeit func(list1, list2)
2.76 µs ± 135 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
I am surprised to see that my method is slower.
I came up with another method, but it is even slower somehow:
from itertools import chain
[list(chain(*([i]*j for i, j in zip(a, b)))) for a, b in zip(list1, list2)]
In [158]: %timeit [list(chain(*([i]*j for i, j in zip(a, b)))) for a, b in zip(list1, list2)]
4.76 µs ± 78.9 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
So I came up with the smartest method I can think of, but it is somehow still slow:
from functools import reduce
from operator import iconcat
def repeat_elements(a, b):
return reduce(iconcat,([i]*j for i, j in zip(a, b)),[])
list(map(repeat_elements, list1, list2))
In [169]: %timeit list(map(repeat_elements, list1, list2))
3.86 µs ± 90.1 ns per loop (mean ± std. dev. of 7 runs, 100,000 loops each)
Somehow my smart methods are all less performant than the idiotic straight-forward approach. This is humbling. But I liked the tiny exercise.
答案4
得分: -1
list1 = [['ABC', 'DEF'], ['GHI', 'JKL'], ['MNO', 'PQR']]
list2 = [[2, 2], [3, 3], [4, 4]]
res = []
for l1, l2 in zip(list1, list2):
res.append([])
for t1, t2 in zip(l1, l2):
res[-1].extend([t1] * t2)
print(res)
英文:
list1 = [['ABC','DEF'],['GHI','JKL'],['MNO','PQR']]
list2 = [[2, 2], [3, 3], [4, 4]]
res = []
for l1, l2 in zip(list1, list2):
res.append([])
for t1, t2 in zip(l1, l2):
res[-1].extend([t1]*t2)
print(res)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论