1. 程式人生 > >python刷題筆記

python刷題筆記

c++和python處理讀入資料:

c++:
#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
    vector<string> s;
    string temp;
    while(cin>>temp)
    {
        s.push_back(temp);
    }

python:

list1 = list(map(str,input().split()))
如果是輸入兩個字串,想要將其分割,可以這麼寫:
n, m = [int(x) for x in input().split()]
讀入字串:
s = list(map(str,input().split(’ ‘)))
列印:print(res,end=’ ')

input()和input().split()的區別:

s = input()
print(s)
輸入:abc123 def456
輸出:abc123 def456

s = input().split()
print(s)
輸入:abc123 def456
輸出:['abc123', 'def456']