Tuple to dictionary in python by using text file

I have a code which returns a dictionary with the names as the keys and the corresponding values which are tuples of numbers. The first number after the name controls how many numbers are in the corresponding tuple (the numbers included in the tuple are taken from the left to right). For example, the line of text "Ali 6 7 6 5 12 31 61 9" has 6 as the first number after the name and th

通过使用文本文件在Python中对元组进行字典

我有一个代码返回一个字典,名称作为键和相应的数值元组。 名称后的第一个数字控制相应元组中包含多少个数字(元组中包含的数字从左到右)。 例如,文本"Ali 6 7 6 5 12 31 61 9"具有6作为名称后面的第一个数字,并且这行文本成为具有关键字“Ali”的字典条目,并且相应的值是元组接下来的六个整数“阿里”:(7,6,5,12,31,61)。 这是我拍摄代码的电影 Bella 5 2 6 2 2 30 4 8 9 2 Gill 2 9 7 54 67 Jin 3

Convert list of dictionaries to Dataframe

I have a list of dictionaries like this: [{'points': 50, 'time': '5:00', 'year': 2010}, {'points': 25, 'time': '6:00', 'month': "february"}, {'points':90, 'time': '9:00', 'month': 'january'}, {'points_h1':20, 'month': 'june'}] and I want to turn this into a pandas DataFrame like this: month points points_h1 time year 0 NaN 50 NaN 5:00 2010 1 february 25

将字典列表转换为Dataframe

我有这样的字典列表: [{'points': 50, 'time': '5:00', 'year': 2010}, {'points': 25, 'time': '6:00', 'month': "february"}, {'points':90, 'time': '9:00', 'month': 'january'}, {'points_h1':20, 'month': 'june'}] 我想把它变成一个像这样的熊猫DataFrame : month points points_h1 time year 0 NaN 50 NaN 5:00 2010 1 february 25 NaN 6:00 NaN 2 january

Is there a short contains function for lists?

我看到人们正在使用any来收集另一个列表来查看一个项目是否存在于一个列表中,但是有一个快速的方法可以做到吗?: if list.contains(myItem): # do something You can use this syntax: if myItem in list: # do something Also, inverse operator: if myItem not in list: # do something It's work fine for lists, tuples, sets and dicts (check keys). Note that this is an O(n) operation in lists

列表中是否有一个简短的包含函数?

我看到人们正在使用any来收集另一个列表来查看一个项目是否存在于一个列表中,但是有一个快速的方法可以做到吗?: if list.contains(myItem): # do something 你可以使用这个语法: if myItem in list: # do something 另外,逆运算符: if myItem not in list: # do something 这对于列表,元组,集合和字典(检查关键字)是很好的。 请注意 ,这是列表和元组中的O(n)操作,但是在集合和字典中是O(1)操

Convert list to tuple in Python

I'm trying to convert a list to a tuple. When I google it, I find a lot of answers similar to: l = [4,5,6] tuple(l) But if I do that I get this error message: TypeError: 'tuple' object is not callable How can I fix this problem? It should work fine. Don't use tuple , list or other special names as a variable name. It's probably what's causing your problem. >

在Python中将列表转换为元组

我试图将列表转换为元组。 当我谷歌,我发现了很多类似的答案: l = [4,5,6] tuple(l) 但如果我这样做,我会得到这个错误信息: TypeError:'tuple'对象不可调用 我该如何解决这个问题? 它应该工作正常。 不要使用tuple , list或其他特殊名称作为变量名称。 这可能是什么导致你的问题。 >>> l = [4,5,6] >>> tuple(l) (4, 5, 6) 扩展eumiro的评论,通常tuple(l)会将列表l转换为元组:

Convert all strings in a list to int

Possible Duplicate: How to convert strings into integers in python? How to convert a string list into an integer in python In python, I want to convert all strings in a list to ints. So if I have: results = ['1', '2', '3'] How do I make it: results = [1, 2, 3] Use the map function(in py2): results = map(int, results) In py3: results = list(map(int, results)) Use a list comprehensi

将列表中的所有字符串转换为int

可能重复: 如何将字符串转换为Python中的整数? 如何将一个字符串列表转换为Python中的整数 在Python中,我想将列表中的所有字符串转换为整数。 所以如果我有: results = ['1', '2', '3'] 我如何做到这一点: results = [1, 2, 3] 使用地图功能(在py2中): results = map(int, results) 在py3中: results = list(map(int, results)) 使用列表理解: results = [int(i) for i in results] 例如 >>&g

How to convert list to string

Possible Duplicate: How to convert list into a string? How can I convert a list to a string using Python? By using ''.join list1 = ['1', '2', '3'] str1 = ''.join(list1) Or if the list is of integers, convert the elements before joining them. list1 = [1, 2, 3] str1 = ''.join(str(e) for e in list1) >>> L = [1,2,3] >>> " ".join(str(x) for x in L) '1 2 3' L =

如何将列表转换为字符串

可能重复: 如何将列表转换为字符串? 我如何使用Python将列表转换为字符串? 通过使用''.join list1 = ['1', '2', '3'] str1 = ''.join(list1) 或者如果列表是整数,则在加入它们之前转换元素。 list1 = [1, 2, 3] str1 = ''.join(str(e) for e in list1) >>> L = [1,2,3] >>> " ".join(str(x) for x in L) '1 2 3' L = ['L','O','L'] makeitastring = ''.join(map(str, L))

Convert a list of characters into a string

If I have a list of chars: a = ['a','b','c','d'] How do I convert it into a single string? a = 'abcd' 使用空字符串的join方法将所有字符串与中间的空字符串连接在一起,如下所示: >>> a = ['a', 'b', 'c', 'd'] >>> ''.join(a) 'abcd' This works in JavaScript or Ruby, why not in Python? >>> ['a', 'b', 'c'].join('') Traceback (most recent call last): File "<stdin>",

将字符列表转换为字符串

如果我有一个字符列表: a = ['a','b','c','d'] 我如何将它转换为单个字符串? a = 'abcd' 使用空字符串的join方法将所有字符串与中间的空字符串连接在一起,如下所示: >>> a = ['a', 'b', 'c', 'd'] >>> ''.join(a) 'abcd' 这适用于JavaScript或Ruby,为什么不在Python中? >>> ['a', 'b', 'c'].join('') Traceback (most recent call last): File "<stdin>", line 1, in <module&

Convert string representation of list to list

I was wondering what the simplest way is to convert a string list like the following to a list : x = u'[ "A","B","C" , " D"]' Even in case user puts spaces in between the commas, and spaces inside of the quotes. I need to handle that as well to: x = ["A", "B", "C", "D"] in Python. I know I can strip spaces with strip() and split() using the split operator and check for non alphabets. But

将列表的字符串表示转换为列表

我想知道最简单的方法是将如下所示的string列表转换为list : x = u'[ "A","B","C" , " D"]' 即使用户在逗号和逗号之间留有空格。 我还需要处理以下问题: x = ["A", "B", "C", "D"] 在Python中。 我知道我可以使用拆分运算符strip()和split()空格,并检查非字母表。 但代码变得非常笨拙。 有没有我不知道的快速功能? >>> import ast >>> x = u'[ "A","B","C" , " D"]' >>> x = ast.liter

Elegant Python function to convert CamelCase to snake

例: >>> convert('CamelCase') 'camel_case' This is pretty thorough: def convert(name): s1 = re.sub('(.)([A-Z][a-z]+)', r'1_2', name) return re.sub('([a-z0-9])([A-Z])', r'1_2', s1).lower() Works with all these (and doesn't harm already-un-cameled versions): >>> convert('CamelCase') 'camel_case' >>> convert('CamelCamelCase') 'camel_camel_case' >>>

优雅的Python函数将CamelCase转换为蛇

例: >>> convert('CamelCase') 'camel_case' 这是非常彻底的: def convert(name): s1 = re.sub('(.)([A-Z][a-z]+)', r'1_2', name) return re.sub('([a-z0-9])([A-Z])', r'1_2', s1).lower() 适用于所有这些(并且不会损害已经非拼图的版本): >>> convert('CamelCase') 'camel_case' >>> convert('CamelCamelCase') 'camel_camel_case' >>> convert('Camel2Camel2Case') 'cam

Convert hex string to int in Python

How do I convert a hex string to an int in Python? I may have it as " 0xffff " or just " ffff ". Without the 0x prefix, you need to specify the base explicitly, otherwise there's no way to tell: x = int("deadbeef", 16) With the 0x prefix, Python can distinguish hex and decimal automatically. >>> print int("0xdeadbeef", 0) 3735928559 >>> print int("

在Python中将十六进制字符串转换为int

如何在Python中将十六进制字符串转换为int? 我可能会把它当作“ 0xffff ”或者只是“ ffff ”。 没有 0x前缀,你需要明确指定基地,否则无法告诉: x = int("deadbeef", 16) 使用 0x前缀,Python可以自动区分十六进制和十进制。 >>> print int("0xdeadbeef", 0) 3735928559 >>> print int("10", 0) 10 (您必须指定0作为基础才能调用此前缀猜测行为;省略第二个参数表示假设基数为10)。 int(hexString,