使用 python 中的函数将字符串列表转换为元组
Coverting a list of strings into tuples using a function in python
我是 Python 的新手,刚接触函数。我正在研究一个示例,不太了解如何编写所需的函数。
函数的输入是以下格式的字符串列表:
lines = ['1: 362.5815 162.5823\n',
'2: 154.1328 354.1330\n',
'3: 168.9325 368.9331\n',.. ]
我必须创建一个函数,将列表中的每个项目转换为元组并将输出存储在新列表中。
要将列表中的单个项目转换为元组,我使用以下代码:
f1 = lines[0].split(" ")
f1tuple1 = tuple(f1)
f1tuple2 = (f1tuple[0], [float(f1tuple[1]), float(f1tuple[2])])
如何对列表中的所有项目执行相同的操作?
在这件事上,我真的很感激帮助。
通过 'for' 循环,您可以 运行 遍历列表中的所有项目:
all_tuples = [] #<-- create a list to put all you tuples into
for value in lines: #<-- for loop, running through all values in you lines list
f1 = value.split(" ") #<-- your code1
f1tuple = tuple(f1) #<-- your code2
f1tuple2 = (f1tuple[0], [float(f1tuple[1]), float(f1tuple[2])]) #<-- your code3
all_tuples.append(f1tuple2) #<-- Adding the tuple to the list
all_tuples
[('1:', [362.5815, 162.5823]),
('2:', [154.1328, 354.133]),
('3:', [168.9325, 368.9331])]
作为函数:
def get_all_tuples(lines): #<-- create a function
all_tuples = []
for value in lines:
f1 = value.split(" ")
f1tuple = tuple(f1)
f1tuple2 = (f1tuple[0], [float(f1tuple[1]), float(f1tuple[2])])
all_tuples.append(f1tuple2)
return all_tuples #<-- return your all_tuples list
编辑:正如评论中正确指出的那样,无需使用地图。
我使用函数和正确的类型进行了更新(第一个元组元素为 int):
lines = ['1: 362.5815 162.5823\n', '2: 154.1328 354.1330\n', '3: 168.9325 368.9331\n']
def create_tuples(lines):
return [(int((y := x.split(" "))[0].strip(':')),
[float(y[1]), float(y[2].strip('\n'))]
) for x in lines]
print(create_tuples(lines))
输出:
[(1, [362.5815, 162.5823]), (2, [154.1328, 354.133]), (3, [168.9325, 368.9331])]
注意:列表理解中的分配仅适用于 python >= 3.8
旧答案:
您可以使用 map
:
fituples = list(map(lambda x: tuple(x.strip("\n").split(" ")), lines))
输出:
[('1:', '362.5815', '162.5823'), ('2:', '154.1328', '354.1330'), ('3:', '168.9325', '368.9331')]
def items(lines):
vals = [x.split(" ") for x in lines]
return [(i[0], i[1], i[2]) for i in vals]
使用re:
tuples = []
for item in lines:
if m := re.match(r"(\d+):\s+(\S+)\s+(\S+)", item):
tuples.append([m.group(1), (m.group(2), m.group(3))])
我是 Python 的新手,刚接触函数。我正在研究一个示例,不太了解如何编写所需的函数。
函数的输入是以下格式的字符串列表:
lines = ['1: 362.5815 162.5823\n',
'2: 154.1328 354.1330\n',
'3: 168.9325 368.9331\n',.. ]
我必须创建一个函数,将列表中的每个项目转换为元组并将输出存储在新列表中。
要将列表中的单个项目转换为元组,我使用以下代码:
f1 = lines[0].split(" ")
f1tuple1 = tuple(f1)
f1tuple2 = (f1tuple[0], [float(f1tuple[1]), float(f1tuple[2])])
如何对列表中的所有项目执行相同的操作? 在这件事上,我真的很感激帮助。
通过 'for' 循环,您可以 运行 遍历列表中的所有项目:
all_tuples = [] #<-- create a list to put all you tuples into
for value in lines: #<-- for loop, running through all values in you lines list
f1 = value.split(" ") #<-- your code1
f1tuple = tuple(f1) #<-- your code2
f1tuple2 = (f1tuple[0], [float(f1tuple[1]), float(f1tuple[2])]) #<-- your code3
all_tuples.append(f1tuple2) #<-- Adding the tuple to the list
all_tuples
[('1:', [362.5815, 162.5823]),
('2:', [154.1328, 354.133]),
('3:', [168.9325, 368.9331])]
作为函数:
def get_all_tuples(lines): #<-- create a function
all_tuples = []
for value in lines:
f1 = value.split(" ")
f1tuple = tuple(f1)
f1tuple2 = (f1tuple[0], [float(f1tuple[1]), float(f1tuple[2])])
all_tuples.append(f1tuple2)
return all_tuples #<-- return your all_tuples list
编辑:正如评论中正确指出的那样,无需使用地图。
我使用函数和正确的类型进行了更新(第一个元组元素为 int):
lines = ['1: 362.5815 162.5823\n', '2: 154.1328 354.1330\n', '3: 168.9325 368.9331\n']
def create_tuples(lines):
return [(int((y := x.split(" "))[0].strip(':')),
[float(y[1]), float(y[2].strip('\n'))]
) for x in lines]
print(create_tuples(lines))
输出:
[(1, [362.5815, 162.5823]), (2, [154.1328, 354.133]), (3, [168.9325, 368.9331])]
注意:列表理解中的分配仅适用于 python >= 3.8
旧答案:
您可以使用 map
:
fituples = list(map(lambda x: tuple(x.strip("\n").split(" ")), lines))
输出:
[('1:', '362.5815', '162.5823'), ('2:', '154.1328', '354.1330'), ('3:', '168.9325', '368.9331')]
def items(lines):
vals = [x.split(" ") for x in lines]
return [(i[0], i[1], i[2]) for i in vals]
使用re:
tuples = []
for item in lines:
if m := re.match(r"(\d+):\s+(\S+)\s+(\S+)", item):
tuples.append([m.group(1), (m.group(2), m.group(3))])