[英]How to change format of a dict in Python?
I have a dictionary or list as below.我有一个字典或列表如下。
dictt ={'X , 5,5,8,7,7,6', 'Y , 2,1,2,3,2', 'Z , 3,3,3,4,4,5,4,5,5,4'}
How can I convert it below format as list or dictionary?如何将其转换为以下格式为列表或字典? I want to see each X/Y/Z for their unique values.
我想查看每个 X/Y/Z 的独特值。 I should not use pandas or numpy.
我不应该使用 pandas 或 numpy。
X 5
X 8
X 7
X 6
Y 2
Y 1
Y 3
Z 3
Z 4
Z 5
The original data format:原始数据格式:
['endDate,weight',
' 2020-06-12 00:00:00+03:00 , 91.5,91.9,91.9,91.9,92.55,92.55,92.55,92.55,92.1,92.1,93.3,93.3 ',
' 2020-06-13 00:00:00+03:00 , 91.6,91.6,92.85,92.85,92.85,92.85,92.3,92.3,92.1,92.1,94.1,94.1 ',
' 2020-06-14 00:00:00+03:00 , 91.5,91.5,91.65,91.65,91.5,91.5,92.9,92.9 ',
' 2020-06-15 00:00:00+03:00 , 91.85,91.85,91.6,91.6,91.85,91.85,92.55,92.55,92.4,92.4,93.7,93.7,93.35,93.35 ',
' 2020-06-16 00:00:00+03:00 , 91.6,91.6,91.3,91.3,92.75,92.75,92.15,92.15,93.15,93.15,92.9,92.9 ',
' 2020-06-17 00:00:00+03:00 , 91.05,91.05,91.85,91.85,92.4,92.4,92.4,92.4,94.0,94.0,93.7,93.7,93.05,93.05,93.05,93.05 ',
' 2020-06-18 00:00:00+03:00 , 91.55,91.55,91.45,91.45,91.25,91.25,91.65,92.2,91.95 ',
' 2020-06-19 00:00:00+03:00 , 91.3,91.6,92.45,92.05,91.8,93.1,92.7,93.5,93.15 ',
' 2020-06-20 00:00:00+03:00 , 90.8,90.8,90.6,90.6,90.6,90.6,92.15,92.15,92.05,92.05,91.4,91.4 ']
For the specific input that you provided, which is a set (not a list or dictionary):对于您提供的特定输入,它是一个集合(不是列表或字典):
dictt ={'X , 5,5,8,7,7,6', 'Y , 2,1,2,3,2', 'Z , 3,3,3,4,4,5,4,5,5,4'}
You can get a dictionary wiht the following:您可以获得以下内容的字典:
res={i.split(',')[0].strip() : [int(k.strip()) for k in i.split(',')[1:]] for i in dictt}
which gives the following:这给出了以下内容:
>>> print(res)
{'X': [5, 5, 8, 7, 7, 6], 'Z': [3, 3, 3, 4, 4, 5, 4, 5, 5, 4], 'Y': [2, 1, 2, 3, 2]}
If you now want to print each key with its values separately as you described, you can do it with the following:如果您现在想按照您的描述分别打印每个键及其值,您可以使用以下操作:
for i in res:
for k in set(res[i]):
print(i, k)
X 8
X 5
X 6
X 7
Z 3
Z 4
Z 5
Y 1
Y 2
Y 3
Assuming your data struct is a dict ( not the one you have provided) - here is the code.假设您的数据结构是一个字典(不是您提供的那个) - 这是代码。 (If it is not a dict - please update the question with the actual data struct)
(如果不是字典 - 请使用实际数据结构更新问题)
data = {'X': '5,5,8,7,7,6', 'Y': '2,1,2,3,2', 'Z': '3,3,3,4,4,5,4,5,5,4'}
for k, v in data.items():
temp = set(v.split(','))
for x in temp:
print(f'{k} {x}')
output output
X 5
X 6
X 7
X 8
Y 2
Y 1
Y 3
Z 5
Z 4
Z 3
new_dict = {}
for i in dictt:
key, value = i.split(' , ')
value = (int(num) for num in value.split(','))
value = set(value)
new_dict[key] = value
This will return a dictionary of the form {'X': {5, 8, 7, 6}, 'Y': {2, 1, 3}, 'Z': {3, 4, 5}}
.这将返回
{'X': {5, 8, 7, 6}, 'Y': {2, 1, 3}, 'Z': {3, 4, 5}}
形式的字典。 This is probably not the exact dictionary you're looking for but the question isn't very clear as to what is required and dictionaries cannot have duplicate keys ( {'X': 5, 'X': 8}
is not possible).这可能不是您要查找的确切字典,但问题不是很清楚需要什么,并且字典不能有重复的键(
{'X': 5, 'X': 8}
是不可能的)。
dictt = {"X , 5,5,8,7,7,6", "Y , 2,1,2,3,2", "Z , 3,3,3,4,4,5,4,5,5,4"}
l = [[e.strip() for e in splitted] for splitted in [x.split(",") for x in list(dictt)]]
# Removing duplicates by turning them into set and list again
d = {x[0]: list(set(x[1:])) for x in l}
for k, values in d.items():
for v in values:
print(f"{k} {v}")
Which outputs:哪个输出:
X 6
X 5
X 8
X 7
Y 2
Y 3
Y 1
Z 3
Z 4
Z 5
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.