简体   繁体   English

Python剥离列表以删除不需要的字符

[英]Python Strip a list to remove unwanted characters

I have a list in my program called student. 我的程序中有一个名为“学生”的列表。
I append to this list a string with the name "Reece". 我将名称为“ Reece”的字符串附加到此列表中。
When I print the variable holding my name it outputs: 当我打印包含我的名字的变量时,它输出:

Reece

When I print the list which I appended the variable too it outputted: 当我打印列表时我也附加了变量,它输出:

['Reece']

How can I like strip it to remove these unwanted characters []' 我如何喜欢剥离它来删除这些不需要的字符[]'

The code I use is this: 我使用的代码是这样的:

name = "Reece"
print name      #Outputs - Reece

student = []
student.append(name)
print student        #Outputs - ["Reece"]

If I then appended another thing: 如果再加上另一件事:

Class = "Class A"
student.append(Class)
print student         #Outputs - ["Reece", "Class A"]

This should produce your desired output 这应该产生您想要的输出

print student[0]

The [ ] are printed because student is a list and [ ] is the list representation. 因为学生是列表,而[]是列表表示,所以会打印[]。

If you want to print multiple names in a list, you should look at the join method. 如果要在列表中打印多个名称,则应查看join方法。

It works like this: 它是这样的:

", ".join(["Reece", "Higgs"])

Gives: 得到:

Reece, Higgs

['Reece'] is the string representation of a list, the square brackets tell you that's what it is, and the quotes mark the start and end of the string in it. ['Reece']是列表的字符串表示形式,方括号告诉您的是它的意思,引号标记其中的字符串的开始和结束。 A longer list might look like this: ['Reece', 'Andy', 'Geoff'] . 更长的列表可能看起来像这样: ['Reece', 'Andy', 'Geoff']

If you'd like to display just one entry, you can refer to its place in the list, counting from zero upwards: 如果您只想显示一个条目,则可以引用它在列表中的位置,从零开始向上计数:

print student[0]

You might use the list as part of a loop: 您可以将列表用作循环的一部分:

for person in student:
    print person,   

The trailing comma removes new lines. 末尾逗号删除新行。 If you want each name on a line by itself you can just do print person . 如果您想单独使用一行中的每个名称,则可以print person

It's also possible to make a single string from a list. 也可以从列表中创建单个字符串。 You can do this with a string and the join method. 您可以使用字符串和join方法执行此操作。

" ".join(student)  # will join all the list entries together with spaces in between
",".join(student)  # will join all the list entries with a comma in between
" hedgehog ".join(student) # will join all the list entries with ' hedgehog ' in between

It depends on the format in which you would like the list to be printed. 这取决于您要打印列表的格式。

If you want to print the list separated by spaces, you should convert it to a string, because Python's style of printing lists is with the brackets. 如果要打印用空格分隔的列表,则应将其转换为字符串,因为Python的打印列表样式带有方括号。

print ' '.join(list)

The ' ' can be replaced with different strings that will join the strings from your list. 可以用其他字符串替换' ' ,这些字符串将连接列表中的字符串。

If you would like to print a specified element on the list on the other hand, you should use: 另一方面,如果要在列表上打印指定的元素,则应使用:

print list[0]

where in place of 0 you can put any number that is in the length range of the list (which means 0 to list length minus 1 as the lists are 0-based). 在0处,您可以放置​​列表长度范围内的任何数字(这意味着0到列表长度减去1,因为列表是基于0的)。

Finally, to print all the elements from the list, simply use: 最后,要打印列表中的所有元素,只需使用:

for element in list:
    print element

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM