我要转换
E =[1,6,1,7,2,3,2,6,3,2,3,8,4,5,4,7,5,4,5,9,6,1,6,7,6,2,7,1,7,6,7,4,8,9,8,3,9,8,9,5,]
进入
E = [{1,6},{1,7},{2,3},{2,6},{3,2},{3,8},{4,5},{4,7},{5,4},{5,9},{6,1},{6,7},{6,2},{7,1},{7,6},{7,4},{8,9},{8,3},{9,8},{9,5}
但我明白了
E=[[{1, 6}], [{1, 7}], [{2, 3}], [{2, 6}], [{2, 3}], [{8, 3}], [{4, 5}], [{4, 7}], [{4, 5}], [{9, 5}], [{1, 6}], [{6, 7}], [{2, 6}], [{1, 7}], [{6, 7}], [{4, 7}], [{8, 9}], [{8, 3}], [{8, 9}], [{9, 5}]]
,集{。}中的顺序也被混淆了(不保留顺序)。
为什么会这样,我该如何解决?
我的代码是:
def convert_to_set(x):
sets = []
l = len(x)
for i in range(0,l,2):
set1 = []
set1.append({x[i],x[i+1]})
sets.append(set1)
return sets
答案 0 :(得分:1)
如果要保留订单,则需要从set
切换到tuple
。这样很容易做到:
list(zip(E[::2], E[1::2]))
那给你:
[(1, 6),
(1, 7),
(2, 3),
(2, 6),
(3, 2),
...
答案 1 :(得分:0)
以下是一个选项:
function check_match($string) {
if (preg_match('/^([a-z]{2,})(?:\1)+(.*)$/', $string, $matches)) {
return strlen($matches[2]) == 0 || $matches[2] == substr($matches[1], 0, strlen($matches[2])) ? $matches[1] : false;
}
return false;
}
foreach (array("abca", "abcabcab", "abcabcabcabc", "ababcababc", "aabbaabb", "aabaab", "aabaabd", "abcd", "aabcaabc", "abcabca") as $str) {
if ($pattern = check_match($str))
echo "$str matched $pattern\n";
else
echo "no pattern for $str\n";
}
此外,集合是无序的,因此您无法保留顺序。