通过Python中的数组语法寻址字符串的一部分(Addressing part of a string via the array syntax in Python)
在Python中,是否可以通过标准数组语法处理字符串中的特定字符?
例如,PHP:
$foo = 'bar';
echo $foo[1]; // Output: a
它在PHP中不起作用,所以我想知道是否可以使用其他方式?
Is it, in Python, possible to address a specific character in a string by the standard array syntax?
Example, PHP:
$foo = 'bar';
echo $foo[1]; // Output: a
It didn't work like in PHP so I wanted to know if it is possible using some other way?
原文:https://stackoverflow.com/questions/2141334
更新时间:2019-12-22 15:04
最满意答案
正如Adam指出的那样,使用索引语法可以在Python中读取字符串数组。 但是,使用以下语法写入字符串是不可能的:
>>> s = 'bar'
>>> s[2] = 'z'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
也许这是你遇到的问题?
As Adam pointed out, reading from a string array is possible in Python using the indexing syntax. What isn't possible though is writing to a string using this syntax:
>>> s = 'bar'
>>> s[2] = 'z'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
Maybe this was the problem you ran into?
2010-01-26
相关问答
当然有: x = 5
a = '{1:.{0}f}'.format(x, 1.12345111)
print(a) # -> 1.12345
如果您不想指定位置( 0和1 ),则只需反转输入即可: a = '{:.{}f}'.format(1.12345111, x)
# ^ the float that is to be formatted goes first
这是因为format()的第一个参数转到了字符串的第一个(最外面的)括号 。 结果,以下失
...
正如Adam指出的那样,使用索引语法可以在Python中读取字符串数组。 但是,使用以下语法写入字符串是不可能的: >>> s = 'bar'
>>> s[2] = 'z'
Traceback (most recent call last):
File "", line 1, in
TypeError: 'str' object does not support item assignment
也许这是你遇到的问题? As Adam pointed out,
...
foreach($test as $key=> $value)
{
if (strpos($value,'set-cookie') !== false)
{
echo $key; // print key containing searched string
}
}
这是另一种选择。 ( 工作示例 ) $matches = preg_grep('/set-cookie/', $test);
$keys = array_keys($matches);
...
尝试这个: b = [a[i][:3] for i in range(len(a))]
print(b)
输出: ['app','foo','cow']
Try this: b = [a[i][:3] for i in range(len(a))]
print(b)
Output: ['app','foo','cow']
函数strpos返回false或0,因此数组匹配不会保存任何带有文本jav的单词。 这是我的解决方案: $myarray = ['java', 'java3', 'mouse', 'java2', 'keyboard'];
$matches = [];
foreach ($myarray as $fn) {
if (strpos($fn, 'jav') !== false) {
$matches[] = $fn;
}
}
PS:我不知道我是否理解你的问题,但我希望
...
如果我找对你,这可能是一个解决方案: my @names = (
'{img_names_will_change.jpg}some unknown text',
'{img_names_will_change.jpg}some unknown text',
'{img_names_will_change.jpg}some unknown text'
);
my @extract;
foreach my $name ( @names ) {
if ( $name =~ m/{(\w+
...
此代码有效: answers = [
['person1', ' 2 1": ["answer 1"], "', ' 3 1": ["answer 0"], "'],
['person2', ' 2 1": ["answer 3"], "', ' 3 1": ["answer 1"], "']]
result = []
for person_ans in answers:
list = []
for val in person_ans:
...
有混合数据的问题 - index strings有些数字。 需要转换为string作为第一步: newestdata = newestdf.assign(
idobject=newestdf.index.astype(str).str.split('/').str[1].str.replace("-", "").str.extract('(\d+)', expand=False).astype(int))
^^^^^
...
如果你想使用find你需要在shell=False时传递一个args list 。 check_output将适用于您的情况,您可以切片列表而不是使用范围,您需要str.format来包装*每个specie / ele: from subprocess import check_output
species_array = ["homo_sapiens", "pan_troglodytes", "pongo_abelii", "gorilla_gorilla", "macaca_mulatta"
...