我是Python新手,但我有Perl编码方面的经验。
我想知道是否有一种优雅的方式来加入'列出一个字符串,如Python中的Perl示例。
my $brands = [ qw( google apple intel qualcomm ) ]; #1st List
my $otherBrands = [ qw( nike reebok puma ) ]; #2nd List
say join ':' , ( @{$brands} , @{$otherBrands} );
# print "google:apple:intel:qualcomm:nike:reebok:puma"
我已经搜索了一段时间,但大多数答案都是使用*进行解包,这在我的情况下是不可行的。
答案 0 :(得分:6)
您可以使用+
加入两个列表,join
加入这些列表。
brands = ["google", "apple", "intel", "qualcomm"]
otherBrands = ["nike", "reebok", "puma"]
print ":".join(brands + otherBrands)
如果你在Perl中寻找类似于qw
的语法(创建一个没有引号的字符串文字列表),据我所知,这在Python中是不存在的。
答案 1 :(得分:2)
您可以尝试以下
list1 = ['google', 'apple', 'intel', 'qualcomm']
list2 = ['nike', 'reebok', 'puma']
your_string = ":".join(list1+list2)
输出应为
'google:apple:intel:qualcomm:nike:reebok:puma'