Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 如何在Python中转换以下列表中的列表/元组?_Python 3.x - Fatal编程技术网

Python 3.x 如何在Python中转换以下列表中的列表/元组?

Python 3.x 如何在Python中转换以下列表中的列表/元组?,python-3.x,Python 3.x,如何转换元组中的列表项 drives_a = [chr(x) + ':' for x in range(65, 90) if os.path.exists(chr(x) + ':')] 输出: ['C:', 'D:', 'G:', 'J:', 'L:', 'M:', 'O:', 'S:', 'U:', 'W:', 'Y:'] 您可以使用列表理解来轻松地将所有列表元素转换为元组 lst = ['C:', 'D:', 'G:', 'J:', 'L:', 'M:', 'O:', 'S:', 'U:

如何转换元组中的列表项

drives_a = [chr(x) + ':' for x in range(65, 90) if os.path.exists(chr(x) + ':')]
输出:

['C:', 'D:', 'G:', 'J:', 'L:', 'M:', 'O:', 'S:', 'U:', 'W:', 'Y:']

您可以使用列表理解来轻松地将所有列表元素转换为元组

lst = ['C:', 'D:', 'G:', 'J:', 'L:', 'M:', 'O:', 'S:', 'U:', 'W:', 'Y:']
list_with_tuples = [(x,x) for x in lst]

# output
# [('C:', 'C:'),('D:', 'D:'),('G:', 'G:'),('J:', 'J:'),('L:', 'L:'),('M:', 'M:'),('O:', 'O:'),('S:', 'S:'),('U:', 'U:'),('W:', 'W:'),('Y:', 'Y:')]
如果要删除所有撇号,可以使用“查找并替换”或直接使用以下命令将列表转换为字符串:

string = '[' + ','.join([f'({x},{x})' for x in lst]) + ']'
# output
# '[(C:,C:),(D:,D:),(G:,G:),(J:,J:),(L:,L:),(M:,M:),(O:,O:),(S:,S:),(U:,U:),(W:,W:),(Y:,Y:)]'

但是我需要这种类型的输出plz Gide me[C:,C:,D:,D:,J:,J:,L:,M:,M:,O:,O:,S:,S:,U:,U:,U:,U:,W:,W:,W:,Y:]当操作系统似乎没有定义时,你能提供你的全部代码吗。@TP7,导入操作系统。你在这里想做什么?你想列出Windows操作系统中所有可用的驱动器吗?