情報学部 | 菅沼ホーム | 目次 | 索引 |
test = "Suganuma"
+---+---+---+---+---+---+---+---+ | S | u | g | a | n | u | m | a | +---+---+---+---+---+---+---+---+ 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1
>>> test = "Suganuma" >>> test[0] 'S' >>> test[-2] 'm'
[m1:m2[:m3]]
>>> test = "Suganuma" >>> test[0:4] 'Suga' >>> test[:4] 'Suga' >>> test[4:] 'numa' >>> test[4:-1] 'num' >>> test[0:8:2] 'Sgnm'
// 配列を使用 char test1[] = "Suganuma"; cout << test1[0] << endl; // test[0] に対応 cout << test1[strlen(test1)-2] << endl; // test[-2] に対応 for (int i1 = 0; i1 < 8; i1 += 2) // test[0:8:2] に対応 cout << test1[i1]; cout << endl; // string クラスを使用 string test2 = "Suganuma"; cout << test2[0] << endl; // test[0] に対応 cout << test2[test2.length()-2] << endl; // test[-2] に対応 for (int i1 = 0; i1 < 8; i1 += 2) // test[0:8:2] に対応 cout << test2[i1]; cout << endl;
// 配列の利用 char x[] = "abcd"; // const char x[] = "abcd"; とすれば修正不可能 x[1] = 'x'; // 修正もOK cout << x << endl; // axcd を出力 // String クラスの利用 string y = "abcd"; // const string y = "abcd"; とすれば y の修正は不可能 string z = y + "xyz"; // + 演算子による結合もOK z[0] = 'A'; // 修正もOK cout << z << endl; // Abcdxyz を出力
const int x[] = {10, 20, 30}; tuple<int, string, double, string> t(1, "aaa", 3.14, "bbb");
>>> () () >>> tuple() ()
>>> ("abc",) ('abc',) >>> "abc", ('abc',) >>> tuple(["abc"]) ('abc',)
>>> ("abc", 10, "xxx") ('abc', 10, 'xxx') >>> "abc",10,"xxx", ('abc', 10, 'xxx') >>> tuple(["abc", 10, complex(1,3)]) ('abc', 10, (1+3j)) >>> tuple("abc") ('a', 'b', 'c') >>> a = ((1, 2, 3), (10, 20, 30)) >>> a[0][1] 2
const char x[] = "abcd"; const string x = "abcd";
>>> b"abc" b'abc'
>>> bytes(5) b'\x00\x00\x00\x00\x00'
>>> bytes(range(5)) b'\x00\x01\x02\x03\x04' >>> bytes([1, 2, 3, 4, 5]) b'\x01\x02\x03\x04\x05'
ostream& operator << (ostream& stream, vector<int> &s) { if (s.empty()) stream << "[]\n"; else { stream << "["; for (unsigned int i1 = 0; i1 < s.size(); i1++) { stream << s[i1]; if (i1 < s.size()-1) stream << ", "; } stream << "]\n"; } return stream; }
>>> [] [] >>> list() []
>>> [1, 2, 3] [1, 2, 3] >>> a = [1, 2, 3] >>> a[0] 1 >>> b = [[1, 2, 3], [4, 5, 6]] >>> b[1][0] 4
>>> list("abcd") ['a', 'b', 'c', 'd'] >>> list(("a", "b", "c", "d")) ['a', 'b', 'c', 'd']
>>> [x for x in "abcd"] ['a', 'b', 'c', 'd']
C++ の場合
vector<int> s; s.push_back(1); s.push_back(4); s.push_back(3); s.push_back(2); s.push_back(5); sort(s.begin(), s.end(), less<int>()); cout << s; sort(s.begin(), s.end(), greater<int>()); cout << s;
>>> list(range(1, 10, 2)) [1, 3, 5, 7, 9] >>> list(range(10, -3, -1)) [10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2] >>> r = range(1, 10, 2) >>> r.step 2 >>> r[2] 5
C++ の場合
vector<int> s1, s2; for (int i1 = 1; i1 < 10; i1 += 2) s1.push_back(i1); cout << s1; for (int i1 = 10; i1 > -3; i1--) s2.push_back(i1); cout << s2;
>>> bytearray() bytearray(b'')
>>> bytearray(5) bytearray(b'\x00\x00\x00\x00\x00')
>>> bytearray(range(5)) bytearray(b'\x00\x01\x02\x03\x04') >>> bytearray([1, 2, 3, 4, 5]) bytearray(b'\x01\x02\x03\x04\x05')
>>> bytearray(b"abc") bytearray(b'abc')
>>> {} # 空の set {} >>> {1, 2, 3} {1, 2, 3} >>> {1, 2, 3, 3, 3} # 同じ要素は含まない {1, 2, 3} >>> set([1, 2, 3]) {1, 2, 3} >>> frozenset([1, 2, 3]) frozenset({1, 2, 3})
ostream& operator << (ostream& stream, set<int> &s) { if (s.empty()) stream << "{}\n"; else { stream << "{"; set<int>::iterator it; unsigned int k = 0; for (it = s.begin(); it != s.end(); it++) { stream << *it; if (k < s.size()-1) stream << ", "; k++; } stream << "}\n"; } return stream; }
>>> d = {} # 空の辞書 >>> d = dict() # 空の辞書 >>> d = {"aaa" : 10, "bbb" : 20, "ccc" : 30} # key:value 対と波括弧の利用 >>> d = dict(aaa = 10, bbb = 20, ccc = 30) # kwarg の利用 >>> d = dict({"aaa" : 10, "bbb" : 20, "ccc" : 30}) # mapping の利用 >>> d = dict({"aaa" : 10, "bbb" : 20}, ccc = 30) # mapping と kwarg >>> d = dict([("aaa" , 10), ("bbb" , 20), ("ccc" , 30)]) # iterable の利用
ostream& operator << (ostream& stream, unordered_map<string, int> &m) { if (m.empty()) stream << "{}\n"; else { unordered_map<string, int>::iterator it; unsigned int k = 0; stream << "{"; for (it = m.begin(); it != m.end(); it++) { stream << (*it).first << " : " << (*it).second; if (k < m.size()-1) stream << ", "; k++; } stream << "}\n"; } return stream; }
情報学部 | 菅沼ホーム | 目次 | 索引 |