Create dictionary values automatically? [closed] - python

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
So i have a list with strings for path of images:
["folder1/_D7327", "folder1/_D7527", "folder2/_D7455", "folder3/_D1237"]
I want to automatically create a dictionary to store each photo:
{"folder1": ["_D7327", "_D7527"], "folder2": ["_D7455"], "folder3": ["_D1237"]}
How can I achieve something like this automatically?

You can use dict.setdefault. For example:
lst = ["folder1/_D7327", "folder1/_D7527", "folder2/_D7455", "folder3/_D1237"]
out = {}
for path in lst:
folder, file = path.split("/")
out.setdefault(folder, []).append(file)
print(out)
Prints:
{'folder1': ['_D7327', '_D7527'], 'folder2': ['_D7455'], 'folder3': ['_D1237']}

Related

python handle json list format to array list [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 11 months ago.
Improve this question
use python 3.6
this json: [["10000000000"], ["12000000000"]]
i want get like result like
"data": {"calleeInfo": [{"phone": "10000000000",},{"phone": "12000000000",}]}
By list generation
temp = [["10000000000"], ["12000000000"]]
value = [{"phone": i[0]} for i in temp]
result = {"data": {"calleeInfo": value}}
print(result)
Output
{'data': {'calleeInfo': [{'phone': '10000000000'}, {'phone': '12000000000'}]}}

convert a list of dictionary in given format [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Input_data=[{​​​'filename':'file_A','start_page':1,'end_page':3,'angle':90}​​​,{'filename':'file_A','start_page':6,'end_page':8,'angle':270}​​​,{'filename':'file_B','start_page':2,'end_page':3,'angle':90}​​​,{'filename':'file_B','start_page':5,'end_page':5,'angle':270}​​​]
output=[{​​​'filename':'file_A','page':1,'angle':90}​​​,
{​​​'filename':'file_A','page':2,'angle':90}​​​,{​​​'filename':'file_A','page':3,'angle':90}​​​,{​​​'filename':'file_A','page':6,'angle':270}​​​,{​​​'filename':'file_A','page':7,'angle':270}​​​,
{​​​'filename':'file_A','page':8,'angle':270}​​​,{​​​'filename':'file_B','page':2,'angle':90}​​​,
{​​​'filename':'file_B','page':3,'angle':90}​​​,{​​​'filename':'file_B','page':5,'angle':270}​​​]
}
If I understood correctly, you want to do:
output = []
for d in Input_data:
for i in range(d["start_page"], d["end_page"] + 1):
output.append({'filename':d["filename"], 'page':i, 'angle':d["angle"]})
print(output)

In python how to store the values of loop in a python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I tried to append these values in the list but it appends only else part.
I want to store these values i printed in a list. How can i achieve this??
for i in d["Cabin new"]:
if i=="nan":
print ("na")
else:
print (i[1:])
Append in both places:
res = []
for i in d["Cabin new"]:
if i=="nan":
res.append('na')
else:
res.append(i[1:])

string concatination at specificv value [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
these items are in a list
SF-04-08-010-MD01,
AHU-VVIP-02-003-MD03,
AHU-02-17-019-DPS03,
AHU-T3-01-PL-TS01,
EF-03-32-108-MD01,
AHU-02-16-019-MD01,
AHU-T3-01-003-MD01,
SF-04-08-010-MD01,
AHU-VVIP-02-003-MD03,
so i want a new list which should be like
SF-04-08
AHU-VVIP
AHU-02-17
AHU-T3-01
EF-03-32
AHU-02-16
AHU-T3-01
SF-04-08
AHU-VVIP-02
using python??
you can use strip like:
data = ['SF-04-08-010-MD01',
'AHU-VVIP-02-003-MD03',
'AHU-02-17-019-DPS03'
]
for item in data:
print ('-'.join(item.split('-')[:3]))
output:
SF-04-08
AHU-VVIP-02
AHU-02-17

Parsing List of Strings in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have the following list stored in a variable:
names = ['a.lastname1', 'b.lastname2']
I would like to parse out the '.' in each string and return a string so it reads each last name on two lines without the 'a.' and 'b.'
Simple solution using str.split() function:
names = ['a.lastname1', 'b.lastname2']
l1, l2 = (lname.split('.')[1] for lname in names)
print(l1, l2)
The output:
lastname1 lastname2

Categories

Resources