我的字符串如下:
x = 'abcdefghi'
Run Code Online (Sandbox Code Playgroud)
我想在3的子集中反转它,以便我的输出是:
x = 'cbafedihg'
Run Code Online (Sandbox Code Playgroud)
即第0个索引与第2个索引交换,第3个索引与第5个交换,依此类推.
下面是我的代码基于转换string为list和交换列表中的元素:
string_list = list(x)
for i in range(len(string_list)/3):
string_list[i*3], string_list[i*3+2] = string_list[i*3+2], string_list[i*3]
''.join(string_list)
# Output: 'cbafedihg'
Run Code Online (Sandbox Code Playgroud)
我想知道什么是实现它的最有效和最pythonic的方式.
注意:len(x)%3始终为0.
我在这里寻求一些帮助,我有一个正是这样的清单
像这样的列表:[1,1,1,5,5,5,10,10,10,10,10,8,8,8,8,8,8]
想要的结果:[1,5,10,10,8,8]
我已经尝试了一切可能的方法,一次遍历列表3并仅替换每三分之一.
''.join([List[i] for i in range(len(List) - 1) if List[i + 1] != XX[i]] + [List[-1]])
Run Code Online (Sandbox Code Playgroud)
我只是无法理解它是否有一些python向导谁能做到这一点?
谢谢
是)我有的:
vec=[{'max': 23.425, 'date': u'2017-07-18', 'ismax': False, 'ismin': False, 'min': 14.615},
{'max': 23.83, 'date': u'2017-07-19', 'ismax': False, 'ismin': False, 'min': 14.765},
{'max': 24.07, 'date': u'2017-07-20', 'ismax': False, 'ismin': False, 'min': 14.985}]
Run Code Online (Sandbox Code Playgroud)
现在我想找到整体的最小值和最大值; 我试过这样做:
mini=min(vec[:]['min'])
maxi=max(vec[:]['max'])
Run Code Online (Sandbox Code Playgroud)
但它说:
类型错误索引必须是整数而不是str
我知道冒号代表"循环所有指数"所以我做错了什么?
My Date = 2015-07-30
2015-07-31
2015-08-03
2015-08-04
2015-08-05
2015-08-06
2015-08-07
2015-08-10
2015-08-11
2015-08-12
2015-08-13
2015-08-14
Run Code Online (Sandbox Code Playgroud)
我如何从这里拨打每个第二个日期?我尝试了这个,但这不起作用.
for i in range(0, len(Date), 2):
abc = Date[i]
Run Code Online (Sandbox Code Playgroud) 我想知道列表变量本身和后跟 [:] 的列表变量之间的区别
例如,
# When nums are List[int] and res are List,
# what is the difference between
res.append(nums[:])
# and
res.append(nums)
Run Code Online (Sandbox Code Playgroud)
我在实现递归置换函数时提出了我的问题
# When nums are List[int] and res are List,
# what is the difference between
res.append(nums[:])
# and
res.append(nums)
Run Code Online (Sandbox Code Playgroud)
提前感谢您的帮助!
所以我有一个数组,我正在尝试从中获取一个子数组。
x = np.arange(0,100).reshape((10, 10))
Run Code Online (Sandbox Code Playgroud)
array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
[20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
[30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
[40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
[50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
[60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
[70, 71, 72, 73, …Run Code Online (Sandbox Code Playgroud) ['baNaNa', 7] #字符串和步长 'utGtGt'# 字符串的每个字符按步长向后移动 import ast
in_string = input()
lis = ast.literal_eval(in_string)
st = lis[0]
step = lis[1]
alphabets = 'abcdefghijklmnopqrstuvwxyz'
password = ''
for letter in st:
if letter in alphabets:
index_val = alphabets.index(letter) - (step)
password += alphabets[index_val]
print(password)
Run Code Online (Sandbox Code Playgroud)
我得到的输出是“utgtgt”。我想要'utGtGt'。对此的帮助将不胜感激。
我想将普通列表(main_list)转换为嵌套列表(ans_list),但我不知道如何。
main_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
ans_list = [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
Run Code Online (Sandbox Code Playgroud) 我试着[:]在一开始就明白了,但我找不到任何文件提到它.学习Python高级语法的最佳位置在哪里?Google搜索无法找到[:].但我最后想出来了.我只是想知道哪里是学习Python'技巧'的最佳位置.
例如:
def test(x, y):
x[:] = y
#x = y
>>> a = [0.5,0.6]
>>> b = [0.3]
>>> test(a, b)
>>>
>>> print a
[0.3] # [0.5,0.6]
Run Code Online (Sandbox Code Playgroud) python ×10
python-3.x ×4
list ×3
slice ×2
string ×2
dictionary ×1
duplicates ×1
indexing ×1
loops ×1
numpy ×1
python-2.7 ×1
reverse ×1
sorting ×1
sub-array ×1