精华内容
下载资源
问答
  • 字符串转换为元组的函数
    2020-11-28 19:04:11

    怎么样:

    $ ./bin/menuscan

    Please enter a single word that is no more than 25 characters: 0123456789

    Thanks! You entered: 0123456789

    ========= MENU =========

    Key Function

    === ========

    C Count the letters

    V Count the vowels

    R Reverse the word

    P Check if the word is a palindrome

    W Enter a new word

    Z Exit

    Please enter a character from the options above: c

    You entered: c

    There are '10' letters in '0123456789'

    ========= MENU =========

    Key Function

    === ========

    C Count the letters

    V Count the vowels

    R Reverse the word

    P Check if the word is a palindrome

    W Enter a new word

    Z Exit

    Please enter a character from the options above: z

    You entered: z

    或者:

    >>> string = "472747372 42 Lawyer John Legend Bishop"

    >>> string.split()[:3] + [' '.join(string.split()[3:5])] + [string.split()[-1]]

    ['472747372', '42', 'Lawyer', 'John Legend', 'Bishop']

    更多相关内容
  • I have a string returnd from a software like "('mono')" from that I needed to convert string to tuple .that I was thinking using ast.literal_eval("('mono')") but it is saying malformed string....

    I have a string returnd from a software like "('mono')" from that I needed to convert string to tuple .

    that I was thinking using ast.literal_eval("('mono')") but it is saying malformed string.

    解决方案

    Since you want tuples, you must expect lists of more than element in some cases. Unfortunately you don't give examples beyond the trivial (mono), so we have to guess. Here's my guess:

    "(mono)"

    "(two,elements)"

    "(even,more,elements)"

    If all your data looks like this, turn it into a list by splitting the string (minus the surrounding parens), then call the tuple constructor. Works even in the single-element case:

    assert data[0] == "(" and data[-1] == ")"

    elements = data[1:-1].split(",")

    mytuple = tuple(elements)

    Or in one step: elements = tuple(data[1:-1].split(",")).

    If your data doesn't look like my examples, edit your question to provide more details.

    展开全文
  • 一个特殊的问题是构造包含0或1项的元组:语法有一些额外的怪癖来适应这些。空元组是由一对空括号构成的; 通过使用逗号跟随值来构造具有一个项目的元组(在括号中包含单个值是不够的)。丑陋但有效。例如:>>> empty ...

    一个特殊的问题是构造包含0或1项的元组:语法有一些额外的怪癖来适应这些。空元组是由一对空括号构成的; 通过使用逗号跟随值来构造具有一个项目的元组(在括号中包含单个值是不够的)。丑陋但有效。例如:

    >>> empty = () >>> singleton = 'hello', # <-- note trailing comma >>> len(empty) 0 >>> len(singleton) 1 >>> singleton ('hello',)

    如果只在字符串对象周围放置一对括号,它们只会将该表达式转换为带括号的表达式(强调添加):带括号的表达式列表产生表达式列表产生的任何内容:如果列表包含至少一个逗号,则它产生一个元组; 否则,它会产生构成表达式列表的单个表达式。

    一对空括号产生一个空元组对象。由于元组是不可变的,因此适用文字规则(即,两次出现的空元组可能会或可能不会产生相同的对象)。

    请注意,元组不是由括号组成,而是使用逗号运算符。唯一的例外是空元组,需要括号 - 在表达式中允许不带括号的“无”会导致含糊不清并允许常见错别字传递未被捕获。

    那是(假设Python 2.7),a = 'Quattro TT'

    print tuple(a) # <-- you create a tuple from a sequence

    # (which is a string)

    print tuple([a]) # <-- you create a tuple from a sequence

    # (which is a list containing a string)

    print tuple(list(a)) # <-- you create a tuple from a sequence

    # (which you create from a string)

    print (a,) # <-- you create a tuple containing the string

    print (a) # <-- it's just the string wrapped in parentheses

    输出符合预期:('Q', 'u', 'a', 't', 't', 'r', 'o', ' ', 'T', 'T')

    ('Quattro TT',)

    ('Q', 'u', 'a', 't', 't', 'r', 'o', ' ', 'T', 'T')

    ('Quattro TT',)

    Quattro TT

    在print语句上添加一些注释。当您尝试在Python 2.7中创建单元素元组作为print语句的一部分时(如在print (a,))中,您需要使用带括号的表单,因为其他的尾随逗号print a,将被视为print语句的一部分,从而导致换行符要从输出中抑制而不是创建元组:除非print语句以逗号结尾,否则最后会写一个'\ n'字符。

    在Python 3.x中,上述示例中的大部分用法实际上都会提高SyntaxError,因为在Python 3中print 会变成一个函数(需要添加一对额外的括号)。但特别是这可能会引起混淆:print (a,) # <-- this prints a tuple containing `a` in Python 2.x

    # but only `a` in Python 3.x

    展开全文
  • 字符串转换为元组

    2021-07-16 13:19:30
    I read some tuple data from a file. The tuples are in string form, for example Color["RED"] = '(255,0,0)'. How can I convert these strings into actual tuples?I want to use this data in PyGame like t.....

    I read some tuple data from a file. The tuples are in string form, for example Color["RED"] = '(255,0,0)'. How can I convert these strings into actual tuples?

    I want to use this data in PyGame like this:

    gameDisplay.fill(Color["RED"])

    # but it doesn't have the right data right now:

    gameDisplay.fill('(255,0,0)')

    解决方案

    You could use the literal_eval of the ast module:

    ast.literal_eval(node_or_string)

    Safely evaluate an expression node or a Unicode or Latin-1 encoded string containing a Python literal or container display. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

    Example:

    >>> import ast

    >>> ast.literal_eval("(255, 0, 0)")

    (255, 0, 0)

    >>>

    Regarding pygame, note that the Color class can also take the name of a color as string:

    >>> import pygame

    >>> pygame.color.Color('RED')

    (255, 0, 0, 255)

    >>>

    so maybe you could generally simplify your code.

    Also, you should not name your dict Color, since there's already the Color class in pygame and that will only lead to confusion.

    展开全文
  • 字符串转换元组

    千次阅读 2018-09-21 09:54:00
    把列表里的某个字符串转换成元祖 a=['哈哈哈哈',2,3,45,66] dd=[] dd.append(a[0]) ...实现过程,先定义一个空列表,然后把字符串添加到列表中,最后用tuple转换元组 list1=[] a='...
  • How to convert following list['abc,cde,eg,ba', 'abc,cde,ba']in to list of tuples?[('abc','cde','eg','ba'), ('abc','cde','ba')]What I have triedoutput = []for item in my_list:a = "','".join((item.split...
  • #-*-coding:utf-8-*-#1、字典dict = {'name': 'Zara','age': 7,'class': 'First'}#字典转为字符串,返回: {'age': 7,'name': 'Zara','class': 'First'}print type(str(dict)),str(dict)#字典可以转为元组...
  • 1、字典dict = {‘name‘: ‘Zara‘, ‘age‘: 7, ‘class‘: ‘First‘}字典转为字符串,返回: {‘age‘: 7, ‘name‘: ‘Zara‘, ‘class‘: ‘First‘}print type(str(dict)), str(dict)字典能够转为元组,返回...
  • 字符串转换元组,发现了一个很好用的函数eval() s = '(0.8, -0.8, (0.0, 0.0, -0.004, 1.0))' t = tuple(eval(s)) print(type(t)) -->输出:<class 'tuple'> print(t) ---> 输出:(0.8, -0.8, (0.0, ...
  • 目录一、问题引入功能快捷键合理的创建标题,有助于目录的生成如何改变文本的样式插入链接与图片如何插入一段漂亮的代码片...我把txt里的内容读取出来,以元组对形式保存在列表中统一格式,然后再保存到sqlite3里。 txt
  • python-字符串转换为列表、元组

    千次阅读 2020-06-07 23:26:11
    #### 输入一个字符串转换成列表、元组,请在下面标注序号的位置填写程序#### #输入一串字符,比如‘I am a student’, tempStr = input() #(1)转换为列表1:['I', 'am', 'a', 'student'] list1=tempStr.split...
  • 碰到的问题:数据库中有一个类型为字符串的字段,该字段对应的值是元组的形式,但是值类型仍属于字符串,在取出使用时,因为类型不匹配出大问题,很烦! 取出的数据和类型如下: ('2018-01-01', '2022-07-22') &...
  • None在列表末尾添加单个元素,任何类型都可以,包括列表或元组等2、L.extend(iterable) -> None以序列的形式,在列表末尾添加多个元素3、L.insert(index, object) -> None在index位置处添加一个元素4、L....
  • 字符串列表元组之间的转换

    千次阅读 2020-12-16 07:10:47
    字符串s = "alex"转换为元组 代码如下: s = "alex" tb = tuple(s) print(tb) c. 将列表li = [“alex”, “seven”]转换为元组 代码如下: li = ["alex", "sever"] tc = tuple(li) print(tc) d. 将元组 tu = ...
  • 字符串转换为列表 使用list()方法 str_1 = 1235 str_2 = 'zhangsan' str_3 = '''lisi''' tuple_1 = list(str_1) tuple_2 = list(str_2) tuple_3 = list(str_3) print(type(tuple_1)) print(type(tuple_2)) print...
  • 一,涉及split()和join()两个函数str.split()这个内置函数实现的是将str转化为list&gt;&gt;&gt; a="I am a boy.hello"&gt;&gt;&gt; a.split(".") #按字符.分隔['I am a...
  • 然后,我们可能还需要将元组转换为字符串列表。与加入join()返回一个字符串,其中的序列元素已通过str分隔符进行了连接。我们将提供list元素作为此函数的参数,并将结果放入列表中。示例listA=[('M','o','n'),('d','...
  • 字符串 1.字符串转为列表: (1):直接用list a = "abc" list_a = list(a) print(type(a)) print(type(list_a)) print(list_a) (2):用符合分割,用split切片 a = "a,b,c" list_a = a.split(",") print(type(a...
  • #-*-coding:utf-8-*-  #1、字典 ...#字典转为字符串,返回:&lt;type 'str'&gt; {'age': 7, 'name': 'Zara', 'class': 'First'} print type(str(dict)), str(dict) #字典可以转为元组...
  • 我有一个具有以下格式的输入文件:[(1,1),(2,1)], 'add', 11[(1,2),(1,3)], 'div', 2[(3,1),(4,1),(3,2),(4,2)], 'times', 240[(2...如何将每个字符串行转换为元组.例如:line: string "[(1,1),(2,1)], 'add', 11" -...
  • #coding:utf-8 #1、字典 dic={'age': 7, 'name': 'Zara...#字典转为字符串 str(dict) ------>"{'age': 7, 'name': 'Zara', 'class': 'First'}" #字典转为元组 tuple(dict)---------->('age', 'na...
  • 字符串转换成列表 a = "[[1,2],[3,4]]" print(type(a)) b = eval(a) c = list(a) print(type(b)) print(type(c)) print(b) print(c) 字符串转换成字典 a = "{1:'a',2:'b'}" print(type(a)) b = dict(eval(a)) print...
  • I've written a function in python that returns a list, for example[(1,1),(2,2),(3,3)]But i want the output as a string so i can replace the comma with another char so the output would be'1@1' '2@2' '3...
  • 分享给大家供大家参考,具体如下:demo.py(字符串、列表、元组的截取):# 切片(截取) [开始索引:结束索引:步长] 步长默认1 结束索引默认截取到末尾# 字符串的截取print("012345"[1:3]) # 12# 列表的截取print([0...
  • 代码: # -*-coding:utf-8 -*- ...#字符串转换为元组 print tuple(str) #列表和元组转换字符串,必须使用join #转换为元组 s1=['a','b','c','d'] s2=('aa','bb','cc','dd') print "".join(tuple(s1)) #

空空如也

空空如也

1 2 3 4 5 ... 20
收藏数 110,502
精华内容 44,200
关键字:

字符串转化为元组