用 Python 从字符串和列表创建元组
如果需要根据一条字符串和一个列表创建元组,可以使用元组方法。
可以使用列表存储异构值(即任何数据类型的数据,如整数、浮点数、字符串等)。
以下对上述情况进行了演示 −
示例
my_list_1 = ['Hey', 'there', 'How', 'are', 'you']
my_list_2 = 'Jane'
print("The first list is :")
print(my_list_1)
print("The string is :")
print(my_list_2)
my_result = tuple(my_list_1 + [my_list_2])
print("The aggregated tuple is : ")
print(my_result)输出
The first list is :
['Hey', 'there', 'How', 'are', 'you']
The string list is :
Jane
The aggregated tuple is :
('Hey', 'there', 'How', 'are', 'you', 'Jane')说明
- 创建了一个列表并显示在控制台上。
- 定义另一个字符串。
- 使用元组和“[]”聚合列表和字符串。
- 最终输出显示在控制台上。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP