从一个字符串和一个字符串列表创建一个元组 [英] Create a tuple from a string and a list of strings

查看:133
本文介绍了从一个字符串和一个字符串列表创建一个元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一个字符串和一个字符串列表组合成一个元组,以便可以将其用作字典键.这将是一个内循环,因此速度很重要.

I need to combine a string along with a list of strings into a tuple so I can use it as a dictionary key. This is going to be in an inner loop so speed is important.

列表很小(通常为1,但有时为2或3个项目).

The list will be small (usually 1, but occasionally 2 or 3 items).

最快的方法是什么?

之前:

my_string == "foo"
my_list == ["bar", "baz", "qux", "etc"]

之后:

my_tuple == ("foo", "bar", "baz", "qux", "etc")

(注意:my_list本身不能更改).

(Note: my_list must not be altered itself).

推荐答案

我不能说性能,但这绝对是我能想到的最简单的方法:

I can't speak for performance, but this is definitely the simplest I can think of:

my_tuple = tuple([my_string] + my_list)

这篇关于从一个字符串和一个字符串列表创建一个元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆