请改写为python:using namespace std; int main() { set<char> a; string s; getline(cin, s);…
- User
请改写为python:using namespace std; int main() { set a; string s; getline(cin, s); for(int i=0; i<s.length(); i++) { a.insert(s[i]);
} for(int i = 0;; i++) { i = s.find(i, ' '); if(i == -1) { break; } s.erase(i); } for(auto it=a.begin(); it!=a.end(); it++) { cout<<*it; } return 0; }
- Assistant
Python equivalent of C++ code using namespace std
import the required module
from typing import Set
function to remove all spaces from a given string
def remove_spaces(s: str) -> str: """ Removes all spaces from a string """ return s.replace(' ', '')
main function
def main() -> int: """ Main function """ # create an empty set a: Set[str] = set()
# read input string from the user s: str = input() # add each character to the set for c in s: a.add(c) # remove all spaces from the string s = remove_spaces(s) # print the resulting set print(''.join(sorted(a))) return 0call the main function
main()
- User
输入一个字符串(包括大小写字母和空格),除去空格输出在字符串中出现过的字符。
输入格式: 输入一个字符串
输出格式: 输出在字符串中出现过的字符,每个字符打印一次,按字母顺序打印(注意:空格不算,大小写字符算不同字符)。打印时按字母的顺序,大写在前,小写在后。
输入样例: 在这里给出一组输入。例如:
This is a good hat 输出样例: 在这里给出相应的输出。例如:
Tadghiost
- Assistant
Python 3.7.3
将输入的字符串转为set,重复字符会自动去重
set可以保证输出时无序重复
s = input() # 读入字符串 char_set = set() # 字符集
for c in s: if c != ' ': # 如果不是空格则加入字符集 char_set.add(c)
结果按字母表顺序排列
res_lst = sorted(char_set, key= lambda x : (x.upper(), x.islower()))
输出结果
print(*res_lst, sep='')