简体   繁体   English

Python:如何将单引号添加到长列表中

[英]Python: How to add single quotes to a long list

I want to know the quickest way to add single quotes to each element in a Python list created by hand.我想知道为手工创建的 Python 列表中的每个元素添加单引号的最快方法。

When generating a list by hand, I generally start by creating a variable (eg my_list), assigning it to list brackets, and then populating the list with elements, surrounded by single quotes:手动生成列表时,我通常首先创建一个变量(例如 my_list),将其分配给列表括号,然后用元素填充列表,并用单引号括起来:

my_list = []

my_list = [ '1','baz','ctrl','4' ]

I want to know if there is a quicker way to make a list, however.但是,我想知道是否有更快的方法来制作列表。 The issue is, I usually finish writing my list, and then go in and add single quotes to every element in the list.问题是,我通常写完我的列表,然后进去给列表中的每个元素添加单引号。 That involves too many keystrokes, I think.我认为这涉及太多的击键。

A quick but not effective solution on Jupyter NB's is, highlighting your list elements and pressing the single quote on your keyboard. Jupyter NB 的一个快速但无效的解决方案是,突出显示您的列表元素并按下键盘上的单引号。 This does not work if you have a list of words that you want to turn to strings, however;但是,如果您有要转换为字符串的单词列表,这将不起作用; Python thinks you are calling variables (eg my_list = [1, baz, ctrl, 4 ]) and throws a NameError message. Python 认为您正在调用变量(例如 my_list = [1, baz, ctrl, 4 ])并抛出 NameError 消息。 In this example, the list element baz would throw:在这个例子中,列表元素 baz 会抛出:

NameError: name 'baz' is not defined

I tried this question on SO, but it only works if your list already contains strings: Join a list of strings in python and wrap each string in quotation marks .我在 SO 上尝试过这个问题,但它仅在您的列表已经包含字符串时才有效: Join a list of strings in python and wrap each string in quotes This question also assumes you are only working with numbers: How to convert list into string with quotes in python .这个问题还假设您只使用数字: 如何在 python 中将列表转换为带引号的字符串

I am not working on a particular project at the moment.我目前没有从事某个特定项目。 This question is just for educational purposes.这个问题仅用于教育目的。 Thank you all for your input/shortcuts.感谢大家的输入/快捷方式。

Yeah but then why not:是的,但为什么不呢:

>>> s = 'a,b,cd,efg'
>>> s.split(',')
['a', 'b', 'cd', 'efg']
>>> 

Then just copy it then paste it in然后只需复制它然后粘贴它

Or idea from @vash_the_stampede:或者来自@vash_the_stampede 的想法:

>>> s = 'a b cd efg'
>>> s.split()
['a', 'b', 'cd', 'efg']
>>>

The best way I found was:我发现的最好方法是:

>>> f = [10, 20, 30]

>>> new_f = [f'{str(i)}' for i in x]

>>> print(new_f)

['10', '20', '30']

You can take input as string and split it to list For eg.您可以将输入作为字符串并将其拆分为列表例如。

eg="This is python program"
print(eg)
eg=eg.split()
print(eg)

This will give output这将给出输出

This is python program这是python程序

['This', 'is', 'python', 'program'] ['this', 'is', 'python', 'program']

Hope this helps希望这可以帮助

It's been a while, but I think I've found a quick way following @U10-Forward's idea:已经有一段时间了,但我想我已经找到了遵循 @U10-Forward 想法的快速方法:

>>> list = ('A B C D E F G Hola Hello').split()
>>> print(list)

['A', 'B', 'C', 'D', 'E', 'F', 'G', 'Hola', 'Hello']

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

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