我怎样才能让它工作?
color_table = {"Red":[1,2,3], "Blue":[4,5,6]}
Run Code Online (Sandbox Code Playgroud)
如何单独访问值?
color_table[Red].0 = 1
color_table[Red].1 = 2
color_table[Red].2 = 3
color_table[Blue].0 = 4
color_table[Blue].1 = 5
Run Code Online (Sandbox Code Playgroud)
我想将这些值分配给一个变量。例如:
x = color_table[Red].0
Run Code Online (Sandbox Code Playgroud) 我在 python3 中遇到了 Unicode 问题,我似乎无法理解为什么会发生这种情况。
\n\nsymbol= "\xe1\xbf\x87\xcc\xa3"\nprint(len(symbol))\n>>>>2\nRun Code Online (Sandbox Code Playgroud)\n\n这封信来自一个单词:\xe1\xbc\x90\xcc\xa3\xce\xbd\xcc\xa3\xcf\x84\xcc\xa3\xe1\xbf\x87\xcc\xa3[\xce\xb1\xe1 \xbd\x90\xcf\x84]\xe1\xbf\x87 我在其中组合了变音符号。我想在 Python 3 中进行统计分析并将结果存储在数据库中,问题是我还将字符的位置(索引)存储在文本中。数据库应用程序正确地将示例中的符号变量计为一个字符,而 Python 将其计为两个字符 - 丢弃整个索引。
\n\n该项目要求我保留变音符号,因此我不能简单地忽略它们或.replace("combining diacritical mark","")对字符串执行 a 操作。
由于 Python3 将 unicode 作为字符串的默认值,我对此感到有点困惑。
\n\n我尝试使用Greek-accentuation 中的base()、strip()和方法: https://pypi.org/project/greek-accentuation/但这也没有帮助。strip_length()
项目要求是:
\n\n这是该项目的简化代码:
\n\n# -*- coding: utf-8 -*-\nimport csv\nfrom alphabet_detector import AlphabetDetector\nad = AlphabetDetector()\nwith open("tbltext.csv", "r", encoding="utf8") as txt:\n …Run Code Online (Sandbox Code Playgroud) 如何获取我当前使用的 IPython 版本?
我知道sys.version,但这是我目前使用的Python版本。
编辑:我是知道的ipython --version,但我要检查内当前IPython的会话。!ipython --version对我不起作用,因为我可能安装了多个 IPython 版本。
我真的不确定为什么这行不通。这是代码的重要部分(来自leetcode挑战)。第一行抛出NameError。
def totalFruit(self, tree: List[int]) -> int:
pass
Run Code Online (Sandbox Code Playgroud)
如果我尝试List先导入,则会出现错误No module named 'List'。我正在使用Anaconda的Python 3.7.3。
我想从某个文件中删除非ascii字符.我已经尝试了这么多正则表达式.
sed -e 's/[\d00-\d128]//g' # not working
cat /bin/mkdir | sed -e 's/[\x00-\x7F]//g' >/tmp/aa
Run Code Online (Sandbox Code Playgroud)
但是这个文件包含一些非ascii字符.
[root@asssdsada ~]$ hexdump /tmp/aa |more
00 01 02 03 04 05 06 07 - 08 09 0A 0B 0C 0D 0E 0F 0123456789ABCDEF
00000000 45 4C 46 B0 F0 73 38 C0 - C0 BC BC FF FF 61 61 61 ELF..s8......aaa
00000010 A0 A0 50 E5 74 64 50 57 - 50 57 50 57 D4 D4 51 E5 ..P.tdPWPWPW..Q.
00000020 74 64 6C …Run Code Online (Sandbox Code Playgroud) import ast
code = '1+1'
expr = ast.parse(code).body[0]
print(type(expr))
compile(ast.Expression(expr), 'string', "eval")
Run Code Online (Sandbox Code Playgroud)
让我明白
import ast
code = '1+1'
expr = ast.parse(code).body[0]
print(type(expr))
compile(ast.Expression(expr), 'string', "eval")
Run Code Online (Sandbox Code Playgroud)
compile(expr, '<string>', "eval")
Run Code Online (Sandbox Code Playgroud)
也不起作用:
TypeError: expected Expression node, got Expr
Run Code Online (Sandbox Code Playgroud) 我已经在我的 Ubuntu 16.04 上下载了 Python 3.8,如果我写python3.8它会显示它存在,但是当我写的时候我python --version得到了我的旧 Python 版本 3.5
我正在尝试__new__在 Python 的元类中键入该方法,以便它使 mypy 满意。代码将是这样的(取自pep-3115 - “Python 3000 中的元类”并精简了一点):
from __future__ import annotations
from typing import Type
# The metaclass
class MetaClass(type):
# The metaclass invocation
def __new__(cls: Type[type], name: str, bases: tuple, classdict: dict) -> type:
result = type.__new__(cls, name, bases, classdict)
print('in __new__')
return result
class MyClass(metaclass=MetaClass):
pass
Run Code Online (Sandbox Code Playgroud)
有了这个,mypy 抱怨说,Incompatible return type for "__new__" (returns "type", but must return a subtype of "MetaClass"),指着线def __new__。
我也试过:
def __new__(cls: Type[MetaClass], name: str, bases: …Run Code Online (Sandbox Code Playgroud) 为什么都insort_left和insort_right存在; 由于元素相等,结果不总是相同的吗?
>>> import bisect
>>> foo = [1,2,3]
>>>
>>> bisect.insort_left(foo, 1)
>>> foo
[1, 1, 2, 3]
>>>
>>> bisect.insort_right(foo, 1)
>>> foo
[1, 1, 1, 2, 3]
Run Code Online (Sandbox Code Playgroud) 我有一个包含字符串的列表,如下所示。
candidates = ["Hello", "World", "HelloWorld", "Foo", "bar", "ar"]
Run Code Online (Sandbox Code Playgroud)
我希望列表被过滤为["HelloWorld", "Foo", "Bar"],因为其他的是子字符串。我可以这样做,但不认为它很快或优雅。
def filter_not_substring(candidates):
survive = []
for a in candidates:
for b in candidates:
if a == b:
continue
if a in b:
break
else:
survive.append(a)
return survive
Run Code Online (Sandbox Code Playgroud)
有什么快速的方法可以做到吗?