[英]Python - list + tuples length of elements
I'm having some issues trying to figure this out (as i'm a pure beginner to python).我在试图解决这个问题时遇到了一些问题(因为我是 python 的纯初学者)。
I have a list of names: names_2 = ["Lars", "Per", "Henrik"]
我有一个名字列表: names_2 = ["Lars", "Per", "Henrik"]
Which I need to convert into a tuple who hold each elements length + the element it self.我需要将其转换为一个元组,该元组包含每个元素的长度 + 它自身的元素。
I tried this:我试过这个:
namesTuple = tuple(names_2 + [len(name) for name in names_2])
Output of this is: ('Lars', 'Per', 'Henrik', 4, 3, 6)
Output 这是:( ('Lars', 'Per', 'Henrik', 4, 3, 6)
The output im looking for is ('Lars', 4, 'Per', 3, 'Henrik', 6)
我正在寻找的 output 是('Lars', 4, 'Per', 3, 'Henrik', 6)
Anyone who can help?有谁能帮忙吗?
You can use a nested generator expression in the tuple
constructor, for instance:您可以在tuple
构造函数中使用嵌套的生成器表达式,例如:
names_tuple = tuple(x for name in names_2 for x in (name, len(name)))
# ('Lars', 4, 'Per', 3, 'Henrik', 6)
If you were to build it in a looping approach, it makes sense to build a list first (tuples are immutable):如果您要以循环方式构建它,则首先构建一个列表是有意义的(元组是不可变的):
names = []
for name in names_2:
# extend (both at once)
names.extend((name, len(name)))
# OR append one by one (no spurious intermediate tuple)
# names.append(name)
# names.append(len(name))
names_tuple = tuple(names)
names_2 = ["Lars", "Per", "Henrik"]
names = []
for name in names_2:
names.append(name)
names.append(len(name))
names = tuple(names)
Iterate over the names, append the name itself and its length to a list, and convert the list to tuple.遍历名称 append 名称本身及其长度为列表,并将列表转换为元组。
Or as a one-liner (but you'll end up with a tuple of tuples):或者作为单行(但你最终会得到一个元组的元组):
names_2 = ["Lars", "Per", "Henrik"]
names = tuple((name, len(name)) for name in names_2)
Zip the list of names with the list of lengths, then flatten the resulting list and convert that to a tuple. Zip 名称列表和长度列表,然后展平结果列表并将其转换为元组。
from itertools import chain
namesTuple = tuple(chain.from_iterable(zip(names_2, map(len, names_2))))
If you prefer something a little less "functional", you can use a generator expression.如果您更喜欢“功能性”较少的东西,则可以使用生成器表达式。
namesTuple = tuple(chain.from_iterable((x, len(x)) for x in names_2))
or (repeating @schwobaseggl's answer )或(重复@schwobaseggl 的回答)
namesTuple = tuple(value for name in names_2 for value in (name, len(name)))
First create a tuple of tuples: ((name_1,lenght_1), (name_2,lenght_2),...) The zip function is existing for that.首先创建一个元组的元组: ((name_1,lenght_1), (name_2,lenght_2),...) zip function 是存在的。
Secondly, you have to flatten this tuple of tuples.其次,你必须展平这个元组的元组。
[In]
names = ["Lars", "Per", "Henrik"]
[In]
zip_tupled = tuple(zip(names, [len(x) for x in names]))
[Out]
zip_tupled = (('Lars', 4), ('Per', 3), ('Henrik', 6))
[In]
final = tuple(item for subtuple in zip_tupled for item in subtuple)
[Out]
final = ('Lars', 4, 'Per', 3, 'Henrik', 6)
This solution is quite close to the solution of schwobaseggl...But less direct/straight.这个解决方案非常接近 schwobaseggl 的解决方案......但不那么直接/直接。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.