Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
全部,
我有以下列表:
["abc", "def", 'ghi"]
我需要从中得到的是一个字符串:
"abc,def,ghi"
第一个和第二个元素之间有一个逗号,但不是在字符串的末尾。
不幸的是,Python 没有这样的结构:
std::string str; for( int i = 0; i < 3; i++ ) { str += list[i]; if( i != 2 ) str += ","; }
我如何在 Python 中做到这一点?
从目标的角度来考虑它,而不是你在 C++ 中的方式。您想将元素连接在一起:
','.join(elements)