简体   繁体   English

如何在Python 3中将字符串列表转换为单独的字符串?

[英]How do you convert a list of strings to separate strings in Python 3?

I want to know if you have a list of strings such as: 我想知道您是否有一个字符串列表,例如:

l = ['ACGAAAG', 'CAGAAGC', 'ACCTGTT']

How do you convert it to: 您如何将其转换为:

O = 'ACGAAAG'
P = 'CAGAAGC'
Q = 'ACCTGTT'

Can you do this without knowing the number of items in a list? 您可以在不知道列表中项目数的情况下执行此操作吗? You have to store them as variables. 您必须将它们存储为变量。 (The variables don't matter.) (变量无关紧要。)

Welcome to SE! 欢迎来到SE!

Structure Known 已知结构

If you know the structure of the string, then you might simply unpack it: 如果您知道字符串的结构,则可以简单地将其解压缩:

O, P, Q = my_list

Structure Unknown 结构未知

Unpack your list using a for loop. 使用for循环解压缩列表。 Do your work on each string inside the loop. 对循环中的每个字符串进行处理。 For the below, I am simply printing each one: 在下面,我只是简单地打印每个:

for element in l:
    print(element)

Good luck! 祝好运!

If you don't know the number of items beforehand, a list is the right structure to keep the items in. 如果您事先不知道项目数,则列表是保留项目正确结构。

You can, though, cut off fist few known items, and leave the unknown tail as a list: 但是,您可以切断一些已知的项目,并将未知的尾部保留为列表:

a, b, *rest = ["ay", "bee", "see", "what", "remains"]
print("%r, %r, rest is %r" % (a, b, rest))
a,b,c = my_list

this will work as long as the numbers of elements in the list is equal to the numbers of variables you want to unpack, it actually work with any iterable, tuple, list, set, etc 只要列表中的元素数量等于您要解压缩的变量的数量,此方法就可以工作,它实际上可以与任何可迭代,元组,列表,集合等一起使用

if the list is longer you can always access the first 3 elements if that is what you want 如果列表较长,那么您始终可以访问前三个元素

a = my_list[0] 
b = my_list[1]
c = my_list[2]

or in one line 或一行

a, b, c = my_list[0], my_list[1], my_list[2]

even better with the slice notation you can get a sub list of the right with the first 3 elements 使用切片符号甚至更好,您可以使用前3个元素获得右侧的子列表

a, b, c = my_list[:3]

those would work as long as the list is at least of size 3, or the numbers of variables you want 只要列表的大小至少为3,或者您想要的变量数量,这些变量就可以工作

you can also use the extended unpack notation 您还可以使用扩展的解压缩符号

a, b, c, *the_rest = my_list

the rest would be a list with everything else in the list other than the first 3 elements and again the list need to be of size 3 or more 其余的将是一个列表,其中除前3个元素外,列表中的所有其他内容,列表的大小必须再次为3或更大

And that pretty much cover all the ways to extract a certain numbers of items 这几乎涵盖了提取特定数量项目的所有方式

Now depending of what you are going to do with those, you may be better with a regular loop 现在,根据要处理的内容,定期循环可能会更好

for item in my_list:
    #do something with the current item, like printing it
    print(item)

in each iteration item would take the value of one element in the list for you to do what you need to do one item at the time 在每个迭代中,项目将采用列表中一个元素的值,以供您执行当时需要执行的一项操作


if what you want is take 3 items at the time in each iteration, there are several way to do it 如果您想要的是每次迭代同时获取3个项目,则有几种方法可以实现

like for example 例如

for i in range(3,len(my_list),3)
    a,b,c = my_list[i-3:i]
    print(a,b,c)

there are more fun construct like 还有更多有趣的结构,例如

it = [iter(my_list)]*3
for a,b,c in zip(*it):
    print(a,b,c)

and other with the itertools module. 以及其他的itertools模块。

But now you said something interesting "so that every term is assigned to a variable" that is the wrong approach, you don't want an unknown number of variables running around that get messy very fast, you work with the list, if you want to do some work with each element it there are plenty of ways of doing it like list comprehension 但是,现在您说了一些有趣的话, “使每个术语都分配给一个变量” ,这是错误的方法,您不希望运行未知数量的变量会很快变得混乱,您可以使用列表,如果需要的话为了对每个元素做一些工作,有很多方法可以做到,例如列表理解

my_new_list = [ some_fun(x) for x in my_list ]

or in the old way 或以旧方式

my_new_list = []
for x in my_list:
    my_new_list.append( some_fun(x) )

or if you need to work with more that 1 item at the time, combine that with some of the above 或者如果您一次需要处理多于一项的内容,请将其与上述某些内容结合使用

I do not know if your use case requires the strings to be stored in different variables. 我不知道您的用例是否要求将字符串存储在不同的变量中。 It usually is a bad idea. 通常这是一个坏主意。

But if you do need it, then you can use exec builtin which takes the string representation of a python statement and executes it. 但是,如果确实需要它,则可以使用exec内置exec ,该exec采用python语句的字符串表示形式并执行它。

list_of_strings = ['ACGAAAG', 'CAGAAGC', 'ACCTGTT']

Dynamically generate variable names equivalent to the column names in an excel sheet. 动态生成与Excel工作表中的列名等效的变量名。 (A,B,C....Z,AA,AB........,AAA....) (A,B,C .... Z,AA,AB ........,AAA ....)

variable_names = ['A', 'B', 'C'] in this specific case 在此特定情况下variable_names = ['A', 'B', 'C']

for vn, st in zip(variable_names, list_of_strings):
        exec('{} = "{}"'.format(vn, st))

Test it out, print(A,B,C) will output the three strings and you can use A,B and C as variables in the rest of the program 测试一下, print(A,B,C)将输出三个字符串,您可以在程序的其余部分中将A,B and C用作变量

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Python将字符串列表转换为浮点列表? - How do you convert a list of strings to a list of floats using Python? 如何将文本文档中的字符串转换为列表,并将列表分隔为 python 中的部分 - How to convert strings in a text doc into list, and separate list into parts in python 在Python中,如何将列表列表中的字符串转换为其他类型,如整数,浮点数,布尔值等? - In Python how do you convert strings in a list of list to other types, such as ints, floats, booleans, etc? 在python中,如何将字典中的字符串列表转换为整数列表? - in python, how do you convert a list of strings within a dictionary to a list of integers? 如何以“ pythonic”方式将单独的字符串添加到列表中的各种字符串? - How do you add separate strings to various strings in a list in a 'pythonic' way? 如何将矩阵转换为字符串数组/列表? - how do you convert a matrix to a array/list of strings? 你如何对python中的字符串数字列表进行排序? - How do you sort a list of numbers which are strings in python? Python 如何遍历包含变量的字符串列表? - Python How do you loop through a list of strings that contain variables? 如何使用 python 的循环替换列表中的多个字符串? - how do you replace multiple strings in a list using loop for python? Python:如何使列表以小写字符串开头? - Python: How do you make a list start with the lowercase strings?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM